You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2016/02/26 15:30:04 UTC

svn commit: r1732486 - in /maven/plugins/branches/maven-compiler-plugin_jigsaw-ea: ./ src/main/java/org/apache/maven/plugin/compiler/ src/test/java/org/apache/maven/plugin/compiler/

Author: rfscholte
Date: Fri Feb 26 14:30:04 2016
New Revision: 1732486

URL: http://svn.apache.org/viewvc?rev=1732486&view=rev
Log:
Branch of m-compiler-p with module-support, so others can test it as well

Added:
    maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/   (props changed)
      - copied from r1731262, maven/plugins/trunk/maven-compiler-plugin/
Modified:
    maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/pom.xml
    maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
    maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
    maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
    maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java

Propchange: maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Feb 26 14:30:04 2016
@@ -0,0 +1,12 @@
+target
+*~
+*.log
+*.ipr
+*.iws
+*.iml
+.idea
+.wtpmodules
+.project
+.classpath
+.settings
+bin

Propchange: maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Fri Feb 26 14:30:04 2016
@@ -0,0 +1 @@
+/maven/plugins/branches/maven-compiler-plugin-annotations:1337379-1351482

Modified: maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/pom.xml?rev=1732486&r1=1731262&r2=1732486&view=diff
==============================================================================
--- maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/pom.xml (original)
+++ maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/pom.xml Fri Feb 26 14:30:04 2016
@@ -63,7 +63,7 @@ under the License.
       ! The following property is used in the integration tests MCOMPILER-157
     -->
     <mavenPluginPluginVersion>3.3</mavenPluginPluginVersion>
-    <plexusCompilerVersion>2.7</plexusCompilerVersion>
+    <plexusCompilerVersion>2.8-SNAPSHOT</plexusCompilerVersion>
     <groovyVersion>1.8.0</groovyVersion>
     <groovyEclipseCompilerVersion>2.7.0-01</groovyEclipseCompilerVersion>
     <groovy-eclipse-batch>2.0.4-04</groovy-eclipse-batch>

Modified: maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java?rev=1732486&r1=1731262&r2=1732486&view=diff
==============================================================================
--- maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java (original)
+++ maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java Fri Feb 26 14:30:04 2016
@@ -21,6 +21,7 @@ package org.apache.maven.plugin.compiler
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
@@ -59,6 +60,8 @@ import org.codehaus.plexus.compiler.util
 import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
 
 import java.io.File;
