1919import java .util .Collection ;
2020import java .util .List ;
2121import org .apache .maven .artifact .Artifact ;
22- import org .apache .maven .execution .MavenSession ;
2322import org .apache .maven .model .Resource ;
24- import org .apache .maven .plugin .BuildPluginManager ;
2523import org .apache .maven .plugin .MojoExecutionException ;
26- import org .apache .maven .plugin .MojoFailureException ;
27- import org .apache .maven .plugins .annotations .Component ;
28- import org .apache .maven .plugins .annotations .Parameter ;
29- import org .apache .maven .project .MavenProject ;
30- import org .codehaus .plexus .util .cli .CommandLineUtils ;
3124
3225/**
3326 * Provides a common base for mojos that run SeedStack applications.
3427 */
3528public abstract class AbstractExecutableMojo extends AbstractSeedStackMojo {
36- @ Parameter (defaultValue = "${project}" , required = true , readonly = true )
37- private MavenProject project ;
38- @ Parameter (defaultValue = "${session}" , required = true , readonly = true )
39- private MavenSession mavenSession ;
40- @ Component
41- private BuildPluginManager buildPluginManager ;
42- @ Parameter (defaultValue = "${project.build.outputDirectory}" , required = true )
43- private File classesDirectory ;
44- @ Parameter (defaultValue = "${project.build.testOutputDirectory}" , required = true )
45- private File testClassesDirectory ;
46- @ Parameter (property = "args" )
47- private String args ;
4829 private final IsolatedThreadGroup isolatedThreadGroup = new IsolatedThreadGroup ("seed-app" );
49- private final Object monitor = new Object ();
5030
5131 @ SuppressFBWarnings (value = {"UW_UNCOND_WAIT" , "WA_NOT_IN_LOOP" }, justification = "Cannot know when the "
5232 + "application is started" )
53- protected void execute (Runnable runnable , boolean testMode ) throws MojoExecutionException , MojoFailureException {
33+ protected void execute (Runnable runnable , boolean testMode ) throws MojoExecutionException {
5434 File [] classPathFiles = getClassPathFiles (testMode );
5535
5636 // Set the system property for proper detection of classpath
5737 System .setProperty ("java.class.path" , buildCpProperty (classPathFiles ));
5838
5939 // Start the launcher thread
60- ClassLoader classLoader = buildClassLoader (classPathFiles );
40+ ClassLoader classLoader ;
41+ try {
42+ classLoader = buildClassLoader (classPathFiles );
43+ } catch (MalformedURLException e ) {
44+ throw new MojoExecutionException ("Unable to build the classloader" , e );
45+ }
6146
6247 // Create an isolated thread
6348 Thread bootstrapThread = new Thread (isolatedThreadGroup , runnable , "main" );
6449 bootstrapThread .setContextClassLoader (classLoader );
6550 bootstrapThread .start ();
6651
6752 // Wait for the application to launch
68- synchronized (monitor ) {
69- try {
70- monitor .wait ();
71- } catch (InterruptedException e ) {
72- throw new MojoExecutionException ("Interrupted while waiting for Seed" );
73- }
74- }
53+ getContext ().waitForStartup ();
7554
7655 // Check for any uncaught exception
7756 synchronized (isolatedThreadGroup ) {
7857 if (isolatedThreadGroup .uncaughtException != null ) {
79- throw new MojoExecutionException ("An exception occurred while executing Seed " ,
58+ throw new MojoExecutionException ("An exception occurred while executing SeedStack application " ,
8059 isolatedThreadGroup .uncaughtException );
8160 }
8261 }
8362 }
8463
85- MavenSession getMavenSession () {
86- return mavenSession ;
87- }
88-
89- BuildPluginManager getBuildPluginManager () {
90- return buildPluginManager ;
91- }
92-
93- MavenProject getProject () {
94- return project ;
95- }
96-
97- File getClassesDirectory () {
98- return classesDirectory ;
99- }
100-
101- File getTestClassesDirectory () {
102- return testClassesDirectory ;
103- }
104-
105- Object getMonitor () {
106- return monitor ;
107- }
108-
109- String [] getArgs () throws MojoExecutionException {
110- try {
111- return CommandLineUtils .translateCommandline (args );
112- } catch (Exception e ) {
113- throw new MojoExecutionException ("Failed to parse arguments" , e );
114- }
115- }
116-
117- URLClassLoader getClassLoader (final URL [] classPathUrls ) {
64+ URLClassLoader createClassLoader (final URL [] classPathUrls ) {
11865 return AccessController .doPrivileged (new PrivilegedAction <URLClassLoader >() {
11966 public URLClassLoader run () {
12067 return new URLClassLoader (classPathUrls );
12168 }
12269 });
12370 }
12471
125- private ClassLoader buildClassLoader (File [] classPathFiles ) throws MojoExecutionException {
126- URL [] classPathUrls = new URL [classPathFiles .length ];
127- for (int i = 0 ; i < classPathFiles .length ; i ++) {
128- try {
129- classPathUrls [i ] = classPathFiles [i ].toURI ().toURL ();
130- } catch (MalformedURLException e ) {
131- throw new MojoExecutionException ("Unable to create URL from " + classPathFiles [i ]);
132- }
133- }
134- return getClassLoader (classPathUrls );
135- }
136-
137- private String buildCpProperty (File [] classPathFiles ) {
138- StringBuilder stringBuilder = new StringBuilder ();
139- for (int i = 0 ; i < classPathFiles .length ; i ++) {
140- File classPathFile = classPathFiles [i ];
141- stringBuilder .append (classPathFile );
142- if (i < classPathFiles .length - 1 ) {
143- stringBuilder .append (File .pathSeparator );
144- }
145- }
146- return stringBuilder .toString ();
147- }
148-
14972 void waitForShutdown () {
15073 boolean found = true ;
15174
@@ -180,34 +103,32 @@ private Thread[] getGroupThreads(final ThreadGroup group) {
180103 return java .util .Arrays .copyOf (threads , n );
181104 }
182105
183- private File [] getClassPathFiles (boolean testMode ) throws MojoExecutionException {
106+ private File [] getClassPathFiles (boolean testMode ) {
184107 List <File > files = new ArrayList <>();
185108
186- try {
187- if (testMode ) {
188- // Project test resources
189- addResources (this .testClassesDirectory , this .project .getTestResources (), files );
109+ if (testMode ) {
110+ // Project test resources
111+ addResources (getContext ().getTestClassesDirectory (),
112+ getContext ().getMavenProject ().getTestResources (),
113+ files );
190114
191- // Project test classes
192- files .add (this . testClassesDirectory );
193- }
115+ // Project test classes
116+ files .add (getContext (). getTestClassesDirectory () );
117+ }
194118
195- // Project resources
196- addResources (this . classesDirectory , this . project .getResources (), files );
119+ // Project resources
120+ addResources (getContext (). getClassesDirectory (), getContext (). getMavenProject () .getResources (), files );
197121
198- // Project classes
199- files .add (this . classesDirectory );
122+ // Project classes
123+ files .add (getContext (). getClassesDirectory () );
200124
201- // Project dependencies (scope is dependent upon the @Mojo annotation and the already executed phase)
202- addArtifacts (this .project .getArtifacts (), files );
203- } catch (MalformedURLException e ) {
204- throw new MojoExecutionException ("Unable to build classpath" , e );
205- }
125+ // Project dependencies (scope is dependent upon the @Mojo annotation and the already executed phase)
126+ addArtifacts (getContext ().getMavenProject ().getArtifacts (), files );
206127
207- return files .toArray (new File [files . size () ]);
128+ return files .toArray (new File [0 ]);
208129 }
209130
210- private void addArtifacts (Collection <Artifact > artifacts , List <File > files ) throws MalformedURLException {
131+ private void addArtifacts (Collection <Artifact > artifacts , List <File > files ) {
211132 for (Artifact artifact : artifacts ) {
212133 File file = artifact .getFile ();
213134 if (file .getName ().endsWith (".jar" )) {
@@ -217,7 +138,7 @@ private void addArtifacts(Collection<Artifact> artifacts, List<File> files) thro
217138 }
218139
219140 private void addResources (File classesDirectory , List <Resource > resources ,
220- List <File > files ) throws MalformedURLException {
141+ List <File > files ) {
221142 for (Resource resource : resources ) {
222143 File directory = new File (resource .getDirectory ());
223144 files .add (directory );
@@ -246,6 +167,26 @@ private void removeDuplicatesFromOutputDirectory(File outputDirectory, File orig
246167 }
247168 }
248169
170+ private ClassLoader buildClassLoader (File [] classPathFiles ) throws MalformedURLException {
171+ URL [] classPathUrls = new URL [classPathFiles .length ];
172+ for (int i = 0 ; i < classPathFiles .length ; i ++) {
173+ classPathUrls [i ] = classPathFiles [i ].toURI ().toURL ();
174+ }
175+ return createClassLoader (classPathUrls );
176+ }
177+
178+ private String buildCpProperty (File [] classPathFiles ) {
179+ StringBuilder stringBuilder = new StringBuilder ();
180+ for (int i = 0 ; i < classPathFiles .length ; i ++) {
181+ File classPathFile = classPathFiles [i ];
182+ stringBuilder .append (classPathFile );
183+ if (i < classPathFiles .length - 1 ) {
184+ stringBuilder .append (File .pathSeparator );
185+ }
186+ }
187+ return stringBuilder .toString ();
188+ }
189+
249190 private class IsolatedThreadGroup extends ThreadGroup {
250191 private Throwable uncaughtException ;
251192
0 commit comments