19
19
* under the License.
20
20
*/
21
21
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 ;
26
22
import org .apache .maven .execution .MavenSession ;
27
23
import org .apache .maven .plugin .AbstractMojo ;
24
+ import org .apache .maven .plugin .MojoExecutionException ;
28
25
import org .apache .maven .plugin .MojoFailureException ;
29
26
import org .apache .maven .plugins .annotations .Component ;
30
27
import org .apache .maven .plugins .annotations .Parameter ;
31
28
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 ;
32
34
import org .eclipse .aether .util .version .GenericVersionScheme ;
33
35
import org .eclipse .aether .version .InvalidVersionSpecificationException ;
34
36
import org .eclipse .aether .version .Version ;
37
39
* Abstract class for Deploy mojo's.
38
40
*/
39
41
public abstract class AbstractDeployMojo
40
- extends AbstractMojo
42
+ extends AbstractMojo
41
43
{
42
-
43
44
/**
44
45
* Flag whether Maven is currently in online/offline mode.
45
46
*/
@@ -49,17 +50,20 @@ public abstract class AbstractDeployMojo
49
50
/**
50
51
* Parameter used to control how many times a failed deployment will be retried before giving up and failing. If a
51
52
* value outside the range 1-10 is specified it will be pulled to the nearest value within the range 1-10.
52
- *
53
+ *
53
54
* @since 2.7
54
55
*/
55
56
@ Parameter ( property = "retryFailedDeploymentCount" , defaultValue = "1" )
56
57
private int retryFailedDeploymentCount ;
57
58
59
+ @ Component
60
+ private RuntimeInformation runtimeInformation ;
61
+
58
62
@ Parameter ( defaultValue = "${session}" , readonly = true , required = true )
59
- private MavenSession session ;
63
+ protected MavenSession session ;
60
64
61
65
@ Component
62
- private RuntimeInformation runtimeInformation ;
66
+ protected RepositorySystem repositorySystem ;
63
67
64
68
private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin" ;
65
69
@@ -68,30 +72,17 @@ public abstract class AbstractDeployMojo
68
72
/* Setters and Getters */
69
73
70
74
void failIfOffline ()
71
- throws MojoFailureException
75
+ throws MojoFailureException
72
76
{
73
77
if ( offline )
74
78
{
75
79
throw new MojoFailureException ( "Cannot deploy artifacts when Maven is in offline mode" );
76
80
}
77
81
}
78
82
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
+ */
95
86
protected void warnIfAffectedPackagingAndMaven ( final String packaging )
96
87
{
97
88
if ( AFFECTED_MAVEN_PACKAGING .equals ( packaging ) )
@@ -116,4 +107,72 @@ protected void warnIfAffectedPackagingAndMaven( final String packaging )
116
107
}
117
108
}
118
109
}
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
+ }
119
178
}
0 commit comments