+import java.io.IOException;
+//import java.io.IOException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Date;
@@ -68,6 +71,8 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.jar.JarFile;
+//import java.util.jar.JarFile;
 
 /**
  * TODO: At least one step could be optimized, currently the plugin will do two
@@ -447,6 +452,8 @@ public abstract class AbstractCompilerMo
     protected abstract SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding );
 
     protected abstract List<String> getClasspathElements();
+    
+    protected abstract List<String> getModulepathElements();
 
     protected abstract List<String> getCompileSourceRoots();
 
@@ -462,6 +469,11 @@ public abstract class AbstractCompilerMo
 
     protected abstract File getGeneratedSourcesDirectory();
 
+    protected final MavenProject getProject()
+    {
+        return project;
+    }
+
     @Override
     public void execute()
         throws MojoExecutionException, CompilationFailureException
@@ -519,6 +531,7 @@ public abstract class AbstractCompilerMo
         {
             getLog().debug( "Source directories: " + compileSourceRoots.toString().replace( ',', '\n' ) );
             getLog().debug( "Classpath: " + getClasspathElements().toString().replace( ',', '\n' ) );
+            getLog().debug( "Modulepath: " + getModulepathElements().toString().replace( ',', '\n' ) );
             getLog().debug( "Output directory: " + getOutputDirectory() );
         }
 
@@ -532,6 +545,8 @@ public abstract class AbstractCompilerMo
 
         compilerConfiguration.setClasspathEntries( getClasspathElements() );
 
+        compilerConfiguration.setModulepathEntries( getModulepathElements() );
+
         compilerConfiguration.setOptimize( optimize );
 
         compilerConfiguration.setDebug( debug );
@@ -819,6 +834,15 @@ public abstract class AbstractCompilerMo
                 getLog().debug( " " + s );
             }
 
+            if ( !getModulepathElements().isEmpty() )
+            {
+                getLog().debug( "Modulepath:" );
+                for ( String s : getModulepathElements() )
+                {
+                    getLog().debug( " " + s );
+                }
+            }
+
             getLog().debug( "Source roots:" );
 
             for ( String root : getCompileSourceRoots() )
@@ -1296,11 +1320,15 @@ public abstract class AbstractCompilerMo
 
         Date buildStartTime = getBuildStartTime();
 
-        for ( String classPathElement : getClasspathElements() )
+        List<String> pathElements = new ArrayList<String>();
+        pathElements.addAll( getClasspathElements() );
+        pathElements.addAll( getModulepathElements() );
+        
+        for ( String pathElement : pathElements )
         {
             // ProjectArtifacts are artifacts which are available in the local project
             // that's the only ones we are interested in now.
-            File artifactPath = new File( classPathElement );
+            File artifactPath = new File( pathElement );
             if ( artifactPath.isDirectory() )
             {
                 if ( hasNewFile( artifactPath, buildStartTime ) )
@@ -1345,7 +1373,7 @@ public abstract class AbstractCompilerMo
 
         return false;
     }
-
+    
     private List<String> resolveProcessorPathEntries()
         throws MojoExecutionException
     {
@@ -1402,4 +1430,66 @@ public abstract class AbstractCompilerMo
                 + e.getLocalizedMessage(), e );
         }
     }
+    
+    protected final boolean hasModuleDescriptor( List<String> pathElements )
+        throws DependencyResolutionRequiredException
+    {
+        boolean hasModuleDescriptor = false;
+
+        for ( String sourceLocation : getCompileSourceRoots() )
+        {
+            if ( new File( sourceLocation, "module-info.java" ).exists() )
+            {
+                hasModuleDescriptor = true;
+                break;
+            }
+        }
+
+        if ( !hasModuleDescriptor )
+        {
+            for ( String entry : pathElements )
+            {
+                File entryFile = new File( entry );
+
+                if ( entryFile.isDirectory() && new File( entryFile, "module-info.class" ).exists() )
+                {
+                    hasModuleDescriptor = true;
+                    break;
+                }
+                else
+                {
+                    JarFile jarFile = null;
+                    try
+                    {
+                        jarFile = new JarFile( entryFile, false );
+
+                        if ( jarFile.getEntry( "module-info.class" ) != null )
+                        {
+                            hasModuleDescriptor = true;
+                            break;
+                        }
+                    }
+                    catch ( IOException e )
+                    {
+                        // noop
+                    }
+                    finally
+                    {
+                        try
+                        {
+                            if ( jarFile != null )
+                            {
+                                jarFile.close();
+                            }
+                        }
+                        catch ( IOException e )
+                        {
+                            // noop
+                        }
+                    }
+                }
+            }
+        }
+        return hasModuleDescriptor;
+    }
 }

Modified: maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java?rev=1732486&r1=1731262&r2=1732486&view=diff
==============================================================================
--- maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java (original)
+++ maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java Fri Feb 26 14:30:04 2016
@@ -20,6 +20,7 @@ package org.apache.maven.plugin.compiler
  */
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Parameter;
@@ -54,12 +55,6 @@ public class CompilerMojo
     private List<String> compileSourceRoots;
 
     /**
-     * Project classpath.
-     */
-    @Parameter( defaultValue = "${project.compileClasspathElements}", readonly = true, required = true )
-    private List<String> classpathElements;
-
-    /**
      * The directory for compiled classes.
      */
     @Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
