diff --git a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java index d00714cf3..1981bed20 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -4149,14 +4149,8 @@ private void addCommandLineOptions(Commandline cmd, List arguments, File StringBuilder options = new StringBuilder(); options.append(StringUtils.join(arguments.iterator(), SystemUtils.LINE_SEPARATOR)); - Charset outputFileEncoding; - if (JAVA_VERSION.isAtLeast("9") && JAVA_VERSION.isBefore("12")) { - outputFileEncoding = StandardCharsets.UTF_8; - } else { - outputFileEncoding = Charset.defaultCharset(); - } try { - Files.write(optionsFile.toPath(), Collections.singleton(options), outputFileEncoding); + Files.write(optionsFile.toPath(), Collections.singleton(options), SystemUtils.getExpectedEncoding()); } catch (IOException e) { throw new MavenReportException( "Unable to write '" + optionsFile.getName() + "' temporary file for command execution", e); @@ -4194,16 +4188,8 @@ private void addCommandLineArgFile(Commandline cmd, File javadocOutputDirectory, quotedFiles.add(JavadocUtil.quotedPathArgument(file)); } - Charset cs; - if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9") - && JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) { - cs = StandardCharsets.UTF_8; - } else { - cs = Charset.defaultCharset(); - } - try { - Files.write(argfileFile.toPath(), quotedFiles, cs); + Files.write(argfileFile.toPath(), quotedFiles, SystemUtils.getExpectedEncoding()); } catch (IOException e) { throw new MavenReportException( "Unable to write '" + argfileFile.getName() + "' temporary file for command execution", e); @@ -5005,7 +4991,8 @@ private boolean isUpToDate(Commandline cmd) throws MavenReportException { Path cacheData = staleDataPath.toPath(); List prvdata; if (Files.isRegularFile(cacheData)) { - prvdata = Files.lines(cacheData, StandardCharsets.UTF_8).collect(Collectors.toList()); + prvdata = Files.lines(cacheData, SystemUtils.getExpectedEncoding()) + .collect(Collectors.toList()); } else { prvdata = null; } diff --git a/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java b/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java index 4cf8187c9..0f84e72f2 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java @@ -20,8 +20,6 @@ import java.io.File; import java.io.IOException; -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -33,13 +31,12 @@ import org.apache.maven.reporting.MavenReportException; import org.codehaus.plexus.util.cli.Commandline; -import static java.nio.charset.StandardCharsets.UTF_8; - /** * Helper class to compute and write data used to detect a * stale javadoc. */ public class StaleHelper { + /** * Compute the data used to detect a stale javadoc * @@ -58,12 +55,8 @@ public static List getStaleData(Commandline cmd) throws MavenReportExcep for (String arg : args) { if (arg.startsWith("@")) { String name = arg.substring(1); + options.addAll(Files.readAllLines(dir.resolve(name), SystemUtils.getExpectedEncoding())); ignored.add(name); - try { - options.addAll(Files.readAllLines(dir.resolve(name), UTF_8)); - } catch (CharacterCodingException e) { - options.addAll(Files.readAllLines(dir.resolve(name), Charset.defaultCharset())); - } } } List state = new ArrayList<>(options); @@ -113,7 +106,7 @@ public static void writeStaleData(Commandline cmd, Path path) throws MavenReport try { List curdata = getStaleData(cmd); Files.createDirectories(path.getParent()); - Files.write(path, curdata, UTF_8); + Files.write(path, curdata, SystemUtils.getExpectedEncoding()); } catch (IOException e) { throw new MavenReportException("Error checking stale data", e); } diff --git a/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java b/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java index d17addba9..a10c0b138 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java @@ -19,6 +19,10 @@ package org.apache.maven.plugins.javadoc; import java.io.File; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +import org.codehaus.plexus.languages.java.version.JavaVersion; /** * Contains several OS-specific methods from Commons-Lang3's SystemUtils. We don't want to use that class because it @@ -146,6 +150,21 @@ public static File getJavaHome() { return new File(System.getProperty(JAVA_HOME_KEY)); } + /** + * Compute the encoding that Javadoc expects for reading and writing of data + * + * @return the expected encoding + * @since 3.12.1 + */ + public static Charset getExpectedEncoding() { + if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9") + && JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) { + return StandardCharsets.UTF_8; + } else { + return Charset.defaultCharset(); + } + } + /** *

* Gets a System property, defaulting to {@code null} if the property cannot be read.