Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDEPLOY-193] Deploy At End feature (no extension) #20

Merged
merged 9 commits into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 85 additions & 27 deletions src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
Expand Up @@ -19,17 +19,16 @@
* under the License.
*/

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
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;
Expand All @@ -51,27 +50,31 @@
public class DeployMojo
extends AbstractDeployMojo
{
private static final Pattern ALT_LEGACY_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+?)::(.+)" );
private static final String DEPLOY_PROCESSED_MARKER =
DeployMojo.class.getName() + ".processed";

private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+)" );
private static final String DEPLOY_ALT_RELEASE_DEPLOYMENT_REPOSITORY =
DeployMojo.class.getName() + ".altReleaseDeploymentRepository";

/**
* When building with multiple threads, reaching the last project doesn't have to mean that all projects are ready
* to be deployed
*/
private static final AtomicInteger READYPROJECTSCOUNTER = new AtomicInteger();
private static final String DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY =
DeployMojo.class.getName() + ".altSnapshotDeploymentRepository";

private static final List<ProjectDeployerRequest> DEPLOYREQUESTS =
Collections.synchronizedList( new ArrayList<ProjectDeployerRequest>() );
private static final String DEPLOY_ALT_DEPLOYMENT_REPOSITORY =
DeployMojo.class.getName() + ".altDeploymentRepository";

private static final Pattern ALT_LEGACY_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+?)::(.+)" );

private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+)" );

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

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

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

/**
* Whether every project should be deployed during its own deploy-phase or at the end of the multimodule build. If
* set to {@code true} and the build fails, none of the reactor projects is deployed.
Expand Down Expand Up @@ -146,12 +149,12 @@ public class DeployMojo
public void execute()
throws MojoExecutionException, MojoFailureException
{
boolean addedDeployRequest = false;
if ( Boolean.parseBoolean( skip )
|| ( "releases".equals( skip ) && !ArtifactUtils.isSnapshot( project.getVersion() ) )
|| ( "snapshots".equals( skip ) && ArtifactUtils.isSnapshot( project.getVersion() ) )
)
{
getPluginContext().put( DEPLOY_PROCESSED_MARKER, Boolean.FALSE );
getLog().info( "Skipping artifact deployment" );
}
else
Expand All @@ -177,29 +180,84 @@ public void execute()
}
else
{
DEPLOYREQUESTS.add( pdr );
addedDeployRequest = true;
if ( altReleaseDeploymentRepository != null )
{
getPluginContext().put(
DEPLOY_ALT_RELEASE_DEPLOYMENT_REPOSITORY,
altReleaseDeploymentRepository
);
}
if ( altSnapshotDeploymentRepository != null )
{
getPluginContext().put(
DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY,
altSnapshotDeploymentRepository
);
}
if ( altDeploymentRepository != null )
{
getPluginContext().put(
DEPLOY_ALT_DEPLOYMENT_REPOSITORY,
altDeploymentRepository
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can add null value - I would not check for null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think that checking for null is not necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am joining this question also.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w/o nullcheck:

Caused by: java.lang.NullPointerException
    at java.util.concurrent.ConcurrentHashMap.putVal (ConcurrentHashMap.java:1011)
    at java.util.concurrent.ConcurrentHashMap.put (ConcurrentHashMap.java:1006)
    at org.apache.maven.plugins.deploy.DeployMojo.execute (DeployMojo.java:183)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)

You know, that plugin context is not a simple Map...

getPluginContext().put( DEPLOY_PROCESSED_MARKER, Boolean.TRUE );
getLog().info( "Deploying " + getProjectReferenceId( project ) + " at end" );
}
}

boolean projectsReady = READYPROJECTSCOUNTER.incrementAndGet() == reactorProjects.size();
if ( projectsReady )
if ( allProjectsMarked() )
{
synchronized ( DEPLOYREQUESTS )
for ( MavenProject reactorProject : reactorProjects )
{
while ( !DEPLOYREQUESTS.isEmpty() )
Map<String, Object> pluginContext = getSession().getPluginContext( pluginDescriptor, reactorProject );
Boolean deploy = (Boolean) pluginContext.get( DEPLOY_PROCESSED_MARKER );
if ( !deploy )
{
ArtifactRepository repo = getDeploymentRepository( DEPLOYREQUESTS.get( 0 ) );

deployProject( getSession().getProjectBuildingRequest(), DEPLOYREQUESTS.remove( 0 ), repo );
getLog().info(
"Project " + getProjectReferenceId( reactorProject ) + " skipped deploy"
);
}
else
{
String altReleaseDeploymentRepository =
(String) pluginContext.get( DEPLOY_ALT_RELEASE_DEPLOYMENT_REPOSITORY );
String altSnapshotDeploymentRepository =
(String) pluginContext.get( DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY );
String altDeploymentRepository =
(String) pluginContext.get( DEPLOY_ALT_DEPLOYMENT_REPOSITORY );

ProjectDeployerRequest pdr = new ProjectDeployerRequest()
.setProject( reactorProject )
.setRetryFailedDeploymentCount( getRetryFailedDeploymentCount() )
.setAltReleaseDeploymentRepository( altReleaseDeploymentRepository )
.setAltSnapshotDeploymentRepository( altSnapshotDeploymentRepository )
.setAltDeploymentRepository( altDeploymentRepository );

ArtifactRepository repo = getDeploymentRepository( pdr );

deployProject( getSession().getProjectBuildingRequest(), pdr, repo );
}
}
}
else if ( addedDeployRequest )
}

private String getProjectReferenceId( MavenProject mavenProject )
{
return mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() + ":" + mavenProject.getVersion();
}

private boolean allProjectsMarked()
{
for ( MavenProject reactorProject : reactorProjects )
{
getLog().info( "Deploying " + project.getGroupId() + ":" + project.getArtifactId() + ":"
+ project.getVersion() + " at end" );
Map<String, Object> pluginContext = getSession().getPluginContext( pluginDescriptor, reactorProject );
if ( !pluginContext.containsKey( DEPLOY_PROCESSED_MARKER ) )
{
return false;
}
}
return true;
}

private void deployProject( ProjectBuildingRequest pbr, ProjectDeployerRequest pir, ArtifactRepository repo )
Expand Down
18 changes: 14 additions & 4 deletions src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
Expand All @@ -28,11 +29,13 @@
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.maven.artifact.repository.ArtifactRepository;
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.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.plugins.deploy.stubs.ArtifactDeployerStub;
Expand Down Expand Up @@ -68,7 +71,6 @@ public class DeployMojoTest

MavenProjectStub project = new MavenProjectStub();

@Mock
private MavenSession session;

@InjectMocks
Expand All @@ -78,7 +80,11 @@ public void setUp()
throws Exception
{
super.setUp();


session = mock( MavenSession.class );
when( session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class)))
.thenReturn( new ConcurrentHashMap<String, Object>() );

remoteRepo = new File( REMOTE_REPO );

remoteRepo.mkdirs();
Expand Down Expand Up @@ -144,7 +150,8 @@ public void testBasicDeploy()
assertTrue( file.exists() );

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


setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );

artifact = ( DeployArtifactStub ) project.getArtifact();
Expand Down Expand Up @@ -251,8 +258,11 @@ public void testSkippingDeploy()
assertTrue( file.exists() );

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


setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
setVariableValueToObject( mojo, "session", session );

artifact = (DeployArtifactStub) project.getArtifact();

Expand Down