@@ -31,25 +31,35 @@ public class AbstractExecutableMojo extends AbstractMojo {
3131
3232 @ Parameter (defaultValue = "${project}" , required = true , readonly = true )
3333 private MavenProject project ;
34+
3435 @ Parameter (defaultValue = "${project.build.outputDirectory}" , required = true )
3536 private File classesDirectory ;
37+
3638 @ Parameter (defaultValue = "${project.build.testOutputDirectory}" , required = true )
3739 private File testClassesDirectory ;
40+
3841 @ Parameter (property = "args" )
3942 private String args ;
4043
4144 private final IsolatedThreadGroup isolatedThreadGroup = new IsolatedThreadGroup ("seed-app" );
45+
4246 private final Object monitor = new Object ();
47+
4348 private boolean testMode = false ;
49+
4450 protected Runnable runnable ;
4551
4652 @ Override
4753 public void execute () throws MojoExecutionException , MojoFailureException {
4854 // Create an isolated thread
4955 Thread bootstrapThread = new Thread (isolatedThreadGroup , runnable , "main" );
56+ File [] classPathFiles = getClassPathFiles ();
57+
58+ // Set the system property for proper detection of classpath
59+ System .setProperty ("java.class.path" , buildCpProperty (classPathFiles ));
5060
5161 // Start the launcher thread
52- bootstrapThread .setContextClassLoader (new URLClassLoader ( getClassPathUrls () ));
62+ bootstrapThread .setContextClassLoader (buildClassLoader ( classPathFiles ));
5363 bootstrapThread .start ();
5464
5565 // Wait for the application to launch
@@ -72,6 +82,30 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7282 joinNonDaemonThreads (isolatedThreadGroup );
7383 }
7484
85+ private ClassLoader buildClassLoader (File [] classPathFiles ) throws MojoExecutionException {
86+ URL [] classPathUrls = new URL [classPathFiles .length ];
87+ for (int i = 0 ; i < classPathFiles .length ; i ++) {
88+ try {
89+ classPathUrls [i ] = classPathFiles [i ].toURI ().toURL ();
90+ } catch (MalformedURLException e ) {
91+ throw new MojoExecutionException ("Unable to create URL from " + classPathFiles [i ]);
92+ }
93+ }
94+ return new URLClassLoader (classPathUrls );
95+ }
96+
97+ private String buildCpProperty (File [] classPathFiles ) {
98+ StringBuilder stringBuilder = new StringBuilder ();
99+ for (int i = 0 ; i < classPathFiles .length ; i ++) {
100+ File classPathFile = classPathFiles [i ];
101+ stringBuilder .append (classPathFile );
102+ if (i < classPathFiles .length - 1 ) {
103+ stringBuilder .append (File .pathSeparator );
104+ }
105+ }
106+ return stringBuilder .toString ();
107+ }
108+
75109 protected void enableTestMode () {
76110 this .testMode = true ;
77111 }
@@ -122,46 +156,46 @@ private Thread[] getGroupThreads(final ThreadGroup group) {
122156 return java .util .Arrays .copyOf (threads , n );
123157 }
124158
125- private URL [] getClassPathUrls () throws MojoExecutionException {
126- List <URL > urls = new ArrayList <URL >();
159+ private File [] getClassPathFiles () throws MojoExecutionException {
160+ List <File > files = new ArrayList <>();
127161
128162 try {
129163 if (testMode ) {
130164 // Project test resources
131- addResources (this .testClassesDirectory , this .project .getTestResources (), urls );
165+ addResources (this .testClassesDirectory , this .project .getTestResources (), files );
132166
133167 // Project test classes
134- urls .add (this .testClassesDirectory . toURI (). toURL () );
168+ files .add (this .testClassesDirectory );
135169 }
136170
137171 // Project resources
138- addResources (this .classesDirectory , this .project .getResources (), urls );
172+ addResources (this .classesDirectory , this .project .getResources (), files );
139173
140174 // Project classes
141- urls .add (this .classesDirectory . toURI (). toURL () );
175+ files .add (this .classesDirectory );
142176
143177 // Project dependencies (scope is dependent upon the @Mojo annotation and the already executed phase)
144- addArtifacts (this .project .getArtifacts (), urls );
178+ addArtifacts (this .project .getArtifacts (), files );
145179 } catch (MalformedURLException e ) {
146180 throw new MojoExecutionException ("Unable to build classpath" , e );
147181 }
148182
149- return urls .toArray (new URL [ urls .size ()]);
183+ return files .toArray (new File [ files .size ()]);
150184 }
151185
152- private void addArtifacts (Collection <Artifact > artifacts , List <URL > urls ) throws MalformedURLException {
186+ private void addArtifacts (Collection <Artifact > artifacts , List <File > files ) throws MalformedURLException {
153187 for (Artifact artifact : artifacts ) {
154188 File file = artifact .getFile ();
155189 if (file .getName ().endsWith (".jar" )) {
156- urls .add (file . toURI (). toURL () );
190+ files .add (file );
157191 }
158192 }
159193 }
160194
161- private void addResources (File classesDirectory , List <Resource > resources , List <URL > urls ) throws MalformedURLException {
195+ private void addResources (File classesDirectory , List <Resource > resources , List <File > files ) throws MalformedURLException {
162196 for (Resource resource : resources ) {
163197 File directory = new File (resource .getDirectory ());
164- urls .add (directory . toURI (). toURL () );
198+ files .add (directory );
165199 removeDuplicatesFromOutputDirectory (classesDirectory , directory );
166200 }
167201 }
0 commit comments