1919 * under the License.
2020 */
2121
22- import java .util .ArrayList ;
23- import java .util .Collections ;
2422import java .util .List ;
25- import java .util .concurrent . atomic . AtomicInteger ;
23+ import java .util .Map ;
2624import java .util .regex .Matcher ;
2725import java .util .regex .Pattern ;
2826
2927import org .apache .maven .artifact .ArtifactUtils ;
3028import org .apache .maven .artifact .repository .ArtifactRepository ;
3129import org .apache .maven .plugin .MojoExecutionException ;
3230import org .apache .maven .plugin .MojoFailureException ;
31+ import org .apache .maven .plugin .descriptor .PluginDescriptor ;
3332import org .apache .maven .plugins .annotations .Component ;
3433import org .apache .maven .plugins .annotations .LifecyclePhase ;
3534import org .apache .maven .plugins .annotations .Mojo ;
@@ -55,23 +54,15 @@ public class DeployMojo
5554
5655 private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern .compile ( "(.+?)::(.+)" );
5756
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- */
6957 @ Parameter ( defaultValue = "${project}" , readonly = true , required = true )
7058 private MavenProject project ;
7159
7260 @ Parameter ( defaultValue = "${reactorProjects}" , required = true , readonly = true )
7361 private List <MavenProject > reactorProjects ;
7462
63+ @ Parameter ( defaultValue = "${plugin}" , required = true , readonly = true )
64+ private PluginDescriptor pluginDescriptor ;
65+
7566 /**
7667 * Whether every project should be deployed during its own deploy-phase or at the end of the multimodule build. If
7768 * set to {@code true} and the build fails, none of the reactor projects is deployed.
@@ -143,63 +134,139 @@ public class DeployMojo
143134 @ Component
144135 private ProjectDeployer projectDeployer ;
145136
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+
146182 public void execute ()
147183 throws MojoExecutionException , MojoFailureException
148184 {
149- boolean addedDeployRequest = false ;
150185 if ( Boolean .parseBoolean ( skip )
151186 || ( "releases" .equals ( skip ) && !ArtifactUtils .isSnapshot ( project .getVersion () ) )
152187 || ( "snapshots" .equals ( skip ) && ArtifactUtils .isSnapshot ( project .getVersion () ) )
153188 )
154189 {
155190 getLog ().info ( "Skipping artifact deployment" );
191+ putState ( State .SKIPPED );
156192 }
157193 else
158194 {
159195 failIfOffline ();
160196
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-
174197 if ( !deployAtEnd )
175198 {
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+
176212 deployProject ( getSession ().getProjectBuildingRequest (), pdr , repo );
213+ putState ( State .DEPLOYED );
177214 }
178215 else
179216 {
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" );
182222 }
183223 }
184224
185- boolean projectsReady = READYPROJECTSCOUNTER .incrementAndGet () == reactorProjects .size ();
186- if ( projectsReady )
225+ if ( allProjectsMarked () )
187226 {
188- synchronized ( DEPLOYREQUESTS )
227+ for ( MavenProject reactorProject : reactorProjects )
189228 {
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 )
191232 {
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 );
195250 }
196251 }
197252 }
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 )
199263 {
200- getLog ().info ( "Deploying " + project .getGroupId () + ":" + project .getArtifactId () + ":"
201- + project .getVersion () + " at end" );
264+ if ( !hasState ( reactorProject ) )
265+ {
266+ return false ;
267+ }
202268 }
269+ return true ;
203270 }
204271
205272 private void deployProject ( ProjectBuildingRequest pbr , ProjectDeployerRequest pir , ArtifactRepository repo )
0 commit comments