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 00f7900482d..cf5dec1812c 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 @@ -96,6 +96,13 @@ public class PropertiesLauncher implements ArchiveFilter { */ public static final String CONFIG_LOCATION = "loader.config.location"; + /** + * Properties key for boolean flag (default false) which if set will cause the + * external configuration properties to be copied to System properties (assuming that + * is allowed by Java security). + */ + public static final String SET_SYSTEM_PROPERTIES = "loader.system"; + private static final List DEFAULT_PATHS = Arrays.asList("lib/"); private List paths = new ArrayList(DEFAULT_PATHS); @@ -213,10 +220,19 @@ public class PropertiesLauncher implements ArchiveFilter { this.properties.put(key, value); } } + if (SystemPropertyUtils.resolvePlaceholders( + "${" + SET_SYSTEM_PROPERTIES + ":false}").equals("true")) { + this.logger.info("Adding resolved properties to System properties"); + for (Object key : Collections.list(this.properties.propertyNames())) { + String value = this.properties.getProperty((String) key); + System.setProperty((String) key, value); + } + } } else { this.logger.info("Not found: " + config); } + } private InputStream getResource(String config) throws Exception { diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java index cd58b7094bb..058ef76b9c9 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java @@ -161,7 +161,7 @@ public abstract class SystemPropertyUtils { System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" + text + "] as system property: " + ex); } - return properties.getProperty(placeholderName); + return properties == null ? null : properties.getProperty(placeholderName); } private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) { 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 9ccd130faa5..869a86a1ed8 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 @@ -33,6 +33,7 @@ public class PropertiesLauncherTests { @After public void close() { + System.clearProperty("loader.system"); System.clearProperty("loader.home"); System.clearProperty("loader.path"); System.clearProperty("loader.main"); @@ -50,6 +51,7 @@ public class PropertiesLauncherTests { public void testUserSpecifiedMain() throws Exception { this.launcher.initialize(new File(".")); assertEquals("demo.Application", this.launcher.getMainClass(null)); + assertEquals(null, System.getProperty("loader.main")); } @Test @@ -68,4 +70,11 @@ public class PropertiesLauncherTests { assertEquals("foo.Bar", this.launcher.getMainClass(null)); } + @Test + public void testSystemPropertiesSet() throws Exception { + System.setProperty("loader.system", "true"); + this.launcher.initialize(new File(".")); + assertEquals("demo.Application", System.getProperty("loader.main")); + } + }