From 314203a01e146a78ef8a2564034944d8c1b62b88 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Mon, 18 Nov 2024 21:07:07 +1000 Subject: [PATCH] [MJAVADOC-814] handle parameters such packages with multi lines (#337) * [MJAVADOC-814] handle parameters such packages with multi lines * manage other parameters with multi lines --------- Signed-off-by: Olivier Lamy --- .../plugins/javadoc/AbstractJavadocMojo.java | 2 +- .../maven/plugins/javadoc/JavadocUtil.java | 33 ++++++++++--------- .../plugins/javadoc/JavadocUtilTest.java | 24 ++++++++++++++ 3 files changed, 43 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 ecd5d77b4..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; @@ -57,6 +56,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; @@ -221,14 +221,17 @@ 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()); + arg = String.join("", list); + if (arg != null && !arg.isEmpty()) { arg = arg.replace("'", "\\'"); arg = "'" + arg + "'"; - - // To prevent javadoc error - arg = arg.replace("\n", " "); } return arg; @@ -253,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]); } @@ -291,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() + "/**"); } } @@ -422,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(); @@ -753,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()) { @@ -889,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]); } /** @@ -932,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()) { @@ -1179,7 +1182,7 @@ public String nextToken() throws NoSuchElementException { lookahead = nextToken; } } - return token; + return token.trim(); } } @@ -1223,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; @@ -1513,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 a421706ff..25c4c485d 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() { @@ -675,4 +680,23 @@ 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); + 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); + assertEquals("'org.apache.uima.analysis_component:org.apache.uima.analysis_engine'", arg); + } + + 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"); + } }