Commit 689eb3e1 authored by Dave Syer's avatar Dave Syer

Add URLs from parent classloader in executable jar

$ (cd spring-boot-tools; mvn clean install -DskipTests=true)
$ (cd spring-boot-samples/spring-boot-sample-simple/; mvn clean package)
$ java -jar spring-boot-samples/spring-boot-sample-simple/target/spring-boot-sample-simple-1.1.0.BUILD-SNAPSHOT.jar

(vanilla executable jar archive: works)

$ java -cp spring-boot-samples/spring-boot-sample-simple/target/spring-boot-sample-simple-1.1.0.BUILD-SNAPSHOT.jar:spring-boot-tools/spring-boot-loader/src/test/resources/jars/app.jar org.springframework.boot.loader.JarLauncher

(jar archive plus vanilla plugin: works)

$ (cd spring-boot-samples/spring-boot-sample-simple/target; rm -rf app && mkdir $_ && cd $_ && jar -xf ../*.jar)
$ java -cp spring-boot-samples/spring-boot-sample-simple/target/app/ org.springframework.boot.loader.JarLauncher

(exploded directory: works)

$ java -cp spring-boot-tools/spring-boot-loader/s:spring-boot-tools/spring-boot-loader/src/test/resources/jars/app.jar org.springframework.boot.loader.JarLauncher

(exploded directory with plugin jar: works)

Potential fix for gh-529
parent a66fc303
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
</descriptors> </descriptors>
<archive> <archive>
<manifest> <manifest>
<mainClass>org.springframework.boot.load.JarLauncher</mainClass> <mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
</manifest> </manifest>
<manifestEntries> <manifestEntries>
<Start-Class>org.springframework.boot.load.it.jar.EmbeddedJarStarter</Start-Class> <Start-Class>org.springframework.boot.load.it.jar.EmbeddedJarStarter</Start-Class>
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
<configuration> <configuration>
<archive> <archive>
<manifest> <manifest>
<mainClass>org.springframework.boot.load.WarLauncher</mainClass> <mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
</manifest> </manifest>
<manifestEntries> <manifestEntries>
<Start-Class>org.springframework.boot.load.it.war.embedded.EmbeddedWarStarter</Start-Class> <Start-Class>org.springframework.boot.load.it.war.embedded.EmbeddedWarStarter</Start-Class>
......
...@@ -16,8 +16,12 @@ ...@@ -16,8 +16,12 @@
package org.springframework.boot.loader; package org.springframework.boot.loader;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import org.springframework.boot.loader.archive.Archive; import org.springframework.boot.loader.archive.Archive;
...@@ -64,6 +68,21 @@ public abstract class ExecutableArchiveLauncher extends Launcher { ...@@ -64,6 +68,21 @@ public abstract class ExecutableArchiveLauncher extends Launcher {
return archives; return archives;
} }
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
Set<URL> copy = new LinkedHashSet<URL>();
ClassLoader loader = getDefaultClassLoader();
if (loader instanceof URLClassLoader) {
for (URL url : ((URLClassLoader) loader).getURLs()) {
copy.add(url);
}
}
for (URL url : urls) {
copy.add(url);
}
return super.createClassLoader(copy.toArray(new URL[copy.size()]));
}
/** /**
* Determine if the specified {@link JarEntry} is a nested item that should be added * Determine if the specified {@link JarEntry} is a nested item that should be added
* to the classpath. The method is called once for each entry. * to the classpath. The method is called once for each entry.
...@@ -81,4 +100,20 @@ public abstract class ExecutableArchiveLauncher extends Launcher { ...@@ -81,4 +100,20 @@ public abstract class ExecutableArchiveLauncher extends Launcher {
protected void postProcessClassPathArchives(List<Archive> archives) throws Exception { protected void postProcessClassPathArchives(List<Archive> archives) throws Exception {
} }
private static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back to system class
// loader...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ExecutableArchiveLauncher.class.getClassLoader();
}
return cl;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment