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