maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rfscho...@apache.org
Subject [maven] 10/31: Refactored to call the resumption manager from the DefaultMaven instead of the CLI. As DefaultMaven has the right info to determine the execution root.
Date Sat, 20 Jun 2020 09:33:23 GMT
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch MNG-5760
in repository https://gitbox.apache.org/repos/asf/maven.git

commit df606590141756018ebca1e4000207e986d705ce
Author: Martin Kanters <Martin.Kanters@infosupport.com>
AuthorDate: Sat May 23 08:27:50 2020 +0200

    Refactored to call the resumption manager from the DefaultMaven instead of the CLI. As
DefaultMaven has the right info to determine the execution root.
---
 .../main/java/org/apache/maven/DefaultMaven.java   | 30 +++++++++++++++++++++-
 .../maven/execution/BuildResumptionManager.java    |  8 +++---
 .../lifecycle/LifecycleExecutionException.java     | 12 +++++++++
 .../main/java/org/apache/maven/cli/MavenCli.java   |  9 ++++---
 4 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
index fc26290..026f729 100644
--- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
+++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
@@ -30,12 +30,14 @@ import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
 import org.apache.maven.artifact.ArtifactUtils;
+import org.apache.maven.execution.BuildResumptionManager;
 import org.apache.maven.execution.DefaultMavenExecutionResult;
 import org.apache.maven.execution.ExecutionEvent;
 import org.apache.maven.execution.MavenExecutionRequest;
@@ -44,6 +46,7 @@ import org.apache.maven.execution.MavenSession;
 import org.apache.maven.execution.ProjectDependencyGraph;
 import org.apache.maven.graph.GraphBuilder;
 import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory;
+import org.apache.maven.lifecycle.LifecycleExecutionException;
 import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
 import org.apache.maven.lifecycle.internal.LifecycleStarter;
 import org.apache.maven.model.Prerequisites;
@@ -99,6 +102,9 @@ public class DefaultMaven
     @Named( GraphBuilder.HINT )
     private GraphBuilder graphBuilder;
 
+    @Inject
+    private BuildResumptionManager buildResumptionManager;
+
     @Override
     public MavenExecutionResult execute( MavenExecutionRequest request )
     {
@@ -312,7 +318,9 @@ public class DefaultMaven
 
             if ( session.getResult().hasExceptions() )
             {
-                return addExceptionToResult( result, session.getResult().getExceptions().get(
0 ) );
+                addExceptionToResult( result, session.getResult().getExceptions().get( 0
) );
+                saveResumptionDataWhenApplicable( result, session );
+                return result;
             }
         }
         finally
@@ -349,6 +357,26 @@ public class DefaultMaven
         }
     }
 
+    private void saveResumptionDataWhenApplicable( MavenExecutionResult result, MavenSession
session )
+    {
+        List<LifecycleExecutionException> lifecycleExecutionExceptions = result.getExceptions().stream()
+                .filter( LifecycleExecutionException.class::isInstance )
+                .map( LifecycleExecutionException.class::cast )
+                .collect( Collectors.toList() );
+
+        if ( !lifecycleExecutionExceptions.isEmpty() )
+        {
+            session.getAllProjects().stream()
+                    .filter( MavenProject::isExecutionRoot )
+                    .findFirst()
+                    .ifPresent( rootProject ->
+                    {
+                        boolean persisted = buildResumptionManager.persistResumptionData(
result, rootProject );
+                        lifecycleExecutionExceptions.forEach( e -> e.setBuildResumptionDataSaved(
persisted ) );
+                    } );
+        }
+    }
+
     public RepositorySystemSession newRepositorySession( MavenExecutionRequest request )
     {
         return repositorySessionFactory.newRepositorySession( request );
diff --git a/maven-core/src/main/java/org/apache/maven/execution/BuildResumptionManager.java
b/maven-core/src/main/java/org/apache/maven/execution/BuildResumptionManager.java
index 0fb22e4..2e7e775 100644
--- a/maven-core/src/main/java/org/apache/maven/execution/BuildResumptionManager.java
+++ b/maven-core/src/main/java/org/apache/maven/execution/BuildResumptionManager.java
@@ -61,7 +61,7 @@ public class BuildResumptionManager
     @Inject
     private Logger logger;
     
-    public boolean persistResumptionData( MavenExecutionResult result )
+    public boolean persistResumptionData( MavenExecutionResult result, MavenProject rootProject
)
     {
         Properties properties = determineResumptionProperties( result );
 
@@ -71,7 +71,7 @@ public class BuildResumptionManager
             return false;
         }
 
-        return writeResumptionFile( result, properties );
+        return writeResumptionFile( rootProject, properties );
     }
 
     public void applyResumptionData( MavenExecutionRequest request, MavenProject rootProject
)
@@ -206,9 +206,9 @@ public class BuildResumptionManager
                 .noneMatch( projectsGAs::contains );
     }
 
-    private boolean writeResumptionFile( MavenExecutionResult result, Properties properties
)
+    private boolean writeResumptionFile( MavenProject rootProject, Properties properties
)
     {
-        Path resumeProperties = Paths.get( result.getProject().getBuild().getDirectory(),
RESUME_PROPERTIES_FILENAME );
+        Path resumeProperties = Paths.get( rootProject.getBuild().getDirectory(), RESUME_PROPERTIES_FILENAME
);
         try
         {
             Files.createDirectories( resumeProperties.getParent() );
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutionException.java
b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutionException.java
index 0831a4f..af9821a 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutionException.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutionException.java
@@ -33,6 +33,8 @@ public class LifecycleExecutionException
 {
     private MavenProject project;
 
+    private boolean buildResumptionDataSaved = false;
+
     public LifecycleExecutionException( String message )
     {
         super( message );
@@ -76,6 +78,16 @@ public class LifecycleExecutionException
         return project;
     }
 
+    public boolean isBuildResumptionDataSaved()
+    {
+        return buildResumptionDataSaved;
+    }
+
+    public void setBuildResumptionDataSaved( boolean isBuildResumptionDataSaved )
+    {
+        this.buildResumptionDataSaved = isBuildResumptionDataSaved;
+    }
+
     private static String createMessage( MojoExecution execution, MavenProject project, Throwable
cause )
     {
         MessageBuilder buffer = buffer( 256 );
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index 113af25..657dfe0 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -992,6 +992,7 @@ public class MavenCli
             Map<String, String> references = new LinkedHashMap<>();
 
             MavenProject project = null;
+            boolean isResumptionDataSaved = false;
 
             for ( Throwable exception : result.getExceptions() )
             {
@@ -1001,7 +1002,9 @@ public class MavenCli
 
                 if ( project == null && exception instanceof LifecycleExecutionException
)
                 {
-                    project = ( (LifecycleExecutionException) exception ).getProject();
+                    LifecycleExecutionException lifecycleExecutionException = (LifecycleExecutionException)
exception;
+                    project = lifecycleExecutionException.getProject();
+                    isResumptionDataSaved = lifecycleExecutionException.isBuildResumptionDataSaved();
                 }
             }
 
@@ -1030,10 +1033,8 @@ public class MavenCli
                 }
             }
 
-            boolean resumeFileCreated = buildResumptionManager.persistResumptionData( result
);
-
             List<MavenProject> sortedProjects = result.getTopologicallySortedProjects();
-            if ( resumeFileCreated )
+            if ( isResumptionDataSaved )
             {
                 logBuildResumeHint( "mvn <args> -r " );
             }


Mime
View raw message