Skip to content

Commit b7dbd49

Browse files
committed
For writing data use always the encoding expected by Javadoc
For writing javadoc does expect a certain encoding dependending on which version of JDK we use. So we have no choice but to actually use that one. Writing only in UTF-8, will always fail in some cases.
1 parent 0af2fde commit b7dbd49

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed

src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,14 +4149,8 @@ private void addCommandLineOptions(Commandline cmd, List<String> arguments, File
41494149
StringBuilder options = new StringBuilder();
41504150
options.append(StringUtils.join(arguments.iterator(), SystemUtils.LINE_SEPARATOR));
41514151

4152-
Charset outputFileEncoding;
4153-
if (JAVA_VERSION.isAtLeast("9") && JAVA_VERSION.isBefore("12")) {
4154-
outputFileEncoding = StandardCharsets.UTF_8;
4155-
} else {
4156-
outputFileEncoding = Charset.defaultCharset();
4157-
}
41584152
try {
4159-
Files.write(optionsFile.toPath(), Collections.singleton(options), outputFileEncoding);
4153+
Files.write(optionsFile.toPath(), Collections.singleton(options), SystemUtils.getExpectedEncoding());
41604154
} catch (IOException e) {
41614155
throw new MavenReportException(
41624156
"Unable to write '" + optionsFile.getName() + "' temporary file for command execution", e);
@@ -4194,16 +4188,8 @@ private void addCommandLineArgFile(Commandline cmd, File javadocOutputDirectory,
41944188
quotedFiles.add(JavadocUtil.quotedPathArgument(file));
41954189
}
41964190

4197-
Charset cs;
4198-
if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
4199-
&& JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
4200-
cs = StandardCharsets.UTF_8;
4201-
} else {
4202-
cs = Charset.defaultCharset();
4203-
}
4204-
42054191
try {
4206-
Files.write(argfileFile.toPath(), quotedFiles, cs);
4192+
Files.write(argfileFile.toPath(), quotedFiles, SystemUtils.getExpectedEncoding());
42074193
} catch (IOException e) {
42084194
throw new MavenReportException(
42094195
"Unable to write '" + argfileFile.getName() + "' temporary file for command execution", e);
@@ -5005,7 +4991,8 @@ private boolean isUpToDate(Commandline cmd) throws MavenReportException {
50054991
Path cacheData = staleDataPath.toPath();
50064992
List<String> prvdata;
50074993
if (Files.isRegularFile(cacheData)) {
5008-
prvdata = Files.lines(cacheData, StandardCharsets.UTF_8).collect(Collectors.toList());
4994+
prvdata = Files.lines(cacheData, SystemUtils.getExpectedEncoding())
4995+
.collect(Collectors.toList());
50094996
} else {
50104997
prvdata = null;
50114998
}

src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23-
import java.nio.charset.Charset;
24-
import java.nio.charset.StandardCharsets;
2523
import java.nio.file.DirectoryStream;
2624
import java.nio.file.Files;
2725
import java.nio.file.Path;
@@ -31,7 +29,6 @@
3129
import java.util.List;
3230

3331
import org.apache.maven.reporting.MavenReportException;
34-
import org.codehaus.plexus.languages.java.version.JavaVersion;
3532
import org.codehaus.plexus.util.cli.Commandline;
3633

3734
/**
@@ -40,20 +37,6 @@
4037
*/
4138
public class StaleHelper {
4239

43-
/**
44-
* Compute the encoding of the stale javadoc
45-
*
46-
* @return the encoding of the stale data
47-
*/
48-
private static Charset getDataCharset() {
49-
if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
50-
&& JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
51-
return StandardCharsets.UTF_8;
52-
} else {
53-
return Charset.defaultCharset();
54-
}
55-
}
56-
5740
/**
5841
* Compute the data used to detect a stale javadoc
5942
*
@@ -72,7 +55,7 @@ public static List<String> getStaleData(Commandline cmd) throws MavenReportExcep
7255
for (String arg : args) {
7356
if (arg.startsWith("@")) {
7457
String name = arg.substring(1);
75-
options.addAll(Files.readAllLines(dir.resolve(name), getDataCharset()));
58+
options.addAll(Files.readAllLines(dir.resolve(name), SystemUtils.getExpectedEncoding()));
7659
ignored.add(name);
7760
}
7861
}
@@ -123,7 +106,7 @@ public static void writeStaleData(Commandline cmd, Path path) throws MavenReport
123106
try {
124107
List<String> curdata = getStaleData(cmd);
125108
Files.createDirectories(path.getParent());
126-
Files.write(path, curdata, getDataCharset());
109+
Files.write(path, curdata, SystemUtils.getExpectedEncoding());
127110
} catch (IOException e) {
128111
throw new MavenReportException("Error checking stale data", e);
129112
}

src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
package org.apache.maven.plugins.javadoc;
2020

2121
import java.io.File;
22+
import java.nio.charset.Charset;
23+
import java.nio.charset.StandardCharsets;
24+
25+
import org.codehaus.plexus.languages.java.version.JavaVersion;
2226

2327
/**
2428
* Contains several OS-specific methods from Commons-Lang3's SystemUtils. We don't want to use that class because it
@@ -146,6 +150,20 @@ public static File getJavaHome() {
146150
return new File(System.getProperty(JAVA_HOME_KEY));
147151
}
148152

153+
/**
154+
* Compute the encoding that Javadoc expects for reading and writing of data
155+
*
156+
* @return the expected encoding
157+
*/
158+
public static Charset getExpectedEncoding() {
159+
if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
160+
&& JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
161+
return StandardCharsets.UTF_8;
162+
} else {
163+
return Charset.defaultCharset();
164+
}
165+
}
166+
149167
/**
150168
* <p>
151169
* Gets a System property, defaulting to {@code null} if the property cannot be read.

0 commit comments

Comments
 (0)