From 374abf80771e22d51a8d56d1e95c1e7eb0200bb9 Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Sat, 26 Oct 2024 14:01:56 -0400 Subject: [PATCH] [encoding] Use standard charset/standardstarset instead of system to work with encoding --- .../codehaus/mojo/spotbugs/SpotBugsGui.groovy | 11 +++++++---- .../mojo/spotbugs/SpotBugsMojo.groovy | 19 ++++++++++--------- .../mojo/spotbugs/XDocsReporter.groovy | 4 +++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy index 1cd250f95..a72a04821 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy @@ -17,6 +17,9 @@ package org.codehaus.mojo.spotbugs import groovy.ant.AntBuilder +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + import javax.inject.Inject import org.apache.maven.execution.MavenSession @@ -138,15 +141,15 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait { ant.java(classname: "edu.umd.cs.findbugs.LaunchAppropriateUI", fork: "true", failonerror: "true", clonevm: "true", maxmemory: "${maxHeap}m") { - String effectiveEncoding = System.getProperty("file.encoding", "UTF-8") + Charset effectiveEncoding = Charset.defaultCharset() ?: StandardCharsets.UTF_8 if (encoding) { - effectiveEncoding = encoding + effectiveEncoding = Charset.forName(encoding) } - log.info("File Encoding is " + effectiveEncoding) + log.info("File Encoding is " + effectiveEncoding.name()) - sysproperty(key: "file.encoding" , value: effectiveEncoding) + sysproperty(key: "file.encoding" , value: effectiveEncoding.name()) // spotbugs assumes that multiple arguments (because of options) means text mode, so need to request gui explicitly jvmarg(value: "-Dfindbugs.launchUI=gui2") diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy index edd12bb5e..4fa81e576 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy @@ -37,6 +37,7 @@ import org.codehaus.plexus.resource.ResourceManager import org.codehaus.plexus.resource.loader.FileResourceLoader import java.nio.charset.Charset +import java.nio.charset.StandardCharsets; import java.nio.file.Files import java.nio.file.Paths import java.util.stream.Collectors @@ -1029,7 +1030,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { forceFileCreation(sarifTempFile) } - outputEncoding = outputEncoding ?: 'UTF-8' + outputEncoding = outputEncoding ?: StandardCharsets.UTF_8 log.debug("****** Executing SpotBugsMojo *******") @@ -1063,17 +1064,17 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { List spotbugsArgs = getSpotbugsArgs(htmlTempFile, xmlTempFile, sarifTempFile) - String effectiveEncoding = System.getProperty("file.encoding", "UTF-8") + Charset effectiveEncoding = Charset.defaultCharset() ?: StandardCharsets.UTF_8 if (sourceEncoding) { - effectiveEncoding = sourceEncoding + effectiveEncoding = Charset.forName(sourceEncoding) } ant.java(classname: "edu.umd.cs.findbugs.FindBugs2", fork: "${fork}", failonerror: "true", clonevm: "false", timeout: "${timeout}", maxmemory: "${maxHeap}m") { - log.debug("File Encoding is " + effectiveEncoding) + log.debug("File Encoding is " + effectiveEncoding.name()) - sysproperty(key: "file.encoding", value: effectiveEncoding) + sysproperty(key: "file.encoding", value: effectiveEncoding.name()) if (jvmArgs && fork) { log.debug("Adding JVM Args => ${jvmArgs}") @@ -1093,7 +1094,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { classpath() { pluginArtifacts.each() { pluginArtifact -> - log.debug(" Adding to pluginArtifact ->" + pluginArtifact.file.toString()) + log.debug(" Adding to pluginArtifact -> " + pluginArtifact.file.toString()) pathelement(location: pluginArtifact.file) } @@ -1163,12 +1164,12 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { outputFile.getParentFile().mkdirs() outputFile.createNewFile() - BufferedWriter writer = Files.newBufferedWriter(outputFile.toPath(), Charset.forName(effectiveEncoding)) + BufferedWriter writer = Files.newBufferedWriter(outputFile.toPath(), effectiveEncoding) - if (effectiveEncoding.equalsIgnoreCase("Cp1252")) { + if (effectiveEncoding.name().equalsIgnoreCase("Cp1252")) { writer.write "" } else { - writer.write "" + writer.write "" } writer.write SpotBugsInfo.EOL diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/XDocsReporter.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/XDocsReporter.groovy index ad610d63a..0064af5d6 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/XDocsReporter.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/XDocsReporter.groovy @@ -20,6 +20,8 @@ import edu.umd.cs.findbugs.Version import groovy.xml.slurpersupport.GPathResult import groovy.xml.StreamingMarkupBuilder +import java.nio.charset.StandardCharsets; + import org.apache.maven.plugin.logging.Log /** @@ -132,7 +134,7 @@ class XDocsReporter { public void generateReport() { StreamingMarkupBuilder xmlBuilder = new StreamingMarkupBuilder() - xmlBuilder.encoding = "UTF-8" + xmlBuilder.encoding = StandardCharsets.UTF_8.name() def xdoc = { mkp.xmlDeclaration()