rfscholte closed pull request #14: Exclude non-exported packages when Java Modules are present. URL: https://github.com/apache/maven-javadoc-plugin/pull/14
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pom.xml b/pom.xml index dbead88..bba7bfa 100644 --- a/pom.xml +++ b/pom.xml @@ -536,7 +536,7 @@ under the License. <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludes> - <exlude>**/*Test*.java</exlude> + <exclude>**/*Test*.java</exclude> </excludes> </configuration> </plugin> diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/invoker.properties b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/invoker.properties new file mode 100644 index 0000000..81ca246 --- /dev/null +++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/invoker.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.java.version=9+ +invoker.goals=javadoc:aggregate diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/pom.xml b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/pom.xml new file mode 100644 index 0000000..e5bea45 --- /dev/null +++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/pom.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.maven.plugins.maven-javadoc-plugin.it</groupId> + <artifactId>MJAVADOC-556</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>module</artifactId> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.0</version> + <configuration> + <source>9</source> + <target>9</target> + </configuration> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/module-info.java b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/module-info.java new file mode 100644 index 0000000..ccb7c17 --- /dev/null +++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/module-info.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +module module +{ + exports package1; +} \ No newline at end of file diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package1/Main1.java b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package1/Main1.java new file mode 100644 index 0000000..f380ab9 --- /dev/null +++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package1/Main1.java @@ -0,0 +1,24 @@ +package package1; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +public final class Main1 +{ +} diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package2/Main2.java b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package2/Main2.java new file mode 100644 index 0000000..22de4c5 --- /dev/null +++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/module/src/main/java/package2/Main2.java @@ -0,0 +1,24 @@ +package package2; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +public final class Main2 +{ +} diff --git a/src/it/projects/MJAVADOC-556_hide-non-exported-packages/pom.xml b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/pom.xml new file mode 100644 index 0000000..7a12e5f --- /dev/null +++ b/src/it/projects/MJAVADOC-556_hide-non-exported-packages/pom.xml @@ -0,0 +1,49 @@ +<?xml version='1.0' encoding='UTF-8'?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.plugins.maven-javadoc-plugin.it</groupId> + <artifactId>MJAVADOC-556</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>pom</packaging> + + <url>https://issues.apache.org/jira/browse/MJAVADOC-556</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>@project.version@</version> + </plugin> + </plugins> + </pluginManagement> + </build> + <modules> + <module>module</module> + </modules> +</project> 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 523014e..1eb26a1 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -82,6 +82,7 @@ import org.codehaus.plexus.archiver.manager.ArchiverManager; import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector; +import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor; import org.codehaus.plexus.languages.java.jpms.LocationManager; import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest; import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult; @@ -1997,9 +1998,6 @@ protected void executeReport( Locale unusedLocale ) return; } - List<String> packageNames = getPackageNames( collectedSourcePaths, files ); - List<String> filesWithUnnamedPackages = getFilesWithUnnamedPackages( collectedSourcePaths, files ); - // ---------------------------------------------------------------------- // Find the javadoc executable and version // ---------------------------------------------------------------------- @@ -2015,6 +2013,17 @@ protected void executeReport( Locale unusedLocale ) } setFJavadocVersion( new File( jExecutable ) ); + List<String> packageNames; + if ( javadocRuntimeVersion.isAtLeast( "9" ) ) + { + packageNames = getPackageNamesRespectingJavaModules( sourcePaths ); + } + else + { + packageNames = getPackageNames( collectedSourcePaths, files ); + } + List<String> filesWithUnnamedPackages = getFilesWithUnnamedPackages( collectedSourcePaths, files ); + // ---------------------------------------------------------------------- // Javadoc output directory as File // ---------------------------------------------------------------------- @@ -2514,7 +2523,7 @@ protected boolean canGenerateReport( List<String> files ) * Method to get the excluded source files from the javadoc and create the argument string * that will be included in the javadoc commandline execution. * - * @param sourcePaths the collection of paths to the source files + * @param sourceFolders the collection of paths to the source files * @return a String that contains the exclude argument that will be used by javadoc * @throws MavenReportException */ @@ -4342,6 +4351,102 @@ private void copyAdditionalJavadocResources( File anOutputDirectory ) return getPackageNamesOrFilesWithUnnamedPackages( sourcePaths, files, true ); } + /** + * @param allSourcePaths not null, containing absolute and relative paths + * @return a list of exported package names for files in allSourcePaths + * @throws MavenReportException if any + * @see #getFiles + * @see #getSourcePaths() + */ + private List<String> getPackageNamesRespectingJavaModules( Map<String, Collection<String>> allSourcePaths ) + throws MavenReportException + { + List<String> returnList = new ArrayList<>(); + + if ( !StringUtils.isEmpty( sourcepath ) ) + { + return returnList; + } + LocationManager locationManager = new LocationManager(); + + for ( Collection<String> artifactSourcePaths: allSourcePaths.values() ) + { + Set<String> exportedPackages = new HashSet<>(); + boolean exportAllPackages; + File mainDescriptor = findMainDescriptor( artifactSourcePaths ); + if ( mainDescriptor != null && !isTest() ) + { + ResolvePathsRequest<File> request = + ResolvePathsRequest.withFiles( Collections.<File>emptyList() ). + setMainModuleDescriptor( mainDescriptor ); + + try + { + Set<JavaModuleDescriptor.JavaExports> exports = locationManager.resolvePaths( request ). + getMainModuleDescriptor().exports(); + if ( exports.isEmpty() ) + { + continue; + } + for ( JavaModuleDescriptor.JavaExports export : exports ) + { + exportedPackages.add( export.source() ); + } + } + catch ( IOException e ) + { + throw new MavenReportException( e.getMessage(), e ); + } + exportAllPackages = false; + } + else + { + exportAllPackages = true; + } + + for ( String currentFile : getFiles( artifactSourcePaths ) ) + { + currentFile = currentFile.replace( '\\', '/' ); + + for ( String currentSourcePath : artifactSourcePaths ) + { + currentSourcePath = currentSourcePath.replace( '\\', '/' ); + + if ( currentFile.contains( currentSourcePath ) ) + { + if ( !currentSourcePath.endsWith( "/" ) ) + { + currentSourcePath += "/"; + } + String packagename = currentFile.substring( currentSourcePath.length() + 1 ); + + /* + * Remove the miscellaneous files + * http://docs.oracle.com/javase/1.4.2/docs/tooldocs/solaris/javadoc.html#unprocessed + */ + if ( packagename.contains( "doc-files" ) ) + { + continue; + } + + if ( packagename.lastIndexOf( "/" ) != -1 ) + { + packagename = packagename.substring( 0, packagename.lastIndexOf( "/" ) ); + packagename = packagename.replace( '/', '.' ); + + if ( exportAllPackages || exportedPackages.contains( packagename ) ) + { + returnList.add( packagename ); + } + } + } + } + } + } + + return returnList; + } + /** * @param sourcePaths could be null * @param files not null ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services