@@ -103,6 +98,10 @@ public class CompilerMojo
     @Parameter ( property = "maven.main.skip" )
     private boolean skipMain;
 
+    private List<String> classpathElements;
+
+    private List<String> modulepathElements;
+    
     protected List<String> getCompileSourceRoots()
     {
         return compileSourceRoots;
@@ -113,6 +112,12 @@ public class CompilerMojo
         return classpathElements;
     }
 
+    @Override
+    protected List<String> getModulepathElements()
+    {
+        return modulepathElements;
+    }
+
     protected File getOutputDirectory()
     {
         return outputDirectory;
@@ -126,6 +131,16 @@ public class CompilerMojo
             getLog().info( "Not compiling main sources" );
             return;
         }
+
+        try
+        {
+            setPaths();
+        }
+        catch ( DependencyResolutionRequiredException e )
+        {
+            throw new MojoExecutionException( e.getMessage() );
+        }
+
         super.execute();
 
         if ( outputDirectory.isDirectory() )
@@ -133,6 +148,22 @@ public class CompilerMojo
             projectArtifact.setFile( outputDirectory );
         }
     }
+    
+    private void setPaths() throws DependencyResolutionRequiredException
+    {
+        List<String> pathElements =
+            JavaMavenProjectUtils.getCompileClasspathElements( getProject(), getOutputDirectory() );
+        if ( hasModuleDescriptor( pathElements ) )
+        {
+            modulepathElements = pathElements;
+            classpathElements = Collections.emptyList();
+        }
+        else
+        {
+            classpathElements = pathElements;
+            modulepathElements = Collections.emptyList();
+        }
+    }
 
     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
     {

Modified: maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java?rev=1732486&r1=1731262&r2=1732486&view=diff
==============================================================================
--- maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java (original)
+++ maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java Fri Feb 26 14:30:04 2016
@@ -19,6 +19,15 @@ package org.apache.maven.plugin.compiler
  * under the License.
  */
 
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
@@ -28,13 +37,6 @@ import org.codehaus.plexus.compiler.util
 import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
 import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
 
-import java.io.File;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 /**
  * Compiles application test sources.
  *
@@ -61,12 +63,6 @@ public class TestCompilerMojo
     private List<String> compileSourceRoots;
 
     /**
-     * Project test classpath.
-     */
-    @Parameter ( defaultValue = "${project.testClasspathElements}", required = true, readonly = true )
-    private List<String> classpathElements;
-
-    /**
      * The directory where compiled test classes go.
      */
     @Parameter ( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true )
@@ -140,6 +136,9 @@ public class TestCompilerMojo
     @Parameter ( defaultValue = "${project.build.directory}/generated-test-sources/test-annotations" )
     private File generatedTestSourcesDirectory;
 
+    private List<String> classpathElements;
+
+    private List<String> modulepathElements;
 
     public void execute()
         throws MojoExecutionException, CompilationFailureException
@@ -150,6 +149,15 @@ public class TestCompilerMojo
         }
         else
         {
+            try
+            {
+                setPaths();
+            }
+            catch ( DependencyResolutionRequiredException e )
+            {
+                throw new MojoExecutionException( e.getMessage() );
+            }
+            
             super.execute();
         }
     }
@@ -163,12 +171,75 @@ public class TestCompilerMojo
     {
         return classpathElements;
     }
+    
+    @Override
+    protected List<String> getModulepathElements()
+    {
+        return modulepathElements;
+    }
 
     protected File getOutputDirectory()
     {
         return outputDirectory;
     }
 
