Skip to content

Commit 3701cce

Browse files
Dave Syerwilkinsona
authored andcommitted
Allow loader.path to refer to nested jars
Previously, each entry in loader.path could only refer to a standard jar file. Refering to such a jar would add all of the classes in the root of the jar to the class path. This commit adds support for referencing a directory within a jar file that contains one or more nested jars. For example: $ java -jar -Dloader.path='jar:file:./lib.jar/!BOOT-INF/lib' my.jar This will add all of the classes in all of that jars in the BOOT-INF/lib directory of lib.jar to the class path. See gh-8334
1 parent 605b0ae commit 3701cce

File tree

6 files changed

+143
-55
lines changed

6 files changed

+143
-55
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
loader.path=jar:file:../executable-props/target/executable-props-0.0.1.BUILD-SNAPSHOT-full.jar/!BOOT-INF/lib
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.launcher.it</groupId>
6+
<artifactId>executable-props-lib</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
</properties>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<version>3.1</version>
17+
<configuration>
18+
<source>1.6</source>
19+
<target>1.6</target>
20+
</configuration>
21+
</plugin>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-dependency-plugin</artifactId>
25+
<version>2.10</version>
26+
<executions>
27+
<execution>
28+
<id>unpack</id>
29+
<phase>prepare-package</phase>
30+
<goals>
31+
<goal>unpack</goal>
32+
</goals>
33+
<configuration>
34+
<artifactItems>
35+
<artifactItem>
36+
<groupId>@project.groupId@</groupId>
37+
<artifactId>@project.artifactId@</artifactId>
38+
<version>@project.version@</version>
39+
<type>jar</type>
40+
</artifactItem>
41+
</artifactItems>
42+
<outputDirectory>${project.build.directory}/assembly</outputDirectory>
43+
</configuration>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
<plugin>
48+
<artifactId>maven-assembly-plugin</artifactId>
49+
<version>2.4</version>
50+
<configuration>
51+
<descriptors>
52+
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
53+
</descriptors>
54+
<archive>
55+
<manifest>
56+
<mainClass>org.springframework.boot.loader.PropertiesLauncher</mainClass>
57+
</manifest>
58+
<manifestEntries>
59+
<Start-Class>org.springframework.boot.load.it.props.EmbeddedJarStarter</Start-Class>
60+
</manifestEntries>
61+
</archive>
62+
</configuration>
63+
<executions>
64+
<execution>
65+
<id>jar-with-dependencies</id>
66+
<phase>package</phase>
67+
<goals>
68+
<goal>single</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
<dependencies>
76+
<dependency>
77+
<groupId>org.springframework</groupId>
78+
<artifactId>spring-context</artifactId>
79+
<version>4.1.4.RELEASE</version>
80+
</dependency>
81+
</dependencies>
82+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<assembly
3+
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
6+
<id>full</id>
7+
<formats>
8+
<format>jar</format>
9+
</formats>
10+
<includeBaseDirectory>false</includeBaseDirectory>
11+
<dependencySets>
12+
<dependencySet>
13+
<useProjectArtifact/>
14+
<includes>
15+
<include>${project.groupId}:${project.artifactId}</include>
16+
</includes>
17+
<outputDirectory>BOOT-INF/classes</outputDirectory>
18+
<unpack>true</unpack>
19+
</dependencySet>
20+
</dependencySets>
21+
<fileSets>
22+
<fileSet>
23+
<directory>${project.build.directory}/assembly</directory>
24+
<outputDirectory>/</outputDirectory>
25+
</fileSet>
26+
</fileSets>
27+
</assembly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
message: World
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def jarfile = './target/executable-props-lib-0.0.1.BUILD-SNAPSHOT-full.jar'
2+
3+
new File("${basedir}/application.properties").delete()
4+
5+
String exec(String command) {
6+
def proc = command.execute([], basedir)
7+
proc.waitFor()
8+
proc.err.text
9+
}
10+
11+
String out = exec("java -jar ${jarfile}")
12+
assert out.contains('Hello Embedded World!'),
13+
'Using -jar my.jar should use the application.properties from the jar\n' + out
14+
15+
out = exec("java -cp ${jarfile} org.springframework.boot.loader.PropertiesLauncher")
16+
assert out.contains('Hello Embedded World!'),
17+
'Using -cp my.jar with PropertiesLauncher should use the application.properties from the jar\n' + out

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.net.HttpURLConnection;
24-
import java.net.MalformedURLException;
2524
import java.net.URL;
2625
import java.net.URLConnection;
2726
import java.util.ArrayList;
2827
import java.util.Collections;
29-
import java.util.Iterator;
3028
import java.util.List;
3129
import java.util.Properties;
3230
import java.util.jar.Manifest;
@@ -469,10 +467,10 @@ private List<Archive> getClassPathArchives(String path) throws Exception {
469467
debug("Adding classpath entries from archive " + archive.getUrl() + root);
470468
lib.add(archive);
471469
}
472-
Archive nested = getNestedArchive(root);
473-
if (nested != null) {
470+
List<Archive> nestedArchives = getNestedArchives(root);
471+
if (nestedArchives != null) {
474472
debug("Adding classpath entries from nested " + root);
475-
lib.add(nested);
473+
lib.addAll(nestedArchives);
476474
}
477475
return lib;
478476
}
@@ -490,19 +488,24 @@ private Archive getArchive(File file) throws IOException {
490488
return null;
491489
}
492490

