[MJAVADOC-498] "module not found" when Java 9 module-info present
Also adjust ITs to run with Java9 

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1813672 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 8e55f06..7fe5e63 100644
--- a/pom.xml
+++ b/pom.xml
@@ -222,6 +222,11 @@
     <!-- Plexus -->
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-java</artifactId>
+      <version>0.9.4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-archiver</artifactId>
       <version>3.4</version>
     </dependency>
@@ -325,6 +330,31 @@
     </pluginManagement>
     <plugins>
       <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>enforce-bytecode-version</id>
+            <configuration>
+              <rules>
+                <enforceBytecodeVersion>
+                  <ignoreClasses>
+                    <ignoreClass>module-info</ignoreClass>
+                  </ignoreClasses>
+                </enforceBytecodeVersion>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+        <dependencies>
+          <dependency>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>extra-enforcer-rules</artifactId>
+            <version>1.0-beta-6</version>
+          </dependency>
+        </dependencies>
+      </plugin>
+      <plugin>
         <groupId>org.codehaus.modello</groupId>
         <artifactId>modello-maven-plugin</artifactId>
         <executions>
@@ -385,6 +415,10 @@
                 <pomExclude>MJAVADOC-181/pom.xml</pomExclude> <!-- seems to be wrong, see comment in Jira issue  -->
                 <pomExclude>output-encoding/pom.xml</pomExclude>
               </pomExcludes>
+              <properties>
+                <maven.compiler.source>${maven.compiler.source}</maven.compiler.source>
+                <maven.compiler.target>${maven.compiler.target}</maven.compiler.target>
+              </properties>
             </configuration>
           </plugin>
         </plugins>
@@ -450,26 +484,5 @@
         </plugins>
       </build>
     </profile>
-    <profile>
-      <id>java9</id>
-      <activation>
-        <jdk>[9,)</jdk>
-      </activation>
-      <build>
-        <plugins>
-          <plugin>
-            <groupId>org.codehaus.plexus</groupId>
-            <artifactId>plexus-component-metadata</artifactId>
-            <dependencies>
-              <dependency>
-                <groupId>org.ow2.asm</groupId>
-                <artifactId>asm-all</artifactId>
-                <version>6.0_ALPHA</version>
-              </dependency>
-            </dependencies>
-          </plugin>
-        </plugins>
-      </build>
-    </profile>
   </profiles>
 </project>
diff --git a/src/it/MJAVADOC-180/pom.xml b/src/it/MJAVADOC-180/pom.xml
index ecec26b..16174b3 100644
--- a/src/it/MJAVADOC-180/pom.xml
+++ b/src/it/MJAVADOC-180/pom.xml
@@ -33,6 +33,8 @@
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.4</maven.compiler.source>
+    <maven.compiler.target>1.4</maven.compiler.target>
   </properties>
 
   <build>
@@ -50,10 +52,6 @@
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>2.0.2</version>
-        <configuration>
-          <source>1.4</source>
-          <target>1.4</target>
-        </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
diff --git a/src/it/MJAVADOC-498_modulepath/invoker.properties b/src/it/MJAVADOC-498_modulepath/invoker.properties
new file mode 100644
index 0000000..b70e6b3
--- /dev/null
+++ b/src/it/MJAVADOC-498_modulepath/invoker.properties
@@ -0,0 +1,18 @@
+# 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 = 1.9+
diff --git a/src/it/MJAVADOC-498_modulepath/pom.xml b/src/it/MJAVADOC-498_modulepath/pom.xml
new file mode 100644
index 0000000..a97ea34
--- /dev/null
+++ b/src/it/MJAVADOC-498_modulepath/pom.xml
@@ -0,0 +1,77 @@
+<?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/maven-v4_0_0.xsd">

+  <modelVersion>4.0.0</modelVersion>

+

+  <groupId>org.apache.maven.plugins.javadoc.it</groupId>

+  <artifactId>mjavadoc489</artifactId>

+  <version>1.0-SNAPSHOT</version>

+

+  <url>https://issues.apache.org/jira/browse/MJAVADOC-489</url>

+

+  <properties>

+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

+  </properties>

+

+  <dependencies>

+    <dependency>

+      <groupId>org.apache.commons</groupId>

+      <artifactId>commons-lang3</artifactId>

+      <version>3.6</version>

+    </dependency>

+    <dependency>

+      <groupId>junit</groupId>

+      <artifactId>junit</artifactId>

+      <version>4.12</version>

+      <scope>test</scope>

+    </dependency>

+  </dependencies>

+

+  <build>

+    <plugins>

+      <plugin>

+        <groupId>org.apache.maven.plugins</groupId>

+        <artifactId>maven-compiler-plugin</artifactId>

+        <version>3.7.0</version>

+        <configuration>

+          <release>9</release>

+        </configuration>

+      </plugin>

+      <plugin>

+        <groupId>org.apache.maven.plugins</groupId>

+        <artifactId>maven-javadoc-plugin</artifactId>

+        <version>@project.version@</version>

+        <executions>

+          <execution>
+            <id></id>
+            <goals>
+              <goal>jar</goal>

