Skip to content

Commit 168cc03

Browse files
committed
New feature: thin.libs to append to classpath
Fixes #171, #15
1 parent 27fe148 commit 168cc03

File tree

7 files changed

+150
-50
lines changed

7 files changed

+150
-50
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ You can set a variety of options on the command line or with system properties (
319319
| `thin.force` | false | Force dependency resolution to happen, even if dependencies have been computed, and marked as "computed" in `thin.properties`. |
320320
| `thin.classpath` | false | Only print the classpath. Don't run the main class. Two formats are supported: "path" and "properties". For backwards compatibility "true" or empty are equivalent to "path". |
321321
| `thin.root` | `${user.home}/.m2` | The location of the local jar cache, laid out as a maven repository. The launcher creates a new directory here called "repository" if it doesn't exist. |
322+
| `thin.libs` | `<empty>` | Additional classpath entries to append at runtime in the same form as you would use in `java -classpath ...`. If this property is defined then unresolved dependencies will be ignored when the classpath is computed, possibly leading to runtime class not found exceptions. |
322323
| `thin.archive` | the same as the target archive | The archive to launch. Can be used to launch a JAR file that was build with a different version of the thin launcher, for instance, or a fat jar built by Spring Boot without the thin launcher. |
323324
| `thin.parent` | `<empty>` | A parent archive to use for dependency management and common classpath entries. If you run two apps with the same parent, they will have a classpath that is the same, reading from left to right, until they actually differ. |
324325
| `thin.location` | `file:.,classpath:/` | The path to directory containing thin properties files (as per `thin.name`), as a comma-separated list of resource locations (directories). These locations plus the same paths relative /META-INF will be searched. |

Diff for: launcher/src/main/java/org/springframework/boot/loader/thin/ArchiveUtils.java

+31-17
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import org.eclipse.aether.artifact.DefaultArtifact;
2929
import org.eclipse.aether.graph.Dependency;
30-
3130
import org.springframework.boot.loader.archive.Archive;
3231
import org.springframework.boot.loader.archive.ExplodedArchive;
3332
import org.springframework.boot.loader.archive.JarFileArchive;
@@ -53,17 +52,15 @@ public static Archive getArchive(String path) {
5352
}
5453
try {
5554
return new JarFileArchive(new JarFile(file));
56-
}
57-
catch (IOException e) {
55+
} catch (IOException e) {
5856
throw new IllegalStateException("Cannot create JAR archive: " + file, e);
5957
}
6058
}
6159

6260
public static File getArchiveRoot(Archive archive) {
6361
try {
6462
return new File(jarFile(archive.getUrl()).toURI());
65-
}
66-
catch (Exception e) {
63+
} catch (Exception e) {
6764
throw new IllegalStateException("Cannot locate JAR archive: " + archive, e);
6865
}
6966
}
@@ -82,19 +79,16 @@ public static String findMainClass(Archive archive) {
8279
return mainClass;
8380
}
8481
}
85-
}
86-
catch (Exception e) {
82+
} catch (Exception e) {
8783
}
8884
try {
8985
File root = getArchiveRoot(archive);
9086
if (archive instanceof ExplodedArchive) {
9187
return MainClassFinder.findSingleMainClass(root);
92-
}
93-
else {
88+
} else {
9489
return MainClassFinder.findSingleMainClass(new JarFile(root), "");
9590
}
96-
}
97-
catch (Exception e) {
91+
} catch (Exception e) {
9892
throw new IllegalStateException("Cannot locate main class in " + archive, e);
9993
}
10094
}
@@ -104,8 +98,7 @@ private static URI findArchive(String path) {
10498
if (archive != null) {
10599
try {
106100
return jarFile(archive.toURL()).toURI();
107-
}
108-
catch (Exception e) {
101+
} catch (Exception e) {
109102
throw new IllegalStateException("Cannot create URI for " + archive);
110103
}
111104
}
@@ -148,8 +141,7 @@ private static URL jarFile(URL url) {
148141
}
149142
try {
150143
url = new URL(path);
151-
}
152-
catch (MalformedURLException e) {
144+
} catch (MalformedURLException e) {
153145
throw new IllegalStateException("Bad URL for jar file: " + path, e);
154146
}
155147
}
@@ -165,8 +157,7 @@ public static List<URL> nestedClasses(Archive archive, String... paths) {
165157
extras.add(classes.getURL());
166158
}
167159
}
168-
}
169-
catch (Exception e) {
160+
} catch (Exception e) {
170161
throw new IllegalStateException("Cannot create urls for resources", e);
171162
}
172163
return extras;
@@ -189,4 +180,27 @@ private static URL[] locateFiles(URL[] urls) {
189180
return urls;
190181
}
191182

183+
public static List<Archive> getArchives(String path) {
184+
List<Archive> list = new ArrayList<>();
185+
for (String element : path.split(File.pathSeparator)) {
186+
if (element.endsWith("*")) {
187+
File dir = new File(element.substring(0, element.length() - 1));
188+
if (dir.isDirectory()) {
189+
for (File file : dir.listFiles()) {
190+
if (file.getName().endsWith(".jar")) {
191+
try {
192+
list.add(getArchive(file.getCanonicalPath()));
193+
} catch (IOException e) {
194+
// ignore
195+
}
196+
}
197+
}
198+
}
199+
} else {
200+
list.add(getArchive(element));
201+
}
202+
}
203+
return list;
204+
}
205+
192206
}

Diff for: launcher/src/main/java/org/springframework/boot/loader/thin/DependencyResolver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ public List<Dependency> dependencies(final Resource resource,
214214
DependencyResolver.globals = null;
215215
DependencyResolutionResult dependencies = result
216216
.getDependencyResolutionResult();
217-
if (!dependencies.getUnresolvedDependencies().isEmpty()) {
217+
if (!dependencies.getUnresolvedDependencies().isEmpty() &&
218+
properties.getProperty(ThinJarLauncher.THIN_LIBS, "").length()==0) {
218219
StringBuilder builder = new StringBuilder();
219220
for (Dependency dependency : dependencies
220221
.getUnresolvedDependencies()) {

Diff for: launcher/src/main/java/org/springframework/boot/loader/thin/PathResolver.java

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class PathResolver {
5959

6060
private String root;
6161

62+
private String libs;
63+
6264
private Properties overrides = new Properties();
6365

6466
private boolean offline;
@@ -79,6 +81,10 @@ public void setRoot(String root) {
7981
this.root = root;
8082
}
8183

84+
public void setLibs(String libs) {
85+
this.libs = libs;
86+
}
87+
8288
public void setOverrides(Properties overrides) {
8389
this.overrides = overrides;
8490
}
@@ -274,6 +280,9 @@ private Properties getProperties(Archive archive, String name, String[] profiles
274280
if (root != null) {
275281
properties.setProperty("thin.root", root);
276282
}
283+
if (libs != null) {
284+
properties.setProperty("thin.libs", libs);
285+
}
277286
if (offline) {
278287
properties.setProperty("thin.offline", "true");
279288
}

0 commit comments

Comments
 (0)