You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2022/12/13 20:59:10 UTC

[maven-compiler-plugin] branch incremental-store-build-time created (now aea94fc)

This is an automated email from the ASF dual-hosted git repository.

olamy pushed a change to branch incremental-store-build-time
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git


      at aea94fc  take into account last compilation time to not recompile even if user has been to install phase

This branch includes the following new commits:

     new aea94fc  take into account last compilation time to not recompile even if user has been to install phase

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-compiler-plugin] 01/01: take into account last compilation time to not recompile even if user has been to install phase

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olamy pushed a commit to branch incremental-store-build-time
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git

commit aea94fc2f0e9aa27ccb119d710b6d0c359783f1c
Author: Olivier Lamy <ol...@apache.org>
AuthorDate: Wed Dec 14 06:58:58 2022 +1000

    take into account last compilation time to not recompile even if user has been to install phase
    
    Signed-off-by: Olivier Lamy <ol...@apache.org>
---
 pom.xml                                            |  2 +-
 .../plugin/compiler/AbstractCompilerMojo.java      | 92 +++++++++++++++++++---
 .../apache/maven/plugin/compiler/CompilerMojo.java |  9 ++-
 .../maven/plugin/compiler/TestCompilerMojo.java    |  5 ++
 4 files changed, 93 insertions(+), 15 deletions(-)

diff --git a/pom.xml b/pom.xml
index b4402d2..ea7255d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -143,7 +143,7 @@ under the License.
     <dependency>
       <groupId>org.apache.maven.shared</groupId>
       <artifactId>maven-shared-incremental</artifactId>
-      <version>1.1</version>
+      <version>2.0.0-SNAPSHOT</version>
       <exclusions>
         <exclusion>
           <artifactId>maven-core</artifactId>
diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
index d9e5a6d..9cd011e 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -30,6 +30,7 @@ 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.Date;
 import java.util.HashSet;
@@ -37,6 +38,7 @@ import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.Set;
 
@@ -565,6 +567,10 @@ public abstract class AbstractCompilerMojo
     @Parameter( defaultValue = "true", property = "maven.compiler.createMissingPackageInfoClass" )
     private boolean createMissingPackageInfoClass = true;
 
+    /**
+     * if <code>true</code> the reasons of the re compilation will be displayed
+     * @since 3.11.0
+     */
     @Parameter( defaultValue = "false", property = "maven.compiler.showCompilationChanges" )
     private boolean showCompilationChanges = false;
     /**
@@ -615,6 +621,8 @@ public abstract class AbstractCompilerMojo
 
     protected abstract String getDebugFileName();
 
+    protected abstract boolean isTest();
+
     protected final MavenProject getProject()
     {
         return project;
@@ -880,7 +888,8 @@ public abstract class AbstractCompilerMojo
                 
                 preparePaths( sources );
 
-                incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles( sources );
+                incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles( sources )
+                        .test( isTest( ) );
 
                 DirectoryScanResult dsr = computeInputFileTreeChanges( incrementalBuildHelper, sources );
 
@@ -920,6 +929,10 @@ public abstract class AbstractCompilerMojo
                     return;
                 }
             }
+            catch ( IOException e )
+            {
+                throw new MojoExecutionException( "Error while analyzing sources and depedencies.", e );
+            }
             catch ( CompilerException e )
             {
                 throw new MojoExecutionException( "Error while computing stale sources.", e );
@@ -933,21 +946,21 @@ public abstract class AbstractCompilerMojo
             try
             {
                 staleSources =
-                    computeStaleSources( compilerConfiguration, compiler, getSourceInclusionScanner( staleMillis ) );
+                        computeStaleSources( compilerConfiguration, compiler,
+                                getSourceInclusionScanner( staleMillis ) );
 
                 canUpdateTarget = compiler.canUpdateTarget( compilerConfiguration );
 
                 if ( compiler.getCompilerOutputStyle().equals( CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES )
-                    && !canUpdateTarget )
+                        && !canUpdateTarget )
                 {
                     getLog().info( "RESCANNING!" );
                     // TODO: This second scan for source files is sub-optimal
                     String inputFileEnding = compiler.getInputFileEnding( compilerConfiguration );
 
                     staleSources = computeStaleSources( compilerConfiguration, compiler,
-                                                             getSourceInclusionScanner( inputFileEnding ) );
+                            getSourceInclusionScanner( inputFileEnding ) );
                 }
-                
             }
             catch ( CompilerException e )
             {
@@ -1378,6 +1391,22 @@ public abstract class AbstractCompilerMojo
                 }
             }
         }
+
+        if ( useIncrementalCompilation )
+        {
+            if ( incrementalBuildHelperRequest.getOutputDirectory().exists() )
+            {
+                getLog().debug( "incrementalBuildHelper#afterBuildSuccess" );
+                // now scan the same directory again and create a diff
+                incrementalBuildHelper.afterCompilationSuccess( incrementalBuildHelperRequest );
+            }
+            else
+            {
+                getLog().debug(
+                        "skip incrementalBuildHelper#afterBuildSuccess as the output directory doesn't exist" );
+            }
+        }
+
     }
 
     private void createMissingPackageInfoClasses( CompilerConfiguration compilerConfiguration,
@@ -1747,6 +1776,7 @@ public abstract class AbstractCompilerMojo
      * @return <code>true</code> if at least one single dependency has changed.
      */
     protected boolean isDependencyChanged()
