29
29
import org .apache .maven .plugins .annotations .Mojo ;
30
30
import org .apache .maven .plugins .annotations .Parameter ;
31
31
import org .apache .maven .plugins .annotations .ResolutionScope ;
32
- import org .apache .maven .project .ProjectBuilder ;
33
32
import org .eclipse .aether .RepositorySystem ;
34
33
import org .eclipse .aether .collection .CollectRequest ;
35
34
import org .eclipse .aether .graph .Dependency ;
@@ -55,12 +54,6 @@ public class ExecJavaMojo extends AbstractExecMojo {
55
54
@ Component
56
55
private RepositorySystem repositorySystem ;
57
56
58
- /**
59
- * @since 1.0
60
- */
61
- @ Component
62
- private ProjectBuilder projectBuilder ;
63
-
64
57
/**
65
58
* The main class to execute.<br>
66
59
* With Java 9 and above you can prefix it with the modulename, e.g. <code>com.greetings/com.greetings.Main</code>
@@ -255,70 +248,63 @@ public void execute() throws MojoExecutionException, MojoFailureException {
255
248
}
256
249
257
250
IsolatedThreadGroup threadGroup = new IsolatedThreadGroup (mainClass /* name */ );
251
+ // TODO:
252
+ // Adjust implementation for future JDKs after removal of SecurityManager.
253
+ // See https://openjdk.org/jeps/411 for basic information.
254
+ // See https://bugs.openjdk.org/browse/JDK-8199704 for details about how users might be able to
255
+ // block
256
+ // System::exit in post-removal JDKs (still undecided at the time of writing this comment).
258
257
Thread bootstrapThread = new Thread (
259
258
threadGroup ,
260
- new Runnable () {
261
- // TODO:
262
- // Adjust implementation for future JDKs after removal of SecurityManager.
263
- // See https://openjdk.org/jeps/411 for basic information.
264
- // See https://bugs.openjdk.org/browse/JDK-8199704 for details about how users might be able to
265
- // block
266
- // System::exit in post-removal JDKs (still undecided at the time of writing this comment).
267
- @ SuppressWarnings ("removal" )
268
- public void run () {
269
- int sepIndex = mainClass .indexOf ('/' );
270
-
271
- final String bootClassName ;
272
- if (sepIndex >= 0 ) {
273
- bootClassName = mainClass .substring (sepIndex + 1 );
274
- } else {
275
- bootClassName = mainClass ;
276
- }
259
+ () -> {
260
+ int sepIndex = mainClass .indexOf ('/' );
261
+
262
+ final String bootClassName ;
263
+ if (sepIndex >= 0 ) {
264
+ bootClassName = mainClass .substring (sepIndex + 1 );
265
+ } else {
266
+ bootClassName = mainClass ;
267
+ }
277
268
278
- SecurityManager originalSecurityManager = System .getSecurityManager ();
279
-
280
- try {
281
- Class <?> bootClass = Thread .currentThread ()
282
- .getContextClassLoader ()
283
- .loadClass (bootClassName );
284
-
285
- MethodHandles .Lookup lookup = MethodHandles .lookup ();
286
-
287
- MethodHandle mainHandle = lookup .findStatic (
288
- bootClass , "main" , MethodType .methodType (void .class , String [].class ));
289
-
290
- if (blockSystemExit ) {
291
- System .setSecurityManager (new SystemExitManager (originalSecurityManager ));
292
- }
293
- mainHandle .invoke (arguments );
294
- } catch (IllegalAccessException
295
- | NoSuchMethodException
296
- | NoSuchMethodError e ) { // just pass it on
297
- Thread .currentThread ()
298
- .getThreadGroup ()
299
- .uncaughtException (
300
- Thread .currentThread (),
301
- new Exception (
302
- "The specified mainClass doesn't contain a main method with appropriate signature." ,
303
- e ));
304
- } catch (
305
- InvocationTargetException
306
- e ) { // use the cause if available to improve the plugin execution output
307
- Throwable exceptionToReport = e .getCause () != null ? e .getCause () : e ;
308
- Thread .currentThread ()
309
- .getThreadGroup ()
310
- .uncaughtException (Thread .currentThread (), exceptionToReport );
311
- } catch (SystemExitException systemExitException ) {
312
- getLog ().info (systemExitException .getMessage ());
313
- if (systemExitException .getExitCode () != 0 ) {
314
- throw systemExitException ;
315
- }
316
- } catch (Throwable e ) { // just pass it on
317
- Thread .currentThread ().getThreadGroup ().uncaughtException (Thread .currentThread (), e );
318
- } finally {
319
- if (blockSystemExit ) {
320
- System .setSecurityManager (originalSecurityManager );
321
- }
269
+ SecurityManager originalSecurityManager = System .getSecurityManager ();
270
+
271
+ try {
272
+ Class <?> bootClass =
273
+ Thread .currentThread ().getContextClassLoader ().loadClass (bootClassName );
274
+
275
+ MethodHandles .Lookup lookup = MethodHandles .lookup ();
276
+
277
+ MethodHandle mainHandle =
278
+ lookup .findStatic (bootClass , "main" , MethodType .methodType (void .class , String [].class ));
279
+
280
+ if (blockSystemExit ) {
281
+ System .setSecurityManager (new SystemExitManager (originalSecurityManager ));
282
+ }
283
+ mainHandle .invoke (arguments );
284
+ } catch (IllegalAccessException | NoSuchMethodException | NoSuchMethodError e ) { // just pass it on
285
+ Thread .currentThread ()
286
+ .getThreadGroup ()
287
+ .uncaughtException (
288
+ Thread .currentThread (),
289
+ new Exception (
290
+ "The specified mainClass doesn't contain a main method with appropriate signature." ,
291
+ e ));
292
+ } catch (InvocationTargetException e ) {
293
+ // use the cause if available to improve the plugin execution output
294
+ Throwable exceptionToReport = e .getCause () != null ? e .getCause () : e ;
295
+ Thread .currentThread ()
296
+ .getThreadGroup ()
297
+ .uncaughtException (Thread .currentThread (), exceptionToReport );
298
+ } catch (SystemExitException systemExitException ) {
299
+ getLog ().info (systemExitException .getMessage ());
300
+ if (systemExitException .getExitCode () != 0 ) {
301
+ throw systemExitException ;
302
+ }
303
+ } catch (Throwable e ) { // just pass it on
304
+ Thread .currentThread ().getThreadGroup ().uncaughtException (Thread .currentThread (), e );
305
+ } finally {
306
+ if (blockSystemExit ) {
307
+ System .setSecurityManager (originalSecurityManager );
322
308
}
323
309
}
324
310
},
@@ -452,7 +438,7 @@ private void joinThread(Thread thread, long timeoutMsecs) {
452
438
453
439
private void terminateThreads (ThreadGroup threadGroup ) {
454
440
long startTime = System .currentTimeMillis ();
455
- Set <Thread > uncooperativeThreads = new HashSet <Thread >(); // these were not responsive to interruption
441
+ Set <Thread > uncooperativeThreads = new HashSet <>(); // these were not responsive to interruption
456
442
for (Collection <Thread > threads = getActiveThreads (threadGroup );
457
443
!threads .isEmpty ();
458
444
threads = getActiveThreads (threadGroup ), threads .removeAll (uncooperativeThreads )) {
@@ -512,7 +498,7 @@ private void terminateThreads(ThreadGroup threadGroup) {
512
498
private Collection <Thread > getActiveThreads (ThreadGroup threadGroup ) {
513
499
Thread [] threads = new Thread [threadGroup .activeCount ()];
514
500
int numThreads = threadGroup .enumerate (threads );
515
- Collection <Thread > result = new ArrayList <Thread >(numThreads );
501
+ Collection <Thread > result = new ArrayList <>(numThreads );
516
502
for (int i = 0 ; i < threads .length && threads [i ] != null ; i ++) {
517
503
result .add (threads [i ]);
518
504
}
0 commit comments