Commit caefb234 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #8465 from Dave Syer

* gh-8334:
  Polish "Allow loader.path to refer to nested jars"
  Allow loader.path to refer to nested jars
parents 605b0aef 6673d8ee
...@@ -147,7 +147,8 @@ files in directories (as opposed to explicitly on the classpath). In the case of ...@@ -147,7 +147,8 @@ files in directories (as opposed to explicitly on the classpath). In the case of
you just add extra jars in those locations if you want more. The `PropertiesLauncher` you just add extra jars in those locations if you want more. The `PropertiesLauncher`
looks in `BOOT-INF/lib/` in your application archive by default, but you can add looks in `BOOT-INF/lib/` in your application archive by default, but you can add
additional locations by setting an environment variable `LOADER_PATH` or `loader.path` additional locations by setting an environment variable `LOADER_PATH` or `loader.path`
in `loader.properties` (comma-separated list of directories or archives). in `loader.properties` (comma-separated list of directories, archives, or directories
within archives).
...@@ -280,7 +281,8 @@ the `Main-Class` attribute and leave out `Start-Class`. ...@@ -280,7 +281,8 @@ the `Main-Class` attribute and leave out `Start-Class`.
* `loader.home` is only the directory location of an additional properties file * `loader.home` is only the directory location of an additional properties file
(overriding the default) as long as `loader.config.location` is not specified. (overriding the default) as long as `loader.config.location` is not specified.
* `loader.path` can contain directories (scanned recursively for jar and zip files), * `loader.path` can contain directories (scanned recursively for jar and zip files),
archive paths, or wildcard patterns (for the default JVM behavior). archive paths, a directory within an archive that is scanned for jar files (for
example, `dependencies.jar!/lib`), or wildcard patterns (for the default JVM behavior).
* `loader.path` (if empty) defaults to `BOOT-INF/lib` (meaning a local directory or a * `loader.path` (if empty) defaults to `BOOT-INF/lib` (meaning a local directory or a
nested one if running from an archive). Because of this `PropertiesLauncher` behaves the nested one if running from an archive). Because of this `PropertiesLauncher` behaves the
same as `JarLauncher` when no additional configuration is provided. same as `JarLauncher` when no additional configuration is provided.
......
loader.path=jar:file:target/executable-props-lib-0.0.1.BUILD-SNAPSHOT-dependencies.jar/!BOOT-INF/lib
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.launcher.it</groupId>
<artifactId>executable-props-lib</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<type>jar</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/app-assembly</outputDirectory>
</configuration>
</execution>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependencies-assembly/BOOT-INF/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>app</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/app.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>org.springframework.boot.loader.PropertiesLauncher</mainClass>
</manifest>
<manifestEntries>
<Start-Class>org.springframework.boot.launcher.it.props.EmbeddedJarStarter</Start-Class>
</manifestEntries>
</archive>
</configuration>
</execution>
<execution>
<id>depedendencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/dependencies.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>app</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact/>
<includes>
<include>${project.groupId}:${project.artifactId}</include>
</includes>
<outputDirectory>BOOT-INF/classes</outputDirectory>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/app-assembly</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/dependencies-assembly</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.launcher.it.props;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Main class to start the embedded server.
*
* @author Dave Syer
*/
public final class EmbeddedJarStarter {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
context.getBean(SpringConfiguration.class).run(args);
context.close();
}
}
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.launcher.it.props;
import java.io.IOException;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
/**
* Spring configuration.
*
* @author Dave Syer
*/
@Configuration
@ComponentScan
public class SpringConfiguration {
private String message = "Jar";
@PostConstruct
public void init() throws IOException {
Properties props = new Properties();
props.load(new ClassPathResource("application.properties").getInputStream());
String value = props.getProperty("message");
if (value!=null) {
this.message = value;
}
}
public void run(String... args) {
System.err.println("Hello Embedded " + this.message + "!");
}
}
def jarfile = './target/executable-props-lib-0.0.1.BUILD-SNAPSHOT-app.jar'
new File("${basedir}/application.properties").delete()
String exec(String command) {
def proc = command.execute([], basedir)
proc.waitFor()
proc.err.text
}
String out = exec("java -jar ${jarfile}")
assert out.contains('Hello Embedded World!'),
'Using -jar my.jar should load dependencies from separate jar and use the application.properties from the jar\n' + out
out = exec("java -cp ${jarfile} org.springframework.boot.loader.PropertiesLauncher")
assert out.contains('Hello Embedded World!'),
'Using -cp my.jar with PropertiesLauncher should load dependencies from separate jar and use the application.properties from the jar\n' + out
...@@ -21,12 +21,10 @@ import java.io.FileInputStream; ...@@ -21,12 +21,10 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.jar.Manifest; import java.util.jar.Manifest;
...@@ -469,10 +467,10 @@ public class PropertiesLauncher extends Launcher { ...@@ -469,10 +467,10 @@ public class PropertiesLauncher extends Launcher {
debug("Adding classpath entries from archive " + archive.getUrl() + root); debug("Adding classpath entries from archive " + archive.getUrl() + root);
lib.add(archive); lib.add(archive);
} }
Archive nested = getNestedArchive(root); List<Archive> nestedArchives = getNestedArchives(root);
if (nested != null) { if (nestedArchives != null) {
debug("Adding classpath entries from nested " + root); debug("Adding classpath entries from nested " + root);
lib.add(nested); lib.addAll(nestedArchives);
} }
return lib; return lib;
} }
...@@ -490,19 +488,24 @@ public class PropertiesLauncher extends Launcher { ...@@ -490,19 +488,24 @@ public class PropertiesLauncher extends Launcher {
return null; return null;
} }
private Archive getNestedArchive(String root) throws Exception { private List<Archive> getNestedArchives(String root) throws Exception {
if (root.startsWith("/") if (root.startsWith("/")
|| this.parent.getUrl().equals(this.home.toURI().toURL())) { || this.parent.getUrl().equals(this.home.toURI().toURL())) {
// If home dir is same as parent archive, no need to add it twice. // If home dir is same as parent archive, no need to add it twice.
return null; return null;
} }
EntryFilter filter = new PrefixMatchingArchiveFilter(root); Archive parent = this.parent;
if (this.parent.getNestedArchives(filter).isEmpty()) { if (root.startsWith("jar:file:") && root.contains("!")) {
return null; int index = root.indexOf("!");
String file = root.substring("jar:file:".length(), index);
parent = new JarFileArchive(new File(file));
root = root.substring(index + 1, root.length());
while (root.startsWith("/")) {
root = root.substring(1);
}
} }
// If there are more archives nested in this subdirectory (root) then create a new EntryFilter filter = new PrefixMatchingArchiveFilter(root);
// virtual archive for them, and have it added to the classpath return parent.getNestedArchives(filter);
return new FilteredArchive(this.parent, filter);
} }
private void addNestedEntries(List<Archive> lib) { private void addNestedEntries(List<Archive> lib) {
...@@ -626,47 +629,4 @@ public class PropertiesLauncher extends Launcher { ...@@ -626,47 +629,4 @@ public class PropertiesLauncher extends Launcher {
} }
/**
* Decorator to apply an {@link Archive.EntryFilter} to an existing {@link Archive}.
*/
private static class FilteredArchive implements Archive {
private final Archive parent;
private final EntryFilter filter;
FilteredArchive(Archive parent, EntryFilter filter) {
this.parent = parent;
this.filter = filter;
}
@Override
public URL getUrl() throws MalformedURLException {
return this.parent.getUrl();
}
@Override
public Manifest getManifest() throws IOException {
return this.parent.getManifest();
}
@Override
public Iterator<Entry> iterator() {
throw new UnsupportedOperationException();
}
@Override
public List<Archive> getNestedArchives(final EntryFilter filter)
throws IOException {
return this.parent.getNestedArchives(new EntryFilter() {
@Override
public boolean matches(Entry entry) {
return FilteredArchive.this.filter.matches(entry)
&& filter.matches(entry);
}
});
}
}
} }
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