Skip to content

Commit 9252698

Browse files
authored
Specify UTF-8 (#1245)
* Specify UTF-8
1 parent 71a6ca4 commit 9252698

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5390,13 +5390,18 @@ private List<OfflineLink> getModulesLinks() throws MavenReportException {
53905390
} catch (MavenInvocationException e) {
53915391
logError("MavenInvocationException: " + e.getMessage(), e);
53925392

5393-
String invokerLogContent = JavadocUtil.readFile(invokerLogFile, null /* platform encoding */);
5394-
5395-
// TODO: Why are we only interested in cases where the JVM won't start?
5396-
// [MJAVADOC-275][jdcasey] I changed the logic here to only throw an error WHEN
5397-
// the JVM won't start (opposite of what it was).
5398-
if (invokerLogContent != null && invokerLogContent.contains(JavadocUtil.ERROR_INIT_VM)) {
5399-
throw new MavenReportException(e.getMessage(), e);
5393+
try {
5394+
String invokerLogContent =
5395+
new String(Files.readAllBytes(invokerLogFile.toPath()), StandardCharsets.UTF_8);
5396+
// TODO: Why are we only interested in cases where the JVM won't start?
5397+
// probably we should throw an error in all cases
5398+
// [MJAVADOC-275][jdcasey] I changed the logic here to only throw an error WHEN
5399+
// the JVM won't start (opposite of what it was).
5400+
if (invokerLogContent.contains(JavadocUtil.ERROR_INIT_VM)) {
5401+
throw new MavenReportException(e.getMessage(), e);
5402+
}
5403+
} catch (IOException ex) {
5404+
// ignore
54005405
}
54015406
} finally {
54025407
// just create the directory to prevent repeated invocations.

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.URLClassLoader;
3636
import java.nio.charset.Charset;
3737
import java.nio.charset.IllegalCharsetNameException;
38+
import java.nio.charset.StandardCharsets;
3839
import java.nio.file.FileVisitResult;
3940
import java.nio.file.Files;
4041
import java.nio.file.Path;
@@ -825,46 +826,57 @@ protected static void invokeMaven(
825826
InvocationResult result = invoke(log, invoker, request, invokerLog, goals, properties, null);
826827

827828
if (result.getExitCode() != 0) {
828-
String invokerLogContent = readFile(invokerLog, "UTF-8");
829+
try {
830+
String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), StandardCharsets.UTF_8);
829831

830-
// see DefaultMaven
831-
if (invokerLogContent != null
832-
&& (!invokerLogContent.contains("Scanning for projects...")
833-
|| invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
834-
if (log != null) {
835-
log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS...");
832+
// see DefaultMaven
833+
if (!invokerLogContent.contains("Scanning for projects...")
834+
|| invokerLogContent.contains(OutOfMemoryError.class.getName())) {
835+
if (log != null) {
836+
log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS...");
836837

837-
if (log.isDebugEnabled()) {
838-
log.debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS...");
838+
if (log.isDebugEnabled()) {
839+
log.debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS...");
840+
}
839841
}
840842
}
841-
result = invoke(log, invoker, request, invokerLog, goals, properties, "");
843+
} catch (IOException e) {
844+
// ignore
842845
}
846+
result = invoke(log, invoker, request, invokerLog, goals, properties, "");
843847
}
844848

845849
if (result.getExitCode() != 0) {
846-
String invokerLogContent = readFile(invokerLog, "UTF-8");
850+
try {
851+
String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), StandardCharsets.UTF_8);
847852

848-
// see DefaultMaven
849-
if (invokerLogContent != null
850-
&& (!invokerLogContent.contains("Scanning for projects...")
851-
|| invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
852-
throw new MavenInvocationException(ERROR_INIT_VM);
853-
}
853+
// see DefaultMaven
854+
if (!invokerLogContent.contains("Scanning for projects...")
855+
|| invokerLogContent.contains(OutOfMemoryError.class.getName())) {
856+
throw new MavenInvocationException(ERROR_INIT_VM);
857+
}
854858

855-
throw new MavenInvocationException(
856-
"Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath());
859+
throw new MavenInvocationException(
860+
"Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath());
861+
} catch (IOException ex) {
862+
// ignore
863+
}
864+
throw new MavenInvocationException(ERROR_INIT_VM);
857865
}
858866
}
859867

860868
/**
861869
* Read the given file and return the content or null if an IOException occurs.
862870
*
863-
* @param javaFile not null
864-
* @param encoding could be null
865-
* @return the content of the given javaFile using the given encoding
871+
* @param javaFile the file to read; not null
872+
* @param encoding a character set name; not null
873+
* @return the content of the given file using the given encoding; null if an IOException occurs
866874
* @since 2.6.1
875+
* @throws java.nio.charset.UnsupportedCharsetException if the named charset is not supported
876+
* @throws NullPointerException if the javaFile or encoding is null
877+
* @deprecated use Files.readString(Path, Charset) in Java 11+ or new String(Files.readAllBytes(Path), Charset) in Java 8
867878
*/
879+
@Deprecated
868880
protected static String readFile(final File javaFile, final String encoding) {
869881
try {
870882
return new String(Files.readAllBytes(javaFile.toPath()), Charset.forName(encoding));

0 commit comments

Comments
 (0)