Skip to content

[MDEPLOY-206] Support parallel deploy at end #5

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

Closed
wants to merge 1 commit into from
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
48 changes: 48 additions & 0 deletions src/it/deploy-at-end-parallel/module1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.deploy.dae.parallel</groupId>
<artifactId>dae</artifactId>
<version>1.0</version>
</parent>
<artifactId>module1</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<AlwaysPass />
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
48 changes: 48 additions & 0 deletions src/it/deploy-at-end-parallel/module2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.deploy.dae.parallel</groupId>
<artifactId>dae</artifactId>
<version>1.0</version>
</parent>
<artifactId>module2</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<AlwaysPass />
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
101 changes: 101 additions & 0 deletions src/it/deploy-at-end-parallel/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.deploy.dae.parallel</groupId>
<artifactId>dae</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<description>
Tests deployment at end.
</description>

<properties>
<maven.test.skip>true</maven.test.skip>
</properties>

<distributionManagement>
<repository>
<id>it</id>
<url>file:///${basedir}/target/repo</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<deployAtEnd>true</deployAtEnd>
<threads>3</threads>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.0.4</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.3.1</version>
</plugin>
</plugins>
</build>

<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
26 changes: 26 additions & 0 deletions src/it/deploy-at-end-parallel/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

assert new File( basedir, "target/repo/org/apache/maven/its/deploy/dae/parallel/dae/1.0/dae-1.0.pom" ).exists()
assert new File( basedir, "module1/target/repo/org/apache/maven/its/deploy/dae/parallel/module1/1.0/module1-1.0.pom" ).exists()
assert new File( basedir, "module2/target/repo/org/apache/maven/its/deploy/dae/parallel/module2/1.0/module2-1.0.pom" ).exists()

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
assert buildLog.text.contains( "[INFO] Deploying with 3 threads" )
78 changes: 75 additions & 3 deletions src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -82,6 +87,14 @@ public class DeployMojo
@Parameter( defaultValue = "false", property = "deployAtEnd" )
private boolean deployAtEnd;

/**
* The number of threads to use during deployment. The value will only be honored if {@code deployAtEnd} is true.
*
* @since 3.0
*/
@Parameter( defaultValue = "1", property = "maven.deploy.threads" )
private int threads = 1;

/**
* Specifies an alternative repository to which the project artifacts should be deployed ( other than those
* specified in &lt;distributionManagement&gt; ). <br/>
Expand Down Expand Up @@ -170,12 +183,71 @@ public void execute()
{
synchronized ( DEPLOYREQUESTS )
{
while ( !DEPLOYREQUESTS.isEmpty() )
int requests = DEPLOYREQUESTS.size();
CompletionService<String> service = null;
if ( threads > 1 )
{
ArtifactRepository repo = getDeploymentRepository( DEPLOYREQUESTS.get( 0 ) );
service = new ExecutorCompletionService( new ScheduledThreadPoolExecutor( threads ) );
getLog().info( "Deploying with " + threads + " threads" );
}

deployProject( getSession().getProjectBuildingRequest(), DEPLOYREQUESTS.remove( 0 ), repo );
for ( final ProjectDeployerRequest projectDeployerRequest : DEPLOYREQUESTS )
{
Callable<String> callable = new Callable<String>()
{
@Override
public String call() throws Exception
{
ArtifactRepository repo = getDeploymentRepository( projectDeployerRequest );
deployProject( getSession().getProjectBuildingRequest(), projectDeployerRequest, repo );
return projectDeployerRequest.getProject().getName();
}
};

if ( threads > 1 )
{
getLog().info( "Submitting " + projectDeployerRequest.getProject().getName() );
service.submit( callable );
}
else
Copy link

Choose a reason for hiding this comment

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

The else could be avoided by using an executor with just a single worker thread.

Copy link
Author

Choose a reason for hiding this comment

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

I did this to maintain passivity by leaving the upload on the current thread.

Copy link

Choose a reason for hiding this comment

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

You could do that by using a ExecutionService which runs the workers in the same thread like the DirectExecutor from https://google.github.io/guava/releases/24.0-jre/api/docs/index.html?com/google/common/util/concurrent/MoreExecutors.html

Copy link
Member

Choose a reason for hiding this comment

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

please no dependency on guava....

Copy link

Choose a reason for hiding this comment

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

I'd create a similar service in Maven's support libraries. Being able to switch freely between parallel and serial execution with the same code is probably useful elsewhere (like running unit tests or reports).

{
try
{
String finished = callable.call();
getLog().info( "Finished " + finished );
}
catch ( RuntimeException e )
{
throw e;
}
catch ( Exception e )
{
throw new RuntimeException( e );
}
}
}

if ( threads > 1 )
{
for ( int i = 0; i < requests; ++i )
{
try
{
String finished = service.take().get();
getLog().info( "Finished " + finished );
}
catch ( InterruptedException e )
{
throw new RuntimeException( e );
}
catch ( ExecutionException e )
{
throw new RuntimeException( e );
}
}
}

DEPLOYREQUESTS.clear();
}
}
else if ( addedDeployRequest )
Expand Down