Skip to content

Commit 21f3285

Browse files
author
notoon
committed
Sets the java.class.path property before run
1 parent 047c71f commit 21f3285

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<groupId>org.seedstack</groupId>
2121
<artifactId>seedstack-maven-plugin</artifactId>
22-
<version>2.4.2-SNAPSHOT</version>
22+
<version>2.4.3-SNAPSHOT</version>
2323
<packaging>maven-plugin</packaging>
2424

2525
<properties>

src/license/THIRD-PARTY.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Please fill the missing licenses for dependencies :
1313
#
1414
#
15-
#Tue Apr 18 16:56:02 CEST 2017
15+
#Tue May 02 16:01:09 CEST 2017
1616
classworlds--classworlds--1.1-alpha-2=
1717
commons-collections--commons-collections--3.1=
1818
dom4j--dom4j--1.6.1=

src/main/java/org/seedstack/maven/AbstractExecutableMojo.java

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)