Skip to content

Commit b9dbfad

Browse files
sfeldstein-vmwarewilkinsona
authored andcommitted
Make dev tools' home directory configurable
This allows separate projects to keep their own settings where common settings such as spring.* or server.* don't conflict. See gh-17924
1 parent 48db35b commit b9dbfad

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private String getPropertySourceName(File file) {
9393

9494
private void addPropertySource(List<PropertySource<?>> propertySources, String fileName,
9595
Function<File, String> propertySourceNamer) {
96-
File home = getHomeDirectory();
96+
File home = getProjectRootFolder();
9797
File file = (home != null) ? new File(home, fileName) : null;
9898
FileSystemResource resource = (file != null) ? new FileSystemResource(file) : null;
9999
if (resource != null && resource.exists() && resource.isFile()) {
@@ -121,6 +121,17 @@ private boolean canLoadFileExtension(PropertySourceLoader loader, String name) {
121121
.anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(name, fileExtension));
122122
}
123123

124+
protected File getProjectRootFolder() {
125+
String rootFolder = System.getenv("PROJECT_ROOT_FOLDER");
126+
if (rootFolder == null) {
127+
rootFolder = System.getProperty("PROJECT_ROOT_FOLDER");
128+
}
129+
if (StringUtils.hasLength(rootFolder)) {
130+
return new File(rootFolder);
131+
}
132+
return getHomeDirectory();
133+
}
134+
124135
protected File getHomeDirectory() {
125136
String home = System.getProperty("user.home");
126137
if (StringUtils.hasLength(home)) {

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessorTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ class DevToolsHomePropertiesPostProcessorTests {
4747

4848
private File home;
4949

50+
private File rootDir;
51+
5052
@BeforeEach
51-
void setup(@TempDir File tempDir) {
53+
void setup(@TempDir File tempDir, @TempDir File rootDir) {
5254
this.home = tempDir;
55+
this.rootDir = rootDir;
5356
this.configDir = this.home + "/.config/spring-boot/";
5457
new File(this.configDir).mkdirs();
5558
}
@@ -63,6 +66,21 @@ void loadsPropertiesFromHomeDirectoryUsingProperties() throws Exception {
6366
assertThat(environment.getProperty("abc")).isEqualTo("def");
6467
}
6568

69+
@Test
70+
void loadsRootFolderProperties() throws Exception {
71+
System.setProperty("PROJECT_ROOT_FOLDER", this.rootDir.getAbsolutePath());
72+
Properties properties = new Properties();
73+
properties.put("uvw", "xyz");
74+
OutputStream out = new FileOutputStream(
75+
new File(this.rootDir, ".config/spring-boot/spring-boot-devtools.properties"));
76+
properties.store(out, null);
77+
out.close();
78+
ConfigurableEnvironment environment = new MockEnvironment();
79+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
80+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
81+
assertThat(environment.getProperty("uvw")).isEqualTo("xyz");
82+
}
83+
6684
@Test
6785
void loadsPropertiesFromConfigDirectoryUsingProperties() throws Exception {
6886
Properties properties = new Properties();

0 commit comments

Comments
 (0)