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 ;
2622import org .apache .maven .execution .MavenSession ;
2723import org .apache .maven .plugin .AbstractMojo ;
24+ import org .apache .maven .plugin .MojoExecutionException ;
2825import org .apache .maven .plugin .MojoFailureException ;
2926import org .apache .maven .plugins .annotations .Component ;
3027import org .apache .maven .plugins .annotations .Parameter ;
3128import 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 ;
3234import org .eclipse .aether .util .version .GenericVersionScheme ;
3335import org .eclipse .aether .version .InvalidVersionSpecificationException ;
3436import org .eclipse .aether .version .Version ;
3739 * Abstract class for Deploy mojo's.
3840 */
3941public 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