From f0f73a4ead6b954491b354f2020d6690d57b5f4d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 25 Feb 2014 10:44:33 +0000 Subject: [PATCH] Current directory (lodaer.path=.) pathology workaround It turns out that loader.path=. was pathological and before this change ended up making the classpath empty (loader.path=.,lib/ would have fixed it). With this change the old behaviour is still supported, but if the only user-supplied path entry is "." (or empty) then it is now kept, and translates into the root of the current archive if running as "java -jar ...". Fixes gh-270 --- .../org/springframework/boot/cli/JarCommandIT.java | 2 +- .../boot/loader/PropertiesLauncher.java | 14 ++++++++++---- .../boot/loader/archive/ExplodedArchive.java | 6 +++--- .../boot/loader/PropertiesLauncherTests.java | 9 ++++++++- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java b/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java index 96b30958825..0c1b2bd152e 100644 --- a/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java +++ b/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java @@ -64,7 +64,7 @@ public class JarCommandIT { Invocation invocation = this.cli.invoke("jar", jar.getAbsolutePath(), "jar.groovy"); invocation.await(); - assertEquals(0, invocation.getErrorOutput().length()); + assertEquals(invocation.getErrorOutput(), 0, invocation.getErrorOutput().length()); assertTrue(jar.exists()); Process process = new JavaExecutable().processBuilder("-jar", 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 c2921e0ccfc..1d232ca99d6 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 @@ -310,12 +310,18 @@ public class PropertiesLauncher extends Launcher { List paths = new ArrayList(); for (String path : commaSeparatedPaths.split(",")) { path = cleanupPath(path); - // Empty path is always on the classpath so no need for it to be explicitly - // listed here + // Empty path (i.e. the archive itself if running from a JAR) is always added + // to the classpath so no need for it to be explicitly listed if (!(path.equals(".") || path.equals(""))) { paths.add(path); } } + if (paths.isEmpty()) { + // On the other hand, we don't want a completely empty path. If the app is + // running from an archive (java -jar) then this will make sure the archive + // itself is included at the very least. + paths.add("."); + } return paths; } @@ -453,12 +459,12 @@ public class PropertiesLauncher extends Launcher { } if (file.isDirectory()) { this.logger.info("Adding classpath entries from " + file); - Archive archive = new ExplodedArchive(file); + Archive archive = new ExplodedArchive(file, false); lib.add(archive); } Archive archive = getArchive(file); if (archive != null) { - this.logger.info("Adding classpath entries from nested " + archive.getUrl() + this.logger.info("Adding classpath entries from archive " + archive.getUrl() + root); lib.add(archive); } diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/archive/ExplodedArchive.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/archive/ExplodedArchive.java index b5b862ecb88..28644b4c63f 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/archive/ExplodedArchive.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/archive/ExplodedArchive.java @@ -85,9 +85,9 @@ public class ExplodedArchive extends Archive { this.entries.put(entry.getName(), entry); } if (file.isDirectory()) { - if (this.recursive) { - for (File child : file.listFiles()) { - if (!SKIPPED_NAMES.contains(child.getName())) { + for (File child : file.listFiles()) { + if (!SKIPPED_NAMES.contains(child.getName())) { + if (file.equals(this.root) || this.recursive) { buildEntries(child); } } 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 1e120c75605..242b04ade46 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 @@ -83,7 +83,14 @@ public class PropertiesLauncherTests { } @Test - public void testUserSpecifiedPath() throws Exception { + public void testUserSpecifiedDotPath() throws Exception { + System.setProperty("loader.path", "."); + PropertiesLauncher launcher = new PropertiesLauncher(); + assertEquals("[.]", ReflectionTestUtils.getField(launcher, "paths").toString()); + } + + @Test + public void testUserSpecifiedWildcardPath() throws Exception { System.setProperty("loader.path", "jars/*"); System.setProperty("loader.main", "demo.Application"); PropertiesLauncher launcher = new PropertiesLauncher();