19
19
* under the License.
20
20
*/
21
21
22
- import java .util .ArrayList ;
23
- import java .util .Collections ;
24
22
import java .util .List ;
25
- import java .util .concurrent . atomic . AtomicInteger ;
23
+ import java .util .Map ;
26
24
import java .util .regex .Matcher ;
27
25
import java .util .regex .Pattern ;
28
26
29
27
import org .apache .maven .artifact .ArtifactUtils ;
30
28
import org .apache .maven .artifact .repository .ArtifactRepository ;
31
29
import org .apache .maven .plugin .MojoExecutionException ;
32
30
import org .apache .maven .plugin .MojoFailureException ;
31
+ import org .apache .maven .plugin .descriptor .PluginDescriptor ;
33
32
import org .apache .maven .plugins .annotations .Component ;
34
33
import org .apache .maven .plugins .annotations .LifecyclePhase ;
35
34
import org .apache .maven .plugins .annotations .Mojo ;
@@ -55,23 +54,15 @@ public class DeployMojo
55
54
56
55
private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern .compile ( "(.+?)::(.+)" );
57
56
58
- /**
59
- * When building with multiple threads, reaching the last project doesn't have to mean that all projects are ready
60
- * to be deployed
61
- */
62
- private static final AtomicInteger READYPROJECTSCOUNTER = new AtomicInteger ();
63
-
64
- private static final List <ProjectDeployerRequest > DEPLOYREQUESTS =
65
- Collections .synchronizedList ( new ArrayList <ProjectDeployerRequest >() );
66
-
67
- /**
68
- */
69
57
@ Parameter ( defaultValue = "${project}" , readonly = true , required = true )
70
58
private MavenProject project ;
71
59
72
60
@ Parameter ( defaultValue = "${reactorProjects}" , required = true , readonly = true )
73
61
private List <MavenProject > reactorProjects ;
74
62
63
+ @ Parameter ( defaultValue = "${plugin}" , required = true , readonly = true )
64
+ private PluginDescriptor pluginDescriptor ;
65
+
75
66
/**
76
67
* Whether every project should be deployed during its own deploy-phase or at the end of the multimodule build. If
77
68
* set to {@code true} and the build fails, none of the reactor projects is deployed.
@@ -143,63 +134,139 @@ public class DeployMojo
143
134
@ Component
144
135
private ProjectDeployer projectDeployer ;
145
136
137
+ private enum State
138
+ {
139
+ SKIPPED , DEPLOYED , TO_BE_DEPLOYED
140
+ }
141
+
142
+ private static final String DEPLOY_PROCESSED_MARKER = DeployMojo .class .getName () + ".processed" ;
143
+
144
+ private static final String DEPLOY_ALT_RELEASE_DEPLOYMENT_REPOSITORY =
145
+ DeployMojo .class .getName () + ".altReleaseDeploymentRepository" ;
146
+
147
+ private static final String DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY =
148
+ DeployMojo .class .getName () + ".altSnapshotDeploymentRepository" ;
149
+
150
+ private static final String DEPLOY_ALT_DEPLOYMENT_REPOSITORY =
151
+ DeployMojo .class .getName () + ".altDeploymentRepository" ;
152
+
153
+ private void putState ( State state )
154
+ {
155
+ getPluginContext ().put ( DEPLOY_PROCESSED_MARKER , state .name () );
156
+ }
157
+
158
+ private void putPluginContextValue ( String key , String value )
159
+ {
160
+ if ( value != null )
161
+ {
162
+ getPluginContext ().put ( key , value );
163
+ }
164
+ }
165
+
166
+ private String getPluginContextValue ( Map <String , Object > pluginContext , String key )
167
+ {
168
+ return (String ) pluginContext .get ( key );
169
+ }
170
+
171
+ private State getState ( Map <String , Object > pluginContext )
172
+ {
173
+ return State .valueOf ( getPluginContextValue ( pluginContext , DEPLOY_PROCESSED_MARKER ) );
174
+ }
175
+
176
+ private boolean hasState ( MavenProject project )
177
+ {
178
+ Map <String , Object > pluginContext = getSession ().getPluginContext ( pluginDescriptor , project );
179
+ return pluginContext .containsKey ( DEPLOY_PROCESSED_MARKER );
180
+ }
181
+
146
182
public void execute ()
147
183
throws MojoExecutionException , MojoFailureException
148
184
{
149
- boolean addedDeployRequest = false ;
150
185
if ( Boolean .parseBoolean ( skip )
151
186
|| ( "releases" .equals ( skip ) && !ArtifactUtils .isSnapshot ( project .getVersion () ) )
152
187
|| ( "snapshots" .equals ( skip ) && ArtifactUtils .isSnapshot ( project .getVersion () ) )
153
188
)
154
189
{
155
190
getLog ().info ( "Skipping artifact deployment" );
191
+ putState ( State .SKIPPED );
156
192
}
157
193
else
158
194
{
159
195
failIfOffline ();
160
196
161
- // CHECKSTYLE_OFF: LineLength
162
- // @formatter:off
163
- ProjectDeployerRequest pdr = new ProjectDeployerRequest ()
164
- .setProject ( project )
165
- .setRetryFailedDeploymentCount ( getRetryFailedDeploymentCount () )
166
- .setAltReleaseDeploymentRepository ( altReleaseDeploymentRepository )
167
- .setAltSnapshotDeploymentRepository ( altSnapshotDeploymentRepository )
168
- .setAltDeploymentRepository ( altDeploymentRepository );
169
- // @formatter:on
170
- // CHECKSTYLE_ON: LineLength
171
-
172
- ArtifactRepository repo = getDeploymentRepository ( pdr );
173
-
174
197
if ( !deployAtEnd )
175
198
{
199
+ // CHECKSTYLE_OFF: LineLength
200
+ // @formatter:off
201
+ ProjectDeployerRequest pdr = new ProjectDeployerRequest ()
202
+ .setProject ( project )
203
+ .setRetryFailedDeploymentCount ( getRetryFailedDeploymentCount () )
204
+ .setAltReleaseDeploymentRepository ( altReleaseDeploymentRepository )
205
+ .setAltSnapshotDeploymentRepository ( altSnapshotDeploymentRepository )
206
+ .setAltDeploymentRepository ( altDeploymentRepository );
207
+ // @formatter:on
208
+ // CHECKSTYLE_ON: LineLength
209
+
210
+ ArtifactRepository repo = getDeploymentRepository ( pdr );
211
+
176
212
deployProject ( getSession ().getProjectBuildingRequest (), pdr , repo );
213
+ putState ( State .DEPLOYED );
177
214
}
178
215
else
179
216
{
180
- DEPLOYREQUESTS .add ( pdr );
181
- addedDeployRequest = true ;
217
+ putState ( State .TO_BE_DEPLOYED );
218
+ putPluginContextValue ( DEPLOY_ALT_RELEASE_DEPLOYMENT_REPOSITORY , altReleaseDeploymentRepository );
219
+ putPluginContextValue ( DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY , altSnapshotDeploymentRepository );
220
+ putPluginContextValue ( DEPLOY_ALT_DEPLOYMENT_REPOSITORY , altDeploymentRepository );
221
+ getLog ().info ( "Deferring deploy for " + getProjectReferenceId ( project ) + " at end" );
182
222
}
183
223
}
184
224
185
- boolean projectsReady = READYPROJECTSCOUNTER .incrementAndGet () == reactorProjects .size ();
186
- if ( projectsReady )
225
+ if ( allProjectsMarked () )
187
226
{
188
- synchronized ( DEPLOYREQUESTS )
227
+ for ( MavenProject reactorProject : reactorProjects )
189
228
{
190
- while ( !DEPLOYREQUESTS .isEmpty () )
229
+ Map <String , Object > pluginContext = getSession ().getPluginContext ( pluginDescriptor , reactorProject );
230
+ State state = getState ( pluginContext );
231
+ if ( state == State .TO_BE_DEPLOYED )
191
232
{
192
- ArtifactRepository repo = getDeploymentRepository ( DEPLOYREQUESTS .get ( 0 ) );
193
-
194
- deployProject ( getSession ().getProjectBuildingRequest (), DEPLOYREQUESTS .remove ( 0 ), repo );
233
+ String altReleaseDeploymentRepository =
234
+ getPluginContextValue ( pluginContext , DEPLOY_ALT_RELEASE_DEPLOYMENT_REPOSITORY );
235
+ String altSnapshotDeploymentRepository =
236
+ getPluginContextValue ( pluginContext , DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY );
237
+ String altDeploymentRepository =
238
+ getPluginContextValue ( pluginContext , DEPLOY_ALT_DEPLOYMENT_REPOSITORY );
239
+
240
+ ProjectDeployerRequest pdr = new ProjectDeployerRequest ()
241
+ .setProject ( reactorProject )
242
+ .setRetryFailedDeploymentCount ( getRetryFailedDeploymentCount () )
243
+ .setAltReleaseDeploymentRepository ( altReleaseDeploymentRepository )
244
+ .setAltSnapshotDeploymentRepository ( altSnapshotDeploymentRepository )
245
+ .setAltDeploymentRepository ( altDeploymentRepository );
246
+
247
+ ArtifactRepository repo = getDeploymentRepository ( pdr );
248
+
249
+ deployProject ( getSession ().getProjectBuildingRequest (), pdr , repo );
195
250
}
196
251
}
197
252
}
198
- else if ( addedDeployRequest )
253
+ }
254
+
255
+ private String getProjectReferenceId ( MavenProject mavenProject )
256
+ {
257
+ return mavenProject .getGroupId () + ":" + mavenProject .getArtifactId () + ":" + mavenProject .getVersion ();
258
+ }
259
+
260
+ private boolean allProjectsMarked ()
261
+ {
262
+ for ( MavenProject reactorProject : reactorProjects )
199
263
{
200
- getLog ().info ( "Deploying " + project .getGroupId () + ":" + project .getArtifactId () + ":"
201
- + project .getVersion () + " at end" );
264
+ if ( !hasState ( reactorProject ) )
265
+ {
266
+ return false ;
267
+ }
202
268
}
269
+ return true ;
203
270
}
204
271
205
272
private void deployProject ( ProjectBuildingRequest pbr , ProjectDeployerRequest pir , ArtifactRepository repo )
0 commit comments