@@ -214,6 +214,13 @@ private void execute(MavenSession session, MojoExecution mojoExecution, Dependen
214214 doExecute (session , mojoExecution , dependencyContext );
215215 }
216216
217+ protected static class NoLock implements NoExceptionCloseable {
218+ public NoLock () {}
219+
220+ @ Override
221+ public void close () {}
222+ }
223+
217224 /**
218225 * Aggregating mojo executions (possibly) modify all MavenProjects, including those that are currently in use
219226 * by concurrently running mojo executions. To prevent race conditions, an aggregating execution will block
@@ -222,54 +229,45 @@ private void execute(MavenSession session, MojoExecution mojoExecution, Dependen
222229 * TODO: ideally, the builder should take care of the ordering in a smarter way
223230 * TODO: and concurrency issues fixed with MNG-7157
224231 */
225- private class ProjectLock implements AutoCloseable {
232+ protected class ProjectLock implements NoExceptionCloseable {
226233 final Lock acquiredAggregatorLock ;
227234 final OwnerReentrantLock acquiredProjectLock ;
228235
229236 ProjectLock (MavenSession session , MojoDescriptor mojoDescriptor ) {
230237 mojos .put (Thread .currentThread (), mojoDescriptor );
231- if (session .getRequest ().getDegreeOfConcurrency () > 1 ) {
232- boolean aggregator = mojoDescriptor .isAggregator ();
233- acquiredAggregatorLock = aggregator ? aggregatorLock .writeLock () : aggregatorLock .readLock ();
234- acquiredProjectLock = getProjectLock (session );
235- if (!acquiredAggregatorLock .tryLock ()) {
236- Thread owner = aggregatorLock .getOwner ();
237- MojoDescriptor ownerMojo = owner != null ? mojos .get (owner ) : null ;
238- String str = ownerMojo != null ? " The " + ownerMojo .getId () : "An" ;
239- String msg = str + " aggregator mojo is already being executed "
240- + "in this parallel build, those kind of mojos require exclusive access to "
241- + "reactor to prevent race conditions. This mojo execution will be blocked "
242- + "until the aggregator mojo is done." ;
243- warn (msg );
244- acquiredAggregatorLock .lock ();
245- }
246- if (!acquiredProjectLock .tryLock ()) {
247- Thread owner = acquiredProjectLock .getOwner ();
248- MojoDescriptor ownerMojo = owner != null ? mojos .get (owner ) : null ;
249- String str = ownerMojo != null ? " The " + ownerMojo .getId () : "A" ;
250- String msg = str + " mojo is already being executed "
251- + "on the project " + session .getCurrentProject ().getGroupId ()
252- + ":" + session .getCurrentProject ().getArtifactId () + ". "
253- + "This mojo execution will be blocked "
254- + "until the mojo is done." ;
255- warn (msg );
256- acquiredProjectLock .lock ();
257- }
258- } else {
259- acquiredAggregatorLock = null ;
260- acquiredProjectLock = null ;
238+ boolean aggregator = mojoDescriptor .isAggregator ();
239+ acquiredAggregatorLock = aggregator ? aggregatorLock .writeLock () : aggregatorLock .readLock ();
240+ acquiredProjectLock = getProjectLock (session );
241+ if (!acquiredAggregatorLock .tryLock ()) {
242+ Thread owner = aggregatorLock .getOwner ();
243+ MojoDescriptor ownerMojo = owner != null ? mojos .get (owner ) : null ;
244+ String str = ownerMojo != null ? " The " + ownerMojo .getId () : "An" ;
245+ String msg = str + " aggregator mojo is already being executed "
246+ + "in this parallel build, those kind of mojos require exclusive access to "
247+ + "reactor to prevent race conditions. This mojo execution will be blocked "
248+ + "until the aggregator mojo is done." ;
249+ warn (msg );
250+ acquiredAggregatorLock .lock ();
251+ }
252+ if (!acquiredProjectLock .tryLock ()) {
253+ Thread owner = acquiredProjectLock .getOwner ();
254+ MojoDescriptor ownerMojo = owner != null ? mojos .get (owner ) : null ;
255+ String str = ownerMojo != null ? " The " + ownerMojo .getId () : "A" ;
256+ String msg = str + " mojo is already being executed "
257+ + "on the project " + session .getCurrentProject ().getGroupId ()
258+ + ":" + session .getCurrentProject ().getArtifactId () + ". "
259+ + "This mojo execution will be blocked "
260+ + "until the mojo is done." ;
261+ warn (msg );
262+ acquiredProjectLock .lock ();
261263 }
262264 }
263265
264266 @ Override
265267 public void close () {
266268 // release the lock in the reverse order of the acquisition
267- if (acquiredProjectLock != null ) {
268- acquiredProjectLock .unlock ();
269- }
270- if (acquiredAggregatorLock != null ) {
271- acquiredAggregatorLock .unlock ();
272- }
269+ acquiredProjectLock .unlock ();
270+ acquiredAggregatorLock .unlock ();
273271 mojos .remove (Thread .currentThread ());
274272 }
275273
@@ -308,7 +306,7 @@ private void doExecute(MavenSession session, MojoExecution mojoExecution, Depend
308306
309307 ensureDependenciesAreResolved (mojoDescriptor , session , dependencyContext );
310308
311- try (ProjectLock lock = new ProjectLock (session , mojoDescriptor )) {
309+ try (NoExceptionCloseable lock = getProjectLock (session , mojoDescriptor )) {
312310 doExecute2 (session , mojoExecution );
313311 } finally {
314312 for (MavenProject forkedProject : forkedProjects ) {
@@ -317,6 +315,23 @@ private void doExecute(MavenSession session, MojoExecution mojoExecution, Depend
317315 }
318316 }
319317
318+ protected interface NoExceptionCloseable extends AutoCloseable {
319+ @ Override
320+ void close ();
321+ }
322+
323+ protected NoExceptionCloseable getProjectLock (MavenSession session , MojoDescriptor mojoDescriptor ) {
324+ if (useProjectLock (session )) {
325+ return new ProjectLock (session , mojoDescriptor );
326+ } else {
327+ return new NoLock ();
328+ }
329+ }
330+
331+ protected boolean useProjectLock (MavenSession session ) {
332+ return session .getRequest ().getDegreeOfConcurrency () > 1 ;
333+ }
334+
320335 private void doExecute2 (MavenSession session , MojoExecution mojoExecution ) throws LifecycleExecutionException {
321336 eventCatapult .fire (ExecutionEvent .Type .MojoStarted , session , mojoExecution );
322337 try {
0 commit comments