+            throws MojoExecutionException, IOException
     {
         if ( session == null )
         {
@@ -1760,6 +1790,8 @@ public abstract class AbstractCompilerMojo
             fileExtensions = Collections.unmodifiableList( Arrays.asList( "class", "jar" ) );
         }
 
+        Set<MavenProject> dependenciesInReactor = findDependenciesInReactor( project, new HashSet<>( ) );
+
         Date buildStartTime = getBuildStartTime();
 
         List<String> pathElements = new ArrayList<>();
@@ -1771,7 +1803,7 @@ public abstract class AbstractCompilerMojo
             File artifactPath = new File( pathElement );
             if ( artifactPath.isDirectory() || artifactPath.isFile() )
             {
-                if ( hasNewFile( artifactPath, buildStartTime ) )
+                if ( hasNewFile( artifactPath, buildStartTime, dependenciesInReactor ) )
                 {
                     if ( showCompilationChanges )
                     {
@@ -1790,29 +1822,67 @@ public abstract class AbstractCompilerMojo
         return false;
     }
 
+    private static Set<MavenProject> findDependenciesInReactor( MavenProject project,
+                                                                Set<MavenProject> visitedProjects )
+    {
+        if ( visitedProjects.contains( project ) )
+        {
+            return Collections.emptySet();
+        }
+        visitedProjects.add( project );
+        Collection<MavenProject> refs = project.getProjectReferences().values();
+        Set<MavenProject> availableProjects = new HashSet<>( refs );
+        for ( MavenProject ref : refs )
+        {
+            availableProjects.addAll( findDependenciesInReactor( ref, visitedProjects ) );
+        }
+        return availableProjects;
+    }
+
     /**
      * @param classPathEntry entry to check
      * @param buildStartTime time build start
      * @return if any changes occurred
      */
-    private boolean hasNewFile( File classPathEntry, Date buildStartTime )
+    private boolean hasNewFile( File classPathEntry, Date buildStartTime, Set<MavenProject> dependenciesInReactor )
+            throws MojoExecutionException, IOException
     {
         if ( !classPathEntry.exists() )
         {
             return false;
         }
 
-        if ( classPathEntry.isFile() )
+        if ( classPathEntry.isFile() && fileExtensions.contains( FileUtils.getExtension( classPathEntry.getName() ) ) )
         {
-            return classPathEntry.lastModified() >= buildStartTime.getTime()
-                    && fileExtensions.contains( FileUtils.getExtension( classPathEntry.getName() ) );
+            //but can be part of reactor and nothing modified in case of repeating mvn install
+            Optional<MavenProject> dependencyProjectReactor = dependenciesInReactor.stream()
+                    .filter( mavenProject ->
+                                    mavenProject.getArtifact().getFile() != null
+                                    && mavenProject.getArtifact().getFile().getAbsolutePath()
+                                            .equals( classPathEntry.getAbsolutePath() ) )
+                    .findFirst();
+            if ( dependencyProjectReactor.isPresent( ) )
+            {
+                IncrementalBuildHelper incrementalBuildHelper =
+                        new IncrementalBuildHelper( mojoExecution, dependencyProjectReactor.get() );
+                 long lastCompilation =
+                         incrementalBuildHelper.getLastCompilationSuccess( new IncrementalBuildHelperRequest()
+                                 .test( isTest( ) ) );
+                return lastCompilation > buildStartTime.getTime();
+            }
+            return classPathEntry.lastModified() >= buildStartTime.getTime();
         }
 
         File[] children = classPathEntry.listFiles();
 
+        if ( children == null )
+        {
+            return false;
+        }
+
         for ( File child : children )
         {
-            if ( hasNewFile( child, buildStartTime ) )
+            if ( hasNewFile( child, buildStartTime, dependenciesInReactor ) )
             {
                 return true;
             }
diff --git a/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
index 305d1e3..49e3e67 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
@@ -419,7 +419,7 @@ public class CompilerMojo
             includes.add( defaultIncludePattern );
         }
         Set<String> excludesIncr = new HashSet<>( excludes );
-        excludesIncr.addAll( excludesIncr );
+        excludesIncr.addAll( this.incrementalExcludes );
         return new SimpleSourceInclusionScanner( includes, excludesIncr );
     }
 
@@ -468,6 +468,9 @@ public class CompilerMojo
         getLog().warn( line );
     }
 
-
-
+    @Override
+    protected boolean isTest()
+    {
+        return false;
+    }
 }
diff --git a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
index ed855ab..87742fe 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
@@ -520,4 +520,9 @@ public class TestCompilerMojo
         return testExcludes;
     }
 
+    protected boolean isTest()
+    {
+        return true;
+    }
+
 }