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
This commit is contained in:
sfeldstein 2019-08-20 14:13:11 -07:00 committed by Andy Wilkinson
parent 48db35bf8d
commit b9dbfad473
2 changed files with 31 additions and 2 deletions

View File

@ -93,7 +93,7 @@ public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProce
private void addPropertySource(List<PropertySource<?>> propertySources, String fileName,
Function<File, String> 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)) {

View File

@ -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();