From 8e84151f8f951c2fc61d51152522f74c6fab743b Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 19 Aug 2014 14:14:01 +0100 Subject: [PATCH] Normalize search path in PropertiesLauncher * Windows: allow absolute file paths without file:/// prefix * All: only add nested archives (not directories), so loader.path=lib/* behaves the same as -classpath=lib/* (except for adding zip files) Fixes gh-1352 --- .../boot/loader/PropertiesLauncher.java | 20 +++++++++++-------- .../boot/loader/PropertiesLauncherTests.java | 11 ++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java index e79e33e204..12044c4c0f 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java @@ -456,7 +456,7 @@ public class PropertiesLauncher extends Launcher { String root = cleanupPath(stripFileUrlPrefix(path)); List lib = new ArrayList(); File file = new File(root); - if (!root.startsWith("/")) { + if (!isAbsolutePath(root)) { file = new File(this.home, root); } if (file.isDirectory()) { @@ -479,6 +479,11 @@ public class PropertiesLauncher extends Launcher { return lib; } + private boolean isAbsolutePath(String root) { + // Windows contains ":" others start with "/" + return root.contains(":") || root.startsWith("/"); + } + private Archive getArchive(File file) throws IOException { String name = file.getName().toLowerCase(); if (name.endsWith(".jar") || name.endsWith(".zip")) { @@ -534,6 +539,10 @@ public class PropertiesLauncher extends Launcher { private String cleanupPath(String path) { path = path.trim(); + // No need for current dir path + if (path.startsWith("./")) { + path = path.substring(2); + } if (path.toLowerCase().endsWith(".jar") || path.toLowerCase().endsWith(".zip")) { return path; } @@ -542,14 +551,10 @@ public class PropertiesLauncher extends Launcher { } else { // It's a directory - if (!path.endsWith("/")) { + if (!path.endsWith("/") && !path.equals(".")) { path = path + "/"; } } - // No need for current dir path - if (path.startsWith("./")) { - path = path.substring(2); - } return path; } @@ -593,8 +598,7 @@ public class PropertiesLauncher extends Launcher { @Override public boolean matches(Entry entry) { - return entry.isDirectory() || entry.getName().endsWith(DOT_JAR) - || entry.getName().endsWith(DOT_ZIP); + return entry.getName().endsWith(DOT_JAR) || entry.getName().endsWith(DOT_ZIP); } } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java index 3491aa21ce..72b79e24ab 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java @@ -112,6 +112,17 @@ public class PropertiesLauncherTests { waitFor("Hello World"); } + @Test + public void testUserSpecifiedJarPathWithDot() throws Exception { + System.setProperty("loader.path", "./jars/app.jar"); + System.setProperty("loader.main", "demo.Application"); + PropertiesLauncher launcher = new PropertiesLauncher(); + assertEquals("[jars/app.jar]", ReflectionTestUtils.getField(launcher, "paths") + .toString()); + launcher.launch(new String[0]); + waitFor("Hello World"); + } + @Test public void testUserSpecifiedClassLoader() throws Exception { System.setProperty("loader.path", "jars/app.jar");