diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java index 6ddba96106a..207827f0f9c 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java @@ -93,7 +93,7 @@ public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProce private void addPropertySource(List> propertySources, String fileName, Function propertySourceNamer) { - File home = getHomeDirectory(); + File home = getProjectRootFolder(); File file = (home != null) ? new File(home, fileName) : null; FileSystemResource resource = (file != null) ? new FileSystemResource(file) : null; if (resource != null && resource.exists() && resource.isFile()) { @@ -121,6 +121,17 @@ public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProce .anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(name, fileExtension)); } + protected File getProjectRootFolder() { + String rootFolder = System.getenv("PROJECT_ROOT_FOLDER"); + if (rootFolder == null) { + rootFolder = System.getProperty("PROJECT_ROOT_FOLDER"); + } + if (StringUtils.hasLength(rootFolder)) { + return new File(rootFolder); + } + return getHomeDirectory(); + } + protected File getHomeDirectory() { String home = System.getProperty("user.home"); if (StringUtils.hasLength(home)) { diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessorTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessorTests.java index 441d76835b4..7d86429a5a3 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessorTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessorTests.java @@ -47,9 +47,12 @@ class DevToolsHomePropertiesPostProcessorTests { private File home; + private File rootDir; + @BeforeEach - void setup(@TempDir File tempDir) { + void setup(@TempDir File tempDir, @TempDir File rootDir) { this.home = tempDir; + this.rootDir = rootDir; this.configDir = this.home + "/.config/spring-boot/"; new File(this.configDir).mkdirs(); } @@ -63,6 +66,21 @@ class DevToolsHomePropertiesPostProcessorTests { assertThat(environment.getProperty("abc")).isEqualTo("def"); } + @Test + void loadsRootFolderProperties() throws Exception { + System.setProperty("PROJECT_ROOT_FOLDER", this.rootDir.getAbsolutePath()); + Properties properties = new Properties(); + properties.put("uvw", "xyz"); + OutputStream out = new FileOutputStream( + new File(this.rootDir, ".config/spring-boot/spring-boot-devtools.properties")); + properties.store(out, null); + out.close(); + ConfigurableEnvironment environment = new MockEnvironment(); + MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor(); + runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null)); + assertThat(environment.getProperty("uvw")).isEqualTo("xyz"); + } + @Test void loadsPropertiesFromConfigDirectoryUsingProperties() throws Exception { Properties properties = new Properties();