From 668ee503b53d40e834987e83c6ffec3ff01b838c Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Sat, 16 Nov 2024 19:52:13 +1000 Subject: [PATCH 1/3] [MJAVADOC-814] handle parameters such packages with multi lines Signed-off-by: Olivier Lamy --- .../apache/maven/plugins/javadoc/JavadocUtil.java | 7 ++++--- .../maven/plugins/javadoc/JavadocUtilTest.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 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 ecd5d77b4..961e98b76 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -57,6 +57,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import java.util.stream.Collectors; import org.apache.http.HttpHeaders; import org.apache.http.HttpHost; @@ -223,12 +224,12 @@ protected static List getExcludedPackages( protected static String quotedArgument(String value) { String arg = value; + List list = Arrays.stream(arg.split("\n")).map(String::trim).collect(Collectors.toList()); + arg = String.join("", list); + if (arg != null && !arg.isEmpty()) { arg = arg.replace("'", "\\'"); arg = "'" + arg + "'"; - - // To prevent javadoc error - arg = arg.replace("\n", " "); } return arg; diff --git a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java index a421706ff..b79f18229 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java @@ -675,4 +675,19 @@ private void stopSilently(Server server) { // ignored } } + + public void testQuotedArgument() throws Exception { + + String value = " org.apache.uima.analysis_component:\n" + + " org.apache.uima.analysis_engine\n"; + + String arg = JavadocUtil.quotedArgument(value); + assertThat(arg).isEqualTo("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'"); + + value = "org.apache.uima.analysis_component:org.apache.uima.analysis_engine"; + + arg = JavadocUtil.quotedArgument(value); + assertThat(arg).isEqualTo("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'"); + + } } From b2585879a5d249f1c11184e35ff7c329f56ab2a1 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Sun, 17 Nov 2024 21:43:44 +1000 Subject: [PATCH 2/3] manage other parameters with multi lines Signed-off-by: Olivier Lamy --- .../plugins/javadoc/AbstractJavadocMojo.java | 2 +- .../maven/plugins/javadoc/JavadocUtil.java | 28 ++++++++++--------- .../plugins/javadoc/JavadocUtilTest.java | 13 +++++++-- 3 files changed, 27 insertions(+), 16 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 9e63b7e81..99fc31ff3 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -5955,7 +5955,7 @@ protected final JavadocOptions buildJavadocOptions() throws IOException { options.setBootclasspathArtifacts(toList(bootclasspathArtifacts)); options.setDocfilesSubdirsUsed(docfilessubdirs); options.setDocletArtifacts(toList(docletArtifact, docletArtifacts)); - options.setExcludedDocfilesSubdirs(excludedocfilessubdir); + options.setExcludedDocfilesSubdirs(excludedocfilessubdir == null ? null : excludedocfilessubdir.trim()); options.setExcludePackageNames(toList(excludePackageNames)); options.setGroups(toList(groups)); options.setLinks(links); 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 961e98b76..7c723c24e 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -20,7 +20,6 @@ import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -222,9 +221,12 @@ protected static List getExcludedPackages( * @return quoted option-argument */ protected static String quotedArgument(String value) { + if (value == null) { + return null; + } String arg = value; - List list = Arrays.stream(arg.split("\n")).map(String::trim).collect(Collectors.toList()); + List list = Arrays.stream(arg.split("\n")).map(String::trim).collect(Collectors.toList()); arg = String.join("", list); if (arg != null && !arg.isEmpty()) { @@ -254,7 +256,7 @@ protected static String quotedPathArgument(String value) { for (int i = 0; i < split.length; i++) { if (i != split.length - 1) { - pathBuilder.append(split[i]).append("\\'"); + pathBuilder.append(split[i].trim()).append("\\'"); } else { pathBuilder.append(split[i]); } @@ -292,7 +294,7 @@ protected static void copyJavadocResources(File outputDirectory, File javadocDir String current; while (st.hasMoreTokens()) { current = st.nextToken(); - excludes.add("**/" + current + "/**"); + excludes.add("**/" + current.trim() + "/**"); } } @@ -423,9 +425,9 @@ protected static List getFilesFromSource( if (sourceFileIncludes == null) { sourceFileIncludes = Collections.singletonList("**/*.java"); } - ds.setIncludes(sourceFileIncludes.toArray(new String[sourceFileIncludes.size()])); - if (sourceFileExcludes != null && sourceFileExcludes.size() > 0) { - ds.setExcludes(sourceFileExcludes.toArray(new String[sourceFileExcludes.size()])); + ds.setIncludes(sourceFileIncludes.toArray(new String[0])); + if (sourceFileExcludes != null && !sourceFileExcludes.isEmpty()) { + ds.setExcludes(sourceFileExcludes.toArray(new String[0])); } ds.setBasedir(sourceDirectory); ds.scan(); @@ -754,7 +756,7 @@ protected static void invokeMaven( if (!projectFile.isFile()) { throw new IllegalArgumentException(projectFile.getAbsolutePath() + " is not a file."); } - if (goals == null || goals.size() == 0) { + if (goals == null || goals.isEmpty()) { throw new IllegalArgumentException("goals should be not empty."); } if (localRepositoryDir == null || !localRepositoryDir.isDirectory()) { @@ -890,7 +892,7 @@ protected static String[] splitPath(final String path) { subpaths.add(pathTokenizer.nextToken()); } - return subpaths.toArray(new String[subpaths.size()]); + return subpaths.toArray(new String[0]); } /** @@ -933,7 +935,7 @@ private static List getClassNamesFromJar(File jarFile) throws IOExceptio List classes = new ArrayList<>(); Pattern pattern = Pattern.compile("(?i)^(META-INF/versions/(?[0-9]+)/)?(?.+)[.]class$"); - try (JarInputStream jarStream = new JarInputStream(new FileInputStream(jarFile))) { + try (JarInputStream jarStream = new JarInputStream(Files.newInputStream(jarFile.toPath()))) { for (JarEntry jarEntry = jarStream.getNextJarEntry(); jarEntry != null; jarEntry = jarStream.getNextJarEntry()) { @@ -1180,7 +1182,7 @@ public String nextToken() throws NoSuchElementException { lookahead = nextToken; } } - return token; + return token.trim(); } } @@ -1224,7 +1226,7 @@ static List toList(String src, String elementPrefix, String elementSuffi sb.append(elementSuffix); } - result.add(sb.toString()); + result.add(sb.toString().trim()); } return result; @@ -1514,7 +1516,7 @@ private static CloseableHttpClient createHttpClient(Settings settings, URL url) builder.setUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); // Some server reject requests that do not have an Accept header - builder.setDefaultHeaders(Arrays.asList(new BasicHeader(HttpHeaders.ACCEPT, "*/*"))); + builder.setDefaultHeaders(Collections.singletonList(new BasicHeader(HttpHeaders.ACCEPT, "*/*"))); if (settings != null && settings.getActiveProxy() != null) { Proxy activeProxy = settings.getActiveProxy(); diff --git a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java index b79f18229..12b9c125f 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java @@ -654,6 +654,11 @@ public void testUnifyPathSeparator() { assertEquals( path1 + ps + path2 + ps + path1 + ps + path2, JavadocUtil.unifyPathSeparator(path1 + ";" + path2 + ":" + path1 + ":" + path2)); + + path1 = "/tmp/maven-javadoc-plugin/src/main/java;\n" + "/tmp/maven-javadoc-plugin/src/main/javadoc\n"; + assertEquals( + "/tmp/maven-javadoc-plugin/src/main/java" + ps + "/tmp/maven-javadoc-plugin/src/main/javadoc", + JavadocUtil.unifyPathSeparator(path1)); } public void testGetIncludedFiles() { @@ -678,8 +683,7 @@ private void stopSilently(Server server) { public void testQuotedArgument() throws Exception { - String value = " org.apache.uima.analysis_component:\n" + - " org.apache.uima.analysis_engine\n"; + String value = " org.apache.uima.analysis_component:\n org.apache.uima.analysis_engine\n"; String arg = JavadocUtil.quotedArgument(value); assertThat(arg).isEqualTo("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'"); @@ -688,6 +692,11 @@ public void testQuotedArgument() throws Exception { arg = JavadocUtil.quotedArgument(value); assertThat(arg).isEqualTo("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'"); + } + public void testToList() throws Exception { + String value = " *.internal:org.acme.exclude1.*:\n org.acme.exclude2\n "; + List values = JavadocUtil.toList(value); + assertThat(values).containsExactly("*.internal", "org.acme.exclude1.*", "org.acme.exclude2"); } } From 74b03e61bbe839f1ab08f8ceb8594e1a6052a37f Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Mon, 18 Nov 2024 11:22:47 +1000 Subject: [PATCH 3/3] very important cosmetic change Signed-off-by: Olivier Lamy --- .../org/apache/maven/plugins/javadoc/JavadocUtilTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java index 12b9c125f..25c4c485d 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java @@ -686,12 +686,12 @@ public void testQuotedArgument() throws Exception { String value = " org.apache.uima.analysis_component:\n org.apache.uima.analysis_engine\n"; String arg = JavadocUtil.quotedArgument(value); - assertThat(arg).isEqualTo("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'"); + assertEquals("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'", arg); value = "org.apache.uima.analysis_component:org.apache.uima.analysis_engine"; arg = JavadocUtil.quotedArgument(value); - assertThat(arg).isEqualTo("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'"); + assertEquals("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'", arg); } public void testToList() throws Exception {