Skip to content

Commit 4e907f1

Browse files
author
Dave Syer
committed
Carefully add nested archives from JAR in PropertiesLauncher
If user runs an executable archive then it and its lib directory will be on the classpath. Entries from loader.path take precedence in a way that should make sense to users (earlier wins like in CLASSPATH env var). Also added new integration tests to verify the behaviour (big improvement on the old ones, which probably aought to be beefed up to the same standard). Fixes gh-2314
1 parent c857f95 commit 4e907f1

File tree

9 files changed

+304
-16
lines changed

9 files changed

+304
-16
lines changed

spring-boot-docs/src/main/asciidoc/appendix-executable-jar-format.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Their purpose is to load resources (`.class` files etc.) from nested jar files o
142142
files in directories (as opposed to explicitly on the classpath). In the case of the
143143
`[Jar|War]Launcher` the nested paths are fixed (`+lib/*.jar+` and `+lib-provided/*.jar+` for
144144
the war case) so you just add extra jars in those locations if you want more. The
145-
`PropertiesLauncher` looks in `lib/` by default, but you can add additional locations by
145+
`PropertiesLauncher` looks in `lib/` in your application archive by default, but you can add additional locations by
146146
setting an environment variable `LOADER_PATH` or `loader.path` in `application.properties`
147147
(comma-separated list of directories or archives).
148148

spring-boot-tools/spring-boot-loader/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<artifactId>logback-classic</artifactId>
3131
<scope>test</scope>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-webmvc</artifactId>
36+
<scope>test</scope>
37+
</dependency>
3338
<!-- Used to provide a signed jar -->
3439
<dependency>
3540
<groupId>org.bouncycastle</groupId>
@@ -57,6 +62,7 @@
5762
<addTestClassPath>true</addTestClassPath>
5863
<skipInvocation>${skipTests}</skipInvocation>
5964
<streamLogs>true</streamLogs>
65+
<parallelThreads>4</parallelThreads>
6066
</configuration>
6167
<executions>
6268
<execution>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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</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.9</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+
<execution>
46+
<id>copy</id>
47+
<phase>prepare-package</phase>
48+
<goals>
49+
<goal>copy-dependencies</goal>
50+
</goals>
51+
<configuration>
52+
<outputDirectory>${project.build.directory}/assembly/lib</outputDirectory>
53+
</configuration>
54+
</execution>
55+
</executions>
56+
</plugin>
57+
<plugin>
58+
<artifactId>maven-assembly-plugin</artifactId>
59+
<version>2.4</version>
60+
<configuration>
61+
<descriptors>
62+
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
63+
</descriptors>
64+
<archive>
65+
<manifest>
66+
<mainClass>org.springframework.boot.loader.PropertiesLauncher</mainClass>
67+
</manifest>
68+
<manifestEntries>
69+
<Start-Class>org.springframework.boot.load.it.props.EmbeddedJarStarter</Start-Class>
70+
</manifestEntries>
71+
</archive>
72+
</configuration>
73+
<executions>
74+
<execution>
75+
<id>jar-with-dependencies</id>
76+
<phase>package</phase>
77+
<goals>
78+
<goal>single</goal>
79+
</goals>
80+
</execution>
81+
</executions>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
<dependencies>
86+
<dependency>
87+
<groupId>org.springframework</groupId>
88+
<artifactId>spring-context</artifactId>
89+
<version>4.1.4.RELEASE</version>
90+
</dependency>
91+
</dependencies>
92+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
<unpack>true</unpack>
18+
</dependencySet>
19+
</dependencySets>
20+
<fileSets>
21+
<fileSet>
22+
<directory>${project.build.directory}/assembly</directory>
23+
<outputDirectory>/</outputDirectory>
24+
</fileSet>
25+
</fileSets>
26+
</assembly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.load.it.props;
18+
19+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
20+
21+
/**
22+
* Main class to start the embedded server.
23+
*
24+
* @author Phillip Webb
25+
*/
26+
public final class EmbeddedJarStarter {
27+
28+
public static void main(String[] args) throws Exception {
29+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
30+
context.getBean(SpringConfiguration.class).run(args);
31+
context.close();
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.load.it.props;
18+
19+
import java.io.IOException;
20+
21+
import javax.annotation.PostConstruct;
22+
23+
import org.springframework.context.annotation.ComponentScan;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.core.io.support.PropertiesLoaderUtils;
26+
27+
/**
28+
* Spring configuration.
29+
*
30+
* @author Phillip Webb
31+
*/
32+
@Configuration
33+
@ComponentScan
34+
public class SpringConfiguration {
35+
36+
private String message = "Jar";
37+
38+
@PostConstruct
39+
public void init() throws IOException {
40+
String value = PropertiesLoaderUtils.loadAllProperties("application.properties").getProperty("message");
41+
if (value!=null) {
42+
this.message = value;
43+
}
44+
45+
}
46+
47+
public void run(String... args) {
48+
System.err.println("Hello Embedded " + this.message + "!");
49+
}
50+
51+
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
message: World
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def jarfile = './target/executable-props-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
18+
19+
new File("${basedir}/application.properties").withWriter { it -> it << "message: Foo" }
20+
out = exec("java -jar ${jarfile}")
21+
assert out.contains('Hello Embedded World!'),
22+
'Should use the application.properties from the jar in preference to local filesystem\n' + out
23+
24+
out = exec("java -Dloader.path=.,lib -jar ${jarfile}")
25+
assert out.contains('Hello Embedded Foo!'),
26+
'With loader.path=.,lib should use the application.properties from the local filesystem\n' + out
27+
28+
new File("${basedir}/target/application.properties").withWriter { it -> it << "message: Spam" }
29+
30+
out = exec("java -Dloader.path=target,.,lib -jar ${jarfile}")
31+
assert out.contains('Hello Embedded Spam!'),
32+
'With loader.path=target,.,lib should use the application.properties from the target directory\n' + out

0 commit comments

Comments
 (0)