From bdcea090e12257881d0510320785dbfb81de35cc Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 6 Oct 2021 22:54:54 +0200 Subject: [PATCH] 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. --- .../maven/plugins/install/InstallMojo.java | 41 ++++++++++--------- .../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 e4f098a5..080bafa5 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 @@ */ 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 @@ 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 void execute() { final String projectKey = project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion(); - final ConcurrentMap pluginContext = - (ConcurrentMap) session.getPluginContext( pluginDescriptor, reactorProjects.get( 0 ) ); - final Map installRequests = (Map) pluginContext.computeIfAbsent( - INSTALL_REQUESTS_KEY, - k -> Collections.synchronizedMap( new LinkedHashMap() ) - ); - 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 void execute() } else { - installRequests.put( projectKey, project ); + getPluginContext().put( INSTALL_PROCESSED_MARKER, Boolean.TRUE ); addedInstallRequest = true; } } - if ( installRequests.size() == reactorProjects.size() ) + if ( allProjectsMarked() ) { - for ( Map.Entry projectEntry : installRequests.entrySet() ) + for ( MavenProject reactorProject : reactorProjects ) { - if ( projectEntry.getValue() == SENTINEL ) + Map 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 @@ else if ( addedInstallRequest ) } } + private boolean allProjectsMarked() + { + for ( MavenProject reactorProject : reactorProjects ) + { + Map 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 5d0ed3c7..6abd82bf 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 void testSkip() 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() );