Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@
</distributionManagement>

<properties>
<mavenVersion>3.0</mavenVersion>
<javaVersion>7</javaVersion>
<mavenVersion>3.2.5</mavenVersion>
<slf4jVersion>1.7.32</slf4jVersion>
<javaVersion>8</javaVersion>
<project.build.outputTimestamp>2020-04-07T21:04:00Z</project.build.outputTimestamp>
</properties>

Expand All @@ -73,13 +74,22 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>

<!-- This is here to override 3.0 coming with maven-artifact-transfer -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
Expand All @@ -96,13 +106,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>2.1</version>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency> <!-- used by maven-plugin-testing-harness, don't give it compile scope! -->
Expand All @@ -120,13 +130,31 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
<version>${slf4jVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.30</version>
<version>${slf4jVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -150,6 +178,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<cloneClean>true</cloneClean>
Expand Down
65 changes: 36 additions & 29 deletions src/main/java/org/apache/maven/plugins/install/InstallMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
import org.apache.maven.shared.transfer.project.NoFileAssignedException;
import org.apache.maven.shared.transfer.project.install.ProjectInstaller;
Expand All @@ -48,24 +50,22 @@
public class InstallMojo
extends AbstractInstallMojo
{
private static final String INSTALL_REQUESTS_KEY = InstallMojo.class.getName() + ".installRequests";

/**
* When building with multiple threads, reaching the last project doesn't have to mean that all projects are ready
* to be installed
*/
private static final AtomicInteger READYPROJECTSCOUNTER = new AtomicInteger();

private static final List<ProjectInstallerRequest> INSTALLREQUESTS =
Collections.synchronizedList( new ArrayList<ProjectInstallerRequest>() );
private static final MavenProject SENTINEL = new MavenProject();

/**
*/
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;

@Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
private List<MavenProject> reactorProjects;

@Parameter( defaultValue = "${session}", required = true, readonly = true )
private MavenSession session;

@Parameter( defaultValue = "${plugin}", required = true, readonly = true )
private PluginDescriptor pluginDescriptor;

/**
* Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
* set to {@code true} and the build fails, none of the reactor projects is installed.
Expand All @@ -91,53 +91,60 @@ public class InstallMojo
public void execute()
throws MojoExecutionException, MojoFailureException
{

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 );
getLog().info( "Skipping artifact installation" );
}
else
{
// CHECKSTYLE_OFF: LineLength
ProjectInstallerRequest projectInstallerRequest =
new ProjectInstallerRequest().setProject( project );
// CHECKSTYLE_ON: LineLength

if ( !installAtEnd )
{
installProject( session.getProjectBuildingRequest(), projectInstallerRequest );
installProject( project );
}
else
{
INSTALLREQUESTS.add( projectInstallerRequest );
installRequests.put( projectKey, project );
addedInstallRequest = true;
}
}

boolean projectsReady = READYPROJECTSCOUNTER.incrementAndGet() == reactorProjects.size();
if ( projectsReady )
if ( installRequests.size() == reactorProjects.size() )
{
synchronized ( INSTALLREQUESTS )
for ( Map.Entry<String, MavenProject> projectEntry : installRequests.entrySet() )
{
while ( !INSTALLREQUESTS.isEmpty() )
if ( projectEntry.getValue() == SENTINEL )
{
getLog().info( "Project " + projectKey + " skipped install" );
}
else
{
installProject( session.getProjectBuildingRequest(), INSTALLREQUESTS.remove( 0 ) );
installProject( projectEntry.getValue() );
}
}
}
else if ( addedInstallRequest )
{
getLog().info( "Installing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
+ project.getVersion() + " at end" );
getLog().info( "Installing " + projectKey + " at end" );
}
}

private void installProject( ProjectBuildingRequest pbr, ProjectInstallerRequest pir )
private void installProject( MavenProject pir )
throws MojoFailureException, MojoExecutionException
{
try
{
installer.install( session.getProjectBuildingRequest(), pir );
installer.install( session.getProjectBuildingRequest(), new ProjectInstallerRequest().setProject( pir ) );
}
catch ( IOException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@

import java.io.File;
import java.io.Reader;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugins.install.InstallFileMojo;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.utils.ReaderFactory;
import org.apache.maven.shared.utils.io.FileUtils;
import org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager;
import org.sonatype.aether.util.DefaultRepositorySystemSession;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.NoLocalRepositoryManagerException;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -215,7 +217,9 @@ public void testInstallFileWithPomAsPackaging()
InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );

assertNotNull( mojo );


setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );

setVariableValueToObject( mojo, "session", createMavenSession() );

assignValuesForParameter( mojo );
Expand Down Expand Up @@ -243,7 +247,9 @@ public void testInstallFile()
InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );

assertNotNull( mojo );


setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );

setVariableValueToObject( mojo, "session", createMavenSession() );

assignValuesForParameter( mojo );
Expand Down Expand Up @@ -281,11 +287,11 @@ private String dotToSlashReplacer( String parameter )
return parameter.replace( '.', '/' );
}

private MavenSession createMavenSession()
private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
{
MavenSession session = mock( MavenSession.class );
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManager( new File( LOCAL_REPO ) ) );
repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO )) );
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession( repositorySession );
when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
* under the License.
*/

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0;
import org.apache.maven.plugins.install.stubs.InstallArtifactStub;
Expand All @@ -38,8 +41,10 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.apache.maven.shared.utils.io.FileUtils;
import org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager;
import org.sonatype.aether.util.DefaultRepositorySystemSession;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.NoLocalRepositoryManagerException;

/**
* @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
Expand Down Expand Up @@ -85,7 +90,8 @@ public void testBasicInstall()

MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
updateMavenProject( project );


setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", createMavenSession() );

Expand Down Expand Up @@ -118,6 +124,7 @@ public void testBasicInstallWithAttachedArtifacts()
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
updateMavenProject( project );

setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", createMavenSession() );

Expand Down Expand Up @@ -160,6 +167,7 @@ public void testUpdateReleaseParamSetToTrue()
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
updateMavenProject( project );

setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", createMavenSession() );

Expand All @@ -186,6 +194,7 @@ public void testInstallIfArtifactFileIsNull()
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
updateMavenProject( project );

setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", createMavenSession() );

Expand Down Expand Up @@ -222,6 +231,7 @@ public void testInstallIfPackagingIsPom()
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
updateMavenProject( project );

setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", createMavenSession() );

Expand Down Expand Up @@ -258,6 +268,7 @@ public void testBasicInstallAndCreate()
MavenSession mavenSession = createMavenSession();
updateMavenProject( project );

setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", mavenSession );

Expand Down Expand Up @@ -314,6 +325,7 @@ public void testSkip()
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
updateMavenProject( project );

setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", createMavenSession() );

Expand Down Expand Up @@ -343,14 +355,15 @@ private String dotToSlashReplacer( String parameter )
return parameter.replace( '.', '/' );
}

private MavenSession createMavenSession()
private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
{
MavenSession session = mock( MavenSession.class );
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManager( new File( LOCAL_REPO ) ) );
repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO )) );
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession( repositorySession );
when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
when( session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class))).thenReturn( new ConcurrentHashMap<>() );
return session;
}

Expand Down