+              <goal>test-jar</goal>
+            </goals>
+          </execution>

+        </executions>

+      </plugin>

+    </plugins>

+  </build>

+

+</project>

diff --git a/src/it/MJAVADOC-498_modulepath/src/main/java/com/foo/MyClass.java b/src/it/MJAVADOC-498_modulepath/src/main/java/com/foo/MyClass.java
new file mode 100644
index 0000000..dae085a
--- /dev/null
+++ b/src/it/MJAVADOC-498_modulepath/src/main/java/com/foo/MyClass.java
@@ -0,0 +1,25 @@
+package com.foo;

+

+/*

+ * 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 class MyClass

+{

+

+}

diff --git a/src/it/MJAVADOC-498_modulepath/src/main/java/module-info.java b/src/it/MJAVADOC-498_modulepath/src/main/java/module-info.java
new file mode 100644
index 0000000..d9800e3
--- /dev/null
+++ b/src/it/MJAVADOC-498_modulepath/src/main/java/module-info.java
@@ -0,0 +1,22 @@
+/*

+ * 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 M.N {

+    requires org.apache.commons.lang3;

+}
\ No newline at end of file
diff --git a/src/it/MJAVADOC-498_modulepath/src/test/java/com/foo/MyTest.java b/src/it/MJAVADOC-498_modulepath/src/test/java/com/foo/MyTest.java
new file mode 100644
index 0000000..5143922
--- /dev/null
+++ b/src/it/MJAVADOC-498_modulepath/src/test/java/com/foo/MyTest.java
@@ -0,0 +1,35 @@
+package com.foo;

+

+/*

+ * 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.

+ */

+

+import static org.junit.Assert.assertNotNull;

+import org.junit.Test;

+

+public class MyTest

+{

+    private MyClass myClass = new MyClass();

+    

+    @Test

+    public void testClass() throws Exception

+    {

+        assertNotNull( myClass );

+    }

+    

+}

diff --git a/src/it/MJAVADOC-498_modulepath/verify.groovy b/src/it/MJAVADOC-498_modulepath/verify.groovy
new file mode 100644
index 0000000..bef3790
--- /dev/null
+++ b/src/it/MJAVADOC-498_modulepath/verify.groovy
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+def log = new File( basedir, 'build.log').text
+
+assert log.count( " --module-path" ) == 2
diff --git a/src/it/additional-dependencies/pom.xml b/src/it/additional-dependencies/pom.xml
index a623058..2ddf4fe 100644
--- a/src/it/additional-dependencies/pom.xml
+++ b/src/it/additional-dependencies/pom.xml
@@ -33,6 +33,8 @@
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.5</maven.compiler.source>
+    <maven.compiler.target>1.5</maven.compiler.target>
   </properties>
 
   <build>
@@ -41,10 +43,6 @@
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>2.3.2</version>
-        <configuration>
-          <source>1.5</source>
-          <target>1.5</target>
-        </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
diff --git a/src/it/javadoc-fix/pom.xml b/src/it/javadoc-fix/pom.xml
index 7a2a325..85f23ba 100644
--- a/src/it/javadoc-fix/pom.xml
+++ b/src/it/javadoc-fix/pom.xml
@@ -29,6 +29,8 @@
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.4</maven.compiler.source>
+    <maven.compiler.target>1.4</maven.compiler.target>
   </properties>
 
   <build>
@@ -43,10 +45,6 @@
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>2.0.2</version>
-        <configuration>
-          <source>1.4</source>
-          <target>1.4</target>
-        </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
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 7ae025c..4d8df75 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -74,6 +74,8 @@
 import org.apache.maven.plugin.MojoExecutionException;

 import org.apache.maven.plugin.MojoFailureException;

 import org.apache.maven.plugin.descriptor.PluginDescriptor;

+import org.apache.maven.plugins.annotations.Component;

+import org.apache.maven.plugins.annotations.Parameter;

 import org.apache.maven.plugins.javadoc.options.BootclasspathArtifact;

 import org.apache.maven.plugins.javadoc.options.DocletArtifact;

 import org.apache.maven.plugins.javadoc.options.Group;

@@ -85,8 +87,6 @@
 import org.apache.maven.plugins.javadoc.options.Taglet;

 import org.apache.maven.plugins.javadoc.options.TagletArtifact;

 import org.apache.maven.plugins.javadoc.options.io.xpp3.JavadocOptionsXpp3Writer;

-import org.apache.maven.plugins.annotations.Component;

-import org.apache.maven.plugins.annotations.Parameter;

 import org.apache.maven.plugins.javadoc.resolver.JavadocBundle;

 import org.apache.maven.plugins.javadoc.resolver.ResourceResolver;

 import org.apache.maven.plugins.javadoc.resolver.SourceResolverConfig;

@@ -117,6 +117,9 @@
 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.LocationManager;

+import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;

+import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;

 import org.codehaus.plexus.util.DirectoryScanner;

 import org.codehaus.plexus.util.FileUtils;

 import org.codehaus.plexus.util.IOUtil;

