Skip to content

Commit

Permalink
MJAVADOC-775 Permit JARs when using taglets/taglet/tagletpath
Browse files Browse the repository at this point in the history
New method JavadocUtil::prunePaths takes care of pruning classpaths
correctly, the existing JavadocUtil::pruneDirs is now just a special
case delegating to prunePaths.
  • Loading branch information
kriegaex committed Dec 13, 2023
1 parent 6b45a44 commit daf8514
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2946,8 +2946,9 @@ private String getTagletPath() throws MavenReportException {
&& (StringUtils.isNotEmpty(taglet.getTagletArtifact().getVersion()))) {
pathParts.addAll(JavadocUtil.pruneFiles(getArtifactsAbsolutePath(taglet.getTagletArtifact())));
} else if (StringUtils.isNotEmpty(taglet.getTagletpath())) {
for (Path dir : JavadocUtil.pruneDirs(project, Collections.singletonList(taglet.getTagletpath()))) {
pathParts.add(dir.toString());
for (Path path :
JavadocUtil.prunePaths(project, Collections.singletonList(taglet.getTagletpath()), true)) {
pathParts.add(path.toString());
}
}
}
Expand Down
47 changes: 35 additions & 12 deletions src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -118,32 +119,54 @@ public class JavadocUtil {
+ "environment variable using -Xms:<size> and -Xmx:<size>.";

/**
* Method that removes the invalid directories in the specified directories. <b>Note</b>: All elements in
* <code>dirs</code> could be an absolute or relative against the project's base directory <code>String</code> path.
* Method that removes invalid classpath elements in the specified paths. <b>Note</b>: All elements in
* <code>paths</code> could be absolute or relative against the project's base directory <code>String</code> path.
* When pruning class paths, you can optionally include "*.jar" files in the result, otherwise only directories are
* permitted.
*
* @param project the current Maven project not null
* @param dirs the collection of <code>String</code> directories path that will be validated.
* @return a List of valid <code>String</code> directories absolute paths.
* @param paths the collection of <code>String</code> paths that will be validated
* @param includeJars whether to include JAR files in the result
* @return a list of valid <code>String</code> directories and optionally JAR files as absolute paths
*/
public static Collection<Path> pruneDirs(MavenProject project, Collection<String> dirs) {
public static Collection<Path> prunePaths(MavenProject project, Collection<String> paths, boolean includeJars) {
final Path projectBasedir = project.getBasedir().toPath();

Set<Path> pruned = new LinkedHashSet<>(dirs.size());
for (String dir : dirs) {
if (dir == null) {
Set<Path> pruned = new LinkedHashSet<>(paths.size());
for (String path : paths) {
if (path == null) {
continue;
}

Path directory = projectBasedir.resolve(dir);

if (Files.isDirectory(directory)) {
pruned.add(directory.toAbsolutePath());
Path resolvedPath = projectBasedir.resolve(path);

if (Files.isDirectory(resolvedPath)
|| includeJars
&& Files.isRegularFile(resolvedPath)
&& resolvedPath
.getFileName()
.toString()
.toLowerCase(Locale.ROOT)
.endsWith(".jar")) {
pruned.add(resolvedPath.toAbsolutePath());
}
}

return pruned;
}

/**
* Method that removes the invalid directories in the specified directories. <b>Note</b>: All elements in
* <code>dirs</code> could be absolute or relative against the project's base directory <code>String</code> path.
*
* @param project the current Maven project not null
* @param dirs the collection of <code>String</code> directories that will be validated
* @return a list of valid <code>String</code> directories as absolute paths
*/
public static Collection<Path> pruneDirs(MavenProject project, Collection<String> dirs) {
return prunePaths(project, dirs, false);
}

/**
* Method that removes the invalid files in the specified files. <b>Note</b>: All elements in <code>files</code>
* should be an absolute <code>String</code> path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -582,18 +584,45 @@ public void testCopyJavadocResources() throws Exception {
*/
public void testPruneDirs() {
List<String> list = new ArrayList<>();
list.add(getBasedir() + "/target/classes");
list.add(getBasedir() + "/target/classes");
list.add(getBasedir() + "/target/classes");
String classesDir = getBasedir() + "/target/classes";
list.add(classesDir);
list.add(classesDir);
list.add(classesDir);

Set<Path> expected = Collections.singleton(Paths.get(getBasedir(), "target/classes"));
Set<Path> expected = Collections.singleton(Paths.get(classesDir));

MavenProjectStub project = new MavenProjectStub();
project.setFile(new File(getBasedir(), "pom.xml"));

assertEquals(expected, JavadocUtil.pruneDirs(project, list));
}

/**
* Method to test prunePaths()
*
*/
public void testPrunePaths() {
List<String> list = new ArrayList<>();
String classesDir = getBasedir() + "/target/classes";
String tagletJar = getBasedir()
+ "/target/test-classes/unit/taglet-test/artifact-taglet/org/tullmann/taglets/1.0/taglets-1.0.jar";
list.add(classesDir);
list.add(classesDir);
list.add(classesDir);
list.add(tagletJar);
list.add(tagletJar);
list.add(tagletJar);

Set<Path> expectedNoJar = Collections.singleton(Paths.get(classesDir));
Set<Path> expectedWithJar = new HashSet<>(Arrays.asList(Paths.get(classesDir), Paths.get(tagletJar)));

MavenProjectStub project = new MavenProjectStub();
project.setFile(new File(getBasedir(), "pom.xml"));

assertEquals(expectedNoJar, JavadocUtil.prunePaths(project, list, false));
assertEquals(expectedWithJar, JavadocUtil.prunePaths(project, list, true));
}

/**
* Method to test unifyPathSeparator()
*
Expand Down

0 comments on commit daf8514

Please sign in to comment.