+    private void setPaths()
+        throws DependencyResolutionRequiredException
+    {
+        boolean hasMainModuleDescriptor =
+            new File( getProject().getBuild().getOutputDirectory(), "module-info.class" ).exists();
+        
+        boolean hasTestModuleDescriptor = false;
+        for ( String sourceRoot : getProject().getTestCompileSourceRoots() )
+        {
+            hasTestModuleDescriptor |= new File( sourceRoot, "module-info.java" ).exists();
+        }
+        
+        List<String> compilePathElements =
+            JavaMavenProjectUtils.getCompileClasspathElements( getProject(),
+                                                           new File( getProject().getBuild().getOutputDirectory() ) );
+
+        List<String> testPathElements =
+                        JavaMavenProjectUtils.getTestClasspathElements( getProject(), getOutputDirectory() );
+
+        List<String> testScopedElements = new ArrayList<String>( testPathElements );
+        testScopedElements.removeAll( compilePathElements );
+        
+        if ( hasTestModuleDescriptor )
+        {
+            if ( hasMainModuleDescriptor )
+            {
+                modulepathElements = testPathElements;
+                classpathElements = Collections.emptyList();
+            }
+            else
+            {
+                modulepathElements = testScopedElements;
+                classpathElements = compilePathElements;
+                // Odd, extra arguments required
+            }
+        }
+        else
+        {
+            if ( hasMainModuleDescriptor )
+            {
+                modulepathElements = compilePathElements;
+                classpathElements = testScopedElements;
+                
+                if ( compilerArgs == null )
+                {
+                    compilerArgs = new ArrayList<String>();
+                }
+                compilerArgs.add( "-addmods" );
+            }
+            else
+            {
+                modulepathElements = Collections.emptyList();
+                classpathElements = testPathElements;
+            }
+        }
+    }
+
     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
     {
         SourceInclusionScanner scanner;

Modified: maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java?rev=1732486&r1=1731262&r2=1732486&view=diff
==============================================================================
--- maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java (original)
+++ maven/plugins/branches/maven-compiler-plugin_jigsaw-ea/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java Fri Feb 26 14:30:04 2016
@@ -32,6 +32,7 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.compiler.stubs.CompilerManagerStub;
@@ -325,26 +326,36 @@ public class CompilerMojoTestCase
         setVariableValueToObject( mojo, "outputDirectory", testClassesDir );
 
         List<String> testClasspathList = new ArrayList<String>();
+        
+        Artifact junitArtifact = mock( Artifact.class );
+        ArtifactHandler handler = mock( ArtifactHandler.class );
+        when( handler.isAddedToClasspath() ).thenReturn( true );
+        when( junitArtifact.getArtifactHandler() ).thenReturn( handler );
 
+        File artifactFile;
         String localRepository = System.getProperty( "localRepository" );
         if ( localRepository != null )
         {
-            testClasspathList.add( localRepository + "/junit/junit/3.8.1/junit-3.8.1.jar" );
+            artifactFile = new File( localRepository, "junit/junit/3.8.1/junit-3.8.1.jar" );
         }
         else
         {
             // for IDE
             String junitURI = org.junit.Test.class.getResource( "Test.class" ).toURI().toString();
             junitURI = junitURI.substring( "jar:".length(), junitURI.indexOf( '!' ) );
-            testClasspathList.add( new File( URI.create( junitURI ) ).getAbsolutePath() );
+            artifactFile = new File( URI.create( junitURI ) );
         }
+        when ( junitArtifact.getFile() ).thenReturn( artifactFile );
         
         testClasspathList.add( compilerMojo.getOutputDirectory().getPath() );
-        setVariableValueToObject( mojo, "classpathElements", testClasspathList );
 
         String testSourceRoot = testPom.getParent() + "/src/test/java";
         setVariableValueToObject( mojo, "compileSourceRoots", Collections.singletonList( testSourceRoot ) );
 
+        MavenProject project = getMockMavenProject();
+        project.setArtifacts( Collections.singleton( junitArtifact )  );
+        project.getBuild().setOutputDirectory( new File( buildDir, "classes" ).getAbsolutePath() );
+        setVariableValueToObject( mojo, "project", project );
         setVariableValueToObject( mojo, "session", getMockMavenSession() );
         setVariableValueToObject( mojo, "mojoExecution", getMockMojoExecution() );