You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2021/10/06 20:56:04 UTC

[maven-install-plugin] 01/01: Install At End, Variant 2

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

cstamas pushed a commit to branch install-at-end-pt2
in repository https://gitbox.apache.org/repos/asf/maven-install-plugin.git

commit bdcea090e12257881d0510320785dbfb81de35cc
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Wed Oct 6 22:54:54 2021 +0200

    Install At End, Variant 2
    
    This PR is inspired by original "installAtEnd" feature,
    but does not need to be run as extension.
    
    In short, it uses plugin contexts of reactor
    sorted projects to store state in.
    
    Also, plugin is updated, org.sonatype.aether removed.
---
 .../apache/maven/plugins/install/InstallMojo.java  | 41 ++++++++++++----------
 .../maven/plugins/install/InstallMojoTest.java     |  1 +
 2 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
index e4f098a..080bafa 100644
--- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
+++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
@@ -20,11 +20,8 @@ package org.apache.maven.plugins.install;
  */
 
 import java.io.IOException;
-import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.ConcurrentMap;
 
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -50,9 +47,7 @@ import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
 public class InstallMojo
     extends AbstractInstallMojo
 {
-    private static final String INSTALL_REQUESTS_KEY = InstallMojo.class.getName() + ".installRequests";
-
-    private static final MavenProject SENTINEL = new MavenProject();
+    private static final String INSTALL_PROCESSED_MARKER = InstallMojo.class.getName() + ".processed";
 
     @Parameter( defaultValue = "${project}", readonly = true, required = true )
     private MavenProject project;
@@ -93,17 +88,10 @@ public class InstallMojo
     {
 
         final String projectKey = project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion();
-        final ConcurrentMap<String, Object> pluginContext =
-                (ConcurrentMap<String, Object>) session.getPluginContext( pluginDescriptor, reactorProjects.get( 0 ) );
-        final Map<String, MavenProject> installRequests = (Map<String, MavenProject>) pluginContext.computeIfAbsent(
-                INSTALL_REQUESTS_KEY,
-                k -> Collections.synchronizedMap( new LinkedHashMap<String, MavenProject>() )
-        );
-
         boolean addedInstallRequest = false;
         if ( skip )
         {
-            installRequests.put( projectKey, SENTINEL );
+            getPluginContext().put( INSTALL_PROCESSED_MARKER, Boolean.FALSE );
             getLog().info( "Skipping artifact installation" );
         }
         else
@@ -114,22 +102,24 @@ public class InstallMojo
             }
             else
             {
-                installRequests.put( projectKey, project );
+                getPluginContext().put( INSTALL_PROCESSED_MARKER, Boolean.TRUE );
                 addedInstallRequest = true;
             }
         }
 
-        if ( installRequests.size() == reactorProjects.size() )
+        if ( allProjectsMarked() )
         {
-            for ( Map.Entry<String, MavenProject> projectEntry : installRequests.entrySet() )
+            for ( MavenProject reactorProject : reactorProjects )
             {
-                if ( projectEntry.getValue() == SENTINEL )
+                Map<String, Object> pluginContext = session.getPluginContext( pluginDescriptor, reactorProject );
+                Boolean install = (Boolean) pluginContext.get( INSTALL_PROCESSED_MARKER );
+                if ( !install )
                 {
                     getLog().info( "Project " + projectKey + " skipped install" );
                 }
                 else
                 {
-                    installProject( projectEntry.getValue() );
+                    installProject( reactorProject );
                 }
             }
         }
@@ -139,6 +129,19 @@ public class InstallMojo
         }
     }
 
+    private boolean allProjectsMarked()
+    {
+        for ( MavenProject reactorProject : reactorProjects )
+        {
+            Map<String, Object> pluginContext = session.getPluginContext( pluginDescriptor, reactorProject );
+            if ( !pluginContext.containsKey( INSTALL_PROCESSED_MARKER ) )
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
     private void installProject( MavenProject pir )
         throws MojoFailureException, MojoExecutionException
     {
diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
index 5d0ed3c..6abd82b 100644
--- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
@@ -325,6 +325,7 @@ public class InstallMojoTest
         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
         updateMavenProject( project );
 
+        setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
         setVariableValueToObject( mojo, "session", createMavenSession() );