@@ -4577,8 +4580,35 @@
             addArgIf( arguments, breakiterator, "-breakiterator", SINCE_JAVADOC_1_5 );

         }

 

-        String classpath = StringUtils.join( getPathElements().iterator(), File.pathSeparator );

-        addArgIfNotEmpty( arguments, "-classpath", JavadocUtil.quotedPathArgument( classpath ) );

+        File mainDescriptor = new File( "src/main/java/module-info.java" );

+        

+        if ( mainDescriptor.exists() && !isTest() )

+        {

+            LocationManager locationManager = new LocationManager();

+            ResolvePathsRequest<File> request =

+                ResolvePathsRequest.withFiles( getPathElements() ).setMainModuleDescriptor( mainDescriptor );

+            try

+            {

+                ResolvePathsResult<File> result = locationManager.resolvePaths( request );

+

+                String classpath = StringUtils.join( result.getClasspathElements().iterator(), File.pathSeparator );

+                addArgIfNotEmpty( arguments, "--class-path", JavadocUtil.quotedPathArgument( classpath ) );

+

+                String modulepath =

+                    StringUtils.join( result.getModulepathElements().keySet().iterator(), File.pathSeparator );

+                getLog().info( "modulepath: " + modulepath );

+                addArgIfNotEmpty( arguments, "--module-path", JavadocUtil.quotedPathArgument( modulepath ) );

+            }

+            catch ( IOException e )

+            {

+                throw new MavenReportException( e.getMessage(), e );

+            }

+        }

+        else

+        {

+            String classpath = StringUtils.join( getPathElements().iterator(), File.pathSeparator );

+            addArgIfNotEmpty( arguments, "-classpath", JavadocUtil.quotedPathArgument( classpath ) );

+        }

 

         if ( StringUtils.isNotEmpty( doclet ) )

         {

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 212d488..5844d66 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -780,12 +780,22 @@
         }

         else

         {

-            cl = new URLClassLoader( new URL[] { jarFile.toURI().toURL() }, null );

+            cl = new URLClassLoader( new URL[] { jarFile.toURI().toURL() }, ClassLoader.getSystemClassLoader() );

         }

 

         List<String> tagletClasses = new ArrayList<>();

 

-        Class<?> tagletClass = cl.loadClass( "com.sun.tools.doclets.Taglet" );

+        Class<?> tagletClass;

+        

+        try 

+        {

+            tagletClass = cl.loadClass( "com.sun.tools.doclets.Taglet" );

+        }

+        catch ( ClassNotFoundException e )

+        {

+            tagletClass = cl.loadClass( "jdk.javadoc.doclet.Taglet" );

+        }

+        

         for ( String s : classes )

         {

             Class<?> c = cl.loadClass( s );

diff --git a/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java b/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java
index ecfcd28..7c00ecc 100644
--- a/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/javadoc/FixJavadocMojoTest.java
@@ -26,7 +26,9 @@
 import java.util.ArrayList;

 import java.util.Arrays;

 import java.util.List;

+import java.util.Properties;

 

+import org.apache.commons.lang3.SystemUtils;

 import org.apache.maven.plugin.logging.Log;

 import org.apache.maven.plugin.testing.AbstractMojoTestCase;

 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;

@@ -639,7 +641,18 @@
         File invokerDir = new File( getBasedir(), "target/invoker" );

         invokerDir.mkdirs();

         File invokerLogFile = FileUtils.createTempFile( "FixJavadocMojoTest", ".txt", invokerDir );

-        JavadocUtil.invokeMaven( log, new File( getBasedir(), "target/local-repo" ), testPom, goals, null,

+        

+        JavadocVersion JAVA_9 = JavadocVersion.parse( SystemUtils.JAVA_SPECIFICATION_VERSION );

+        

+        Properties properties = new Properties();

+        

+        if( JavadocVersion.parse( SystemUtils.JAVA_SPECIFICATION_VERSION ).compareTo( JAVA_9 ) >= 0 )

+        {

+            properties.put( "maven.compiler.source", "1.6" );

+            properties.put( "maven.compiler.target", "1.6" );

+        }

+        

+        JavadocUtil.invokeMaven( log, new File( getBasedir(), "target/local-repo" ), testPom, goals, properties,

                                  invokerLogFile );

     }

 

diff --git a/src/test/resources/unit/fix-test/pom.xml b/src/test/resources/unit/fix-test/pom.xml
index 8a12c87..060f218 100644
--- a/src/test/resources/unit/fix-test/pom.xml
+++ b/src/test/resources/unit/fix-test/pom.xml
@@ -25,6 +25,11 @@
   <version>1.1-SNAPSHOT</version>
   <name>Javadoc Test Project</name>
   <packaging>jar</packaging>
+  
+  <properties>
+    <maven.compiler.source>1.4</maven.compiler.source>
+    <maven.compiler.target>1.4</maven.compiler.target>
+  </properties>
 
   <build>
     <plugins>
@@ -32,10 +37,6 @@
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>2.0.2</version>
-        <configuration>
-          <source>1.4</source>
-          <target>1.4</target>
-        </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>