Skip to content

Commit 16541da

Browse files
authored
[MDEPLOY-296] Streamline plugin (#26)
Original plugin made hoops and loops, instead to perform what it needed to perform. Partly to blame this was unfinished state of MAT API (it was able to install project only). Deploying project is needed in DeployMojo, but DeployFileMojo was forced to make hoops and loops due this, as it was passed one file (and maybe pomFile), and it was forced to create "fake" project, decorate and fake setup it with all whistle and bells, only to get it via MAT to resolver that would "decompose" it back into set of artifacts needing a deploy. So it went this file-artifact-project-artifact route, that made all the logic fragile and overly complicated. This PR completely reworks m-deploy-p making it (almost trivially) simple: it does what it needs to do, without any fuss, and does it in streamlined way: No fuss, no magic, no fake project building etc.
1 parent 89c28fb commit 16541da

File tree

8 files changed

+407
-408
lines changed

8 files changed

+407
-408
lines changed

pom.xml

+3-11
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ under the License.
6363
</distributionManagement>
6464

6565
<properties>
66+
<javaVersion>7</javaVersion>
6667
<mavenVersion>3.2.5</mavenVersion>
6768
<slf4jVersion>1.7.5</slf4jVersion> <!-- Keep in sync with resolver used in maven above -->
6869
<resolverVersion>1.0.0.v20140518</resolverVersion> <!-- Keep in sync with resolver used in maven above -->
69-
<javaVersion>7</javaVersion>
7070
<project.build.outputTimestamp>2021-12-27T14:11:19Z</project.build.outputTimestamp>
7171
</properties>
7272

@@ -102,16 +102,6 @@ under the License.
102102
<version>${slf4jVersion}</version>
103103
<scope>provided</scope>
104104
</dependency>
105-
<dependency>
106-
<groupId>org.apache.maven.shared</groupId>
107-
<artifactId>maven-artifact-transfer</artifactId>
108-
<version>0.13.1</version>
109-
</dependency>
110-
<dependency>
111-
<groupId>commons-io</groupId>
112-
<artifactId>commons-io</artifactId>
113-
<version>2.6</version>
114-
</dependency>
115105
<dependency>
116106
<groupId>org.codehaus.plexus</groupId>
117107
<artifactId>plexus-utils</artifactId>
@@ -121,11 +111,13 @@ under the License.
121111
<groupId>org.eclipse.aether</groupId>
122112
<artifactId>aether-api</artifactId>
123113
<version>${resolverVersion}</version>
114+
<scope>provided</scope>
124115
</dependency>
125116
<dependency>
126117
<groupId>org.eclipse.aether</groupId>
127118
<artifactId>aether-util</artifactId>
128119
<version>${resolverVersion}</version>
120+
<scope>compile</scope> <!-- To work in Maven versions older than 3.9.0 -->
129121
</dependency>
130122

131123
<!-- dependencies to annotations -->

src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java

+85-26
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
* under the License.
2020
*/
2121

22-
import org.apache.maven.artifact.repository.ArtifactRepository;
23-
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
24-
import org.apache.maven.artifact.repository.MavenArtifactRepository;
25-
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
2622
import org.apache.maven.execution.MavenSession;
2723
import org.apache.maven.plugin.AbstractMojo;
24+
import org.apache.maven.plugin.MojoExecutionException;
2825
import org.apache.maven.plugin.MojoFailureException;
2926
import org.apache.maven.plugins.annotations.Component;
3027
import org.apache.maven.plugins.annotations.Parameter;
3128
import org.apache.maven.rtinfo.RuntimeInformation;
29+
import org.eclipse.aether.RepositorySystem;
30+
import org.eclipse.aether.RepositorySystemSession;
31+
import org.eclipse.aether.deployment.DeployRequest;
32+
import org.eclipse.aether.deployment.DeploymentException;
33+
import org.eclipse.aether.repository.RemoteRepository;
3234
import org.eclipse.aether.util.version.GenericVersionScheme;
3335
import org.eclipse.aether.version.InvalidVersionSpecificationException;
3436
import org.eclipse.aether.version.Version;
@@ -37,9 +39,8 @@
3739
* Abstract class for Deploy mojo's.
3840
*/
3941
public abstract class AbstractDeployMojo
40-
extends AbstractMojo
42+
extends AbstractMojo
4143
{
42-
4344
/**
4445
* Flag whether Maven is currently in online/offline mode.
4546
*/
@@ -49,17 +50,20 @@ public abstract class AbstractDeployMojo
4950
/**
5051
* Parameter used to control how many times a failed deployment will be retried before giving up and failing. If a
5152
* value outside the range 1-10 is specified it will be pulled to the nearest value within the range 1-10.
52-
*
53+
*
5354
* @since 2.7
5455
*/
5556
@Parameter( property = "retryFailedDeploymentCount", defaultValue = "1" )
5657
private int retryFailedDeploymentCount;
5758

59+
@Component
60+
private RuntimeInformation runtimeInformation;
61+
5862
@Parameter( defaultValue = "${session}", readonly = true, required = true )
59-
private MavenSession session;
63+
protected MavenSession session;
6064

6165
@Component
62-
private RuntimeInformation runtimeInformation;
66+
protected RepositorySystem repositorySystem;
6367

6468
private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin";
6569

@@ -68,30 +72,17 @@ public abstract class AbstractDeployMojo
6872
/* Setters and Getters */
6973

7074
void failIfOffline()
71-
throws MojoFailureException
75+
throws MojoFailureException
7276
{
7377
if ( offline )
7478
{
7579
throw new MojoFailureException( "Cannot deploy artifacts when Maven is in offline mode" );
7680
}
7781
}
7882

79-
int getRetryFailedDeploymentCount()
80-
{
81-
return retryFailedDeploymentCount;
82-
}
83-
84-
protected ArtifactRepository createDeploymentArtifactRepository( String id, String url )
85-
{
86-
return new MavenArtifactRepository( id, url, new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
87-
new ArtifactRepositoryPolicy() );
88-
}
89-
90-
protected final MavenSession getSession()
91-
{
92-
return session;
93-
}
94-
83+
/**
84+
* If this plugin used in pre-3.9.0 Maven, the packaging {@code maven-plugin} will not deploy G level metadata.
85+
*/
9586
protected void warnIfAffectedPackagingAndMaven( final String packaging )
9687
{
9788
if ( AFFECTED_MAVEN_PACKAGING.equals( packaging ) )
@@ -116,4 +107,72 @@ protected void warnIfAffectedPackagingAndMaven( final String packaging )
116107
}
117108
}
118109
}
110+
111+
/**
112+
* Creates resolver {@link RemoteRepository} equipped with needed whistles and bells.
113+
*/
114+
protected RemoteRepository getRemoteRepository( final String repositoryId, final String url )
115+
{
116+
RemoteRepository result = new RemoteRepository.Builder( repositoryId, "default", url ).build();
117+
118+
if ( result.getAuthentication() == null || result.getProxy() == null )
119+
{
120+
RemoteRepository.Builder builder = new RemoteRepository.Builder( result );
121+
122+
if ( result.getAuthentication() == null )
123+
{
124+
builder.setAuthentication( session.getRepositorySession().getAuthenticationSelector()
125+
.getAuthentication( result ) );
126+
}
127+
128+
if ( result.getProxy() == null )
129+
{
130+
builder.setProxy( session.getRepositorySession().getProxySelector().getProxy( result ) );
131+
}
132+
133+
result = builder.build();
134+
}
135+
136+
return result;
137+
}
138+
139+
/**
140+
* Handles high level retries (this was buried into MAT).
141+
*/
142+
protected void deploy( RepositorySystemSession session, DeployRequest deployRequest ) throws MojoExecutionException
143+
{
144+
int retryFailedDeploymentCounter = Math.max( 1, Math.min( 10, retryFailedDeploymentCount ) );
145+
DeploymentException exception = null;
146+
for ( int count = 0; count < retryFailedDeploymentCounter; count++ )
147+
{
148+
try
149+
{
150+
if ( count > 0 )
151+
{
152+
getLog().info( "Retrying deployment attempt " + ( count + 1 ) + " of "
153+
+ retryFailedDeploymentCounter );
154+
}
155+
156+
repositorySystem.deploy( session, deployRequest );
157+
exception = null;
158+
break;
159+
}
160+
catch ( DeploymentException e )
161+
{
162+
if ( count + 1 < retryFailedDeploymentCounter )
163+
{
164+
getLog().warn( "Encountered issue during deployment: " + e.getLocalizedMessage() );
165+
getLog().debug( e );
166+
}
167+
if ( exception == null )
168+
{
169+
exception = e;
170+
}
171+
}
172+
}
173+
if ( exception != null )
174+
{
175+
throw new MojoExecutionException( exception.getMessage(), exception );
176+
}
177+
}
119178
}

0 commit comments

Comments
 (0)