From e951339d93cfd42914b66063b98e25494a354ad9 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 19 Aug 2025 14:01:23 -0400 Subject: [PATCH 1/4] Specify UTF-8 --- .../org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0e7d29f4d..ee07fdb06 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -5393,7 +5393,7 @@ private List getModulesLinks() throws MavenReportException { } catch (MavenInvocationException e) { logError("MavenInvocationException: " + e.getMessage(), e); - String invokerLogContent = JavadocUtil.readFile(invokerLogFile, null /* platform encoding */); + String invokerLogContent = JavadocUtil.readFile(invokerLogFile, "UTF-8"); // TODO: Why are we only interested in cases where the JVM won't start? // [MJAVADOC-275][jdcasey] I changed the logic here to only throw an error WHEN From a5fdd59c9feddd65f2513bebcc6fc8774ade2a94 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 20 Aug 2025 06:33:29 -0400 Subject: [PATCH 2/4] use charset --- .../plugins/javadoc/AbstractJavadocMojo.java | 20 ++++--- .../maven/plugins/javadoc/JavadocUtil.java | 59 +++++++++++-------- 2 files changed, 49 insertions(+), 30 deletions(-) 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 ee07fdb06..a49446382 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -5393,13 +5393,19 @@ private List getModulesLinks() throws MavenReportException { } catch (MavenInvocationException e) { logError("MavenInvocationException: " + e.getMessage(), e); - String invokerLogContent = JavadocUtil.readFile(invokerLogFile, "UTF-8"); - - // TODO: Why are we only interested in cases where the JVM won't start? - // [MJAVADOC-275][jdcasey] I changed the logic here to only throw an error WHEN - // the JVM won't start (opposite of what it was). - if (invokerLogContent != null && invokerLogContent.contains(JavadocUtil.ERROR_INIT_VM)) { - throw new MavenReportException(e.getMessage(), e); + try { + String invokerLogContent = new String(Files.readAllBytes(invokerLogFile.toPath()), + StandardCharsets.UTF_8); + // TODO: Why are we only interested in cases where the JVM won't start? + // probably we should throw an error in all cases + // [MJAVADOC-275][jdcasey] I changed the logic here to only throw an error WHEN + // the JVM won't start (opposite of what it was). + if (invokerLogContent.contains(JavadocUtil.ERROR_INIT_VM)) { + throw new MavenReportException(e.getMessage(), e); + } + } + catch (IOException ex) { + // ignore } } finally { // just create the directory to prevent repeated invocations. diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java index dd8ccbc6c..015935028 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -35,6 +35,7 @@ import java.net.URLClassLoader; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; +import java.nio.charset.StandardCharsets; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -825,46 +826,58 @@ protected static void invokeMaven( InvocationResult result = invoke(log, invoker, request, invokerLog, goals, properties, null); if (result.getExitCode() != 0) { - String invokerLogContent = readFile(invokerLog, "UTF-8"); - - // see DefaultMaven - if (invokerLogContent != null - && (!invokerLogContent.contains("Scanning for projects...") - || invokerLogContent.contains(OutOfMemoryError.class.getName()))) { - if (log != null) { - log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS..."); - - if (log.isDebugEnabled()) { - log.debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS..."); + try { + String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), + StandardCharsets.UTF_8); + + // see DefaultMaven + if (!invokerLogContent.contains("Scanning for projects...") + || invokerLogContent.contains(OutOfMemoryError.class.getName())) { + if (log != null) { + log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS..."); + + if (log.isDebugEnabled()) { + log.debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS..."); + } } } - result = invoke(log, invoker, request, invokerLog, goals, properties, ""); + } catch (IOException e) { + // ignore } + result = invoke(log, invoker, request, invokerLog, goals, properties, ""); } if (result.getExitCode() != 0) { - String invokerLogContent = readFile(invokerLog, "UTF-8"); + try { + String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), + StandardCharsets.UTF_8); - // see DefaultMaven - if (invokerLogContent != null - && (!invokerLogContent.contains("Scanning for projects...") - || invokerLogContent.contains(OutOfMemoryError.class.getName()))) { + // see DefaultMaven + if (!invokerLogContent.contains("Scanning for projects...") + || invokerLogContent.contains(OutOfMemoryError.class.getName())) { + throw new MavenInvocationException(ERROR_INIT_VM); + } + + throw new MavenInvocationException( + "Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath()); + } catch (IOException ex) { throw new MavenInvocationException(ERROR_INIT_VM); } - - throw new MavenInvocationException( - "Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath()); } } /** * Read the given file and return the content or null if an IOException occurs. * - * @param javaFile not null - * @param encoding could be null - * @return the content of the given javaFile using the given encoding + * @param javaFile the file to read; not null + * @param encoding a character set name; not null + * @return the content of the given java file using the given encoding; null if an IOException occurs * @since 2.6.1 + * @throws java.nio.charset.UnsupportedCharsetException if the named charset is not supported + * @throws NullPointerException if the javaFile or encoding is null + * @deprecated use Files.readString(Path, Charset) in Java 11+ or new String(Files.readAllBytes(Path), Charset) in Java 8 */ + @Deprecated protected static String readFile(final File javaFile, final String encoding) { try { return new String(Files.readAllBytes(javaFile.toPath()), Charset.forName(encoding)); From 02710fd2fbe81864f6e6db767b7a84580df181f4 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 20 Aug 2025 06:43:31 -0400 Subject: [PATCH 3/4] move --- .../java/org/apache/maven/plugins/javadoc/JavadocUtil.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java index 015935028..9d5a98fb2 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -861,8 +861,9 @@ protected static void invokeMaven( throw new MavenInvocationException( "Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath()); } catch (IOException ex) { - throw new MavenInvocationException(ERROR_INIT_VM); + // ignore } + throw new MavenInvocationException(ERROR_INIT_VM); } } @@ -871,7 +872,7 @@ protected static void invokeMaven( * * @param javaFile the file to read; not null * @param encoding a character set name; not null - * @return the content of the given java file using the given encoding; null if an IOException occurs + * @return the content of the given file using the given encoding; null if an IOException occurs * @since 2.6.1 * @throws java.nio.charset.UnsupportedCharsetException if the named charset is not supported * @throws NullPointerException if the javaFile or encoding is null From ad2d8e14573d60fb701e60247e0b23da9243ee93 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 20 Aug 2025 06:49:23 -0400 Subject: [PATCH 4/4] spotless --- .../maven/plugins/javadoc/AbstractJavadocMojo.java | 7 +++---- .../apache/maven/plugins/javadoc/JavadocUtil.java | 12 +++++------- 2 files changed, 8 insertions(+), 11 deletions(-) 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 a49446382..106f92ce3 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -5394,8 +5394,8 @@ private List getModulesLinks() throws MavenReportException { logError("MavenInvocationException: " + e.getMessage(), e); try { - String invokerLogContent = new String(Files.readAllBytes(invokerLogFile.toPath()), - StandardCharsets.UTF_8); + String invokerLogContent = + new String(Files.readAllBytes(invokerLogFile.toPath()), StandardCharsets.UTF_8); // TODO: Why are we only interested in cases where the JVM won't start? // probably we should throw an error in all cases // [MJAVADOC-275][jdcasey] I changed the logic here to only throw an error WHEN @@ -5403,8 +5403,7 @@ private List getModulesLinks() throws MavenReportException { if (invokerLogContent.contains(JavadocUtil.ERROR_INIT_VM)) { throw new MavenReportException(e.getMessage(), e); } - } - catch (IOException ex) { + } catch (IOException ex) { // ignore } } finally { diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java index 9d5a98fb2..f42d2914d 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -827,12 +827,11 @@ protected static void invokeMaven( if (result.getExitCode() != 0) { try { - String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), - StandardCharsets.UTF_8); + String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), StandardCharsets.UTF_8); // see DefaultMaven if (!invokerLogContent.contains("Scanning for projects...") - || invokerLogContent.contains(OutOfMemoryError.class.getName())) { + || invokerLogContent.contains(OutOfMemoryError.class.getName())) { if (log != null) { log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS..."); @@ -842,19 +841,18 @@ protected static void invokeMaven( } } } catch (IOException e) { - // ignore + // ignore } result = invoke(log, invoker, request, invokerLog, goals, properties, ""); } if (result.getExitCode() != 0) { try { - String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), - StandardCharsets.UTF_8); + String invokerLogContent = new String(Files.readAllBytes(invokerLog.toPath()), StandardCharsets.UTF_8); // see DefaultMaven if (!invokerLogContent.contains("Scanning for projects...") - || invokerLogContent.contains(OutOfMemoryError.class.getName())) { + || invokerLogContent.contains(OutOfMemoryError.class.getName())) { throw new MavenInvocationException(ERROR_INIT_VM); }