493-
private Archive getNestedArchive(String root) throws Exception {
491+
private List<Archive> getNestedArchives(String root) throws Exception {
494492
if (root.startsWith("/")
495493
|| this.parent.getUrl().equals(this.home.toURI().toURL())) {
496494
// If home dir is same as parent archive, no need to add it twice.
497495
return null;
498496
}
499-
EntryFilter filter = new PrefixMatchingArchiveFilter(root);
500-
if (this.parent.getNestedArchives(filter).isEmpty()) {
501-
return null;
497+
Archive parent = this.parent;
498+
if (root.startsWith("jar:file:") && root.contains("!")) {
499+
int index = root.indexOf("!");
500+
String file = root.substring("jar:file:".length(), index);
501+
parent = new JarFileArchive(new File(file));
502+
root = root.substring(index + 1, root.length());
503+
while (root.startsWith("/")) {
504+
root = root.substring(1);
505+
}
502506
}
503-
// If there are more archives nested in this subdirectory (root) then create a new
504-
// virtual archive for them, and have it added to the classpath
505-
return new FilteredArchive(this.parent, filter);
507+
EntryFilter filter = new PrefixMatchingArchiveFilter(root);
508+
return parent.getNestedArchives(filter);
506509
}
507510

508511
private void addNestedEntries(List<Archive> lib) {
@@ -626,47 +629,4 @@ public boolean matches(Entry entry) {
626629

627630
}
628631

629-
/**
630-
* Decorator to apply an {@link Archive.EntryFilter} to an existing {@link Archive}.
631-
*/
632-
private static class FilteredArchive implements Archive {
633-
634-
private final Archive parent;
635-
636-
private final EntryFilter filter;
637-
638-
FilteredArchive(Archive parent, EntryFilter filter) {
639-
this.parent = parent;
640-
this.filter = filter;
641-
}
642-
643-
@Override
644-
public URL getUrl() throws MalformedURLException {
645-
return this.parent.getUrl();
646-
}
647-
648-
@Override
649-
public Manifest getManifest() throws IOException {
650-
return this.parent.getManifest();
651-
}
652-
653-
@Override
654-
public Iterator<Entry> iterator() {
655-
throw new UnsupportedOperationException();
656-
}
657-
658-
@Override
659-
public List<Archive> getNestedArchives(final EntryFilter filter)
660-
throws IOException {
661-
return this.parent.getNestedArchives(new EntryFilter() {
662-
@Override
663-
public boolean matches(Entry entry) {
664-
return FilteredArchive.this.filter.matches(entry)
665-
&& filter.matches(entry);
666-
}
667-
});
668-
}
669-
670-
}
671-
672632
}

0 commit comments

Comments
 (0)