Skip to content

Commit 00a3ad0

Browse files
htztomicmbhave
authored andcommitted
Added support for devtools YAML configuration
See gh-17915
1 parent b43827d commit 00a3ad0

File tree

2 files changed

+164
-13
lines changed

2 files changed

+164
-13
lines changed

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.util.Properties;
22+
import java.util.logging.Logger;
2223

2324
import org.springframework.boot.SpringApplication;
2425
import org.springframework.boot.devtools.DevToolsEnablementDeducer;
@@ -35,30 +36,48 @@
3536
*
3637
* @author Phillip Webb
3738
* @author Andy Wilkinson
39+
* @author HaiTao Zhang
3840
* @since 1.3.0
3941
*/
4042
public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProcessor {
4143

42-
private static final String FILE_NAME = ".spring-boot-devtools.properties";
44+
private static final String[] FILE_NAMES = new String[] { ".spring-boot-devtools.yml", ".spring-boot-devtools.yaml",
45+
".spring-boot-devtools.properties" };
46+
47+
private Logger logger = Logger.getLogger(getClass().getName());
4348

4449
@Override
4550
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
4651
if (DevToolsEnablementDeducer.shouldEnable(Thread.currentThread())) {
4752
File home = getHomeFolder();
48-
File propertyFile = (home != null) ? new File(home, FILE_NAME) : null;
53+
Properties properties = processDir(home, "/.config/spring-boot/", environment);
54+
if (properties.isEmpty()) {
55+
processDir(home, "", environment);
56+
}
57+
}
58+
}
59+
60+
private Properties processDir(File home, String configPath, ConfigurableEnvironment environment) {
61+
Properties properties = new Properties();
62+
for (String fileName : FILE_NAMES) {
63+
File propertyFile = (home != null) ? new File(home, configPath + fileName) : null;
4964
if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
50-
FileSystemResource resource = new FileSystemResource(propertyFile);
51-
Properties properties;
52-
try {
53-
properties = PropertiesLoaderUtils.loadProperties(resource);
54-
environment.getPropertySources()
55-
.addFirst(new PropertiesPropertySource("devtools-local", properties));
56-
}
57-
catch (IOException ex) {
58-
throw new IllegalStateException("Unable to load " + FILE_NAME, ex);
59-
}
65+
addProperty(propertyFile, environment, fileName, properties);
6066
}
6167
}
68+
return properties;
69+
}
70+
71+
private void addProperty(File propertyFile, ConfigurableEnvironment environment, String fileName,
72+
Properties properties) {
73+
FileSystemResource resource = new FileSystemResource(propertyFile);
74+
try {
75+
PropertiesLoaderUtils.fillProperties(properties, resource);
76+
environment.getPropertySources().addFirst(new PropertiesPropertySource("devtools-local", properties));
77+
}
78+
catch (IOException ex) {
79+
throw new IllegalStateException("Unable to load " + fileName, ex);
80+
}
6281
}
6382

6483
protected File getHomeFolder() {

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

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Phillip Webb
3838
* @author Andy Wilkinson
39+
* @author HaiTao Zhang
3940
*/
4041
class DevToolsHomePropertiesPostProcessorTests {
4142

@@ -47,7 +48,7 @@ void setup(@TempDir File tempDir) throws IOException {
4748
}
4849

4950
@Test
50-
void loadsHomeProperties() throws Exception {
51+
void loadsPropertiesFromHomeFolderUsingProperties() throws Exception {
5152
Properties properties = new Properties();
5253
properties.put("abc", "def");
5354
OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.properties"));
@@ -59,6 +60,137 @@ void loadsHomeProperties() throws Exception {
5960
assertThat(environment.getProperty("abc")).isEqualTo("def");
6061
}
6162

63+
@Test
64+
void loadsPropertiesFromHomeFolderUsingYml() throws Exception {
65+
Properties properties = new Properties();
66+
properties.put("abc", "def");
67+
OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.yml"));
68+
properties.store(out, null);
69+
out.close();
70+
ConfigurableEnvironment environment = new MockEnvironment();
71+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
72+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
73+
assertThat(environment.getProperty("abc")).isEqualTo("def");
74+
}
75+
76+
@Test
77+
void loadsPropertiesFromHomeFolderUsingYaml() throws Exception {
78+
Properties properties = new Properties();
79+
properties.put("abc", "def");
80+
OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.yaml"));
81+
properties.store(out, null);
82+
out.close();
83+
ConfigurableEnvironment environment = new MockEnvironment();
84+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
85+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
86+
assertThat(environment.getProperty("abc")).isEqualTo("def");
87+
}
88+
89+
@Test
90+
void loadsPropertiesFromConfigFolderUsingProperties() throws Exception {
91+
Properties properties = new Properties();
92+
new File(this.home + "/.config/spring-boot").mkdirs();
93+
properties.put("abc", "def");
94+
OutputStream out = new FileOutputStream(
95+
new File(this.home + "/.config/spring-boot", ".spring-boot-devtools.properties"));
96+
properties.store(out, null);
97+
out.close();
98+
ConfigurableEnvironment environment = new MockEnvironment();
99+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
100+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
101+
assertThat(environment.getProperty("abc")).isEqualTo("def");
102+
}
103+
104+
@Test
105+
void loadsPropertiesFromConfigFolderUsingYml() throws Exception {
106+
Properties properties = new Properties();
107+
new File(this.home + "/.config/spring-boot").mkdirs();
108+
properties.put("abc", "def");
109+
OutputStream out = new FileOutputStream(
110+
new File(this.home + "/.config/spring-boot", ".spring-boot-devtools.yml"));
111+
properties.store(out, null);
112+
out.close();
113+
ConfigurableEnvironment environment = new MockEnvironment();
114+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
115+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
116+
assertThat(environment.getProperty("abc")).isEqualTo("def");
117+
}
118+
119+
@Test
120+
void loadsPropertiesFromConfigFolderUsingYaml() throws Exception {
121+
Properties properties = new Properties();
122+
new File(this.home + "/.config/spring-boot").mkdirs();
123+
properties.put("abc", "def");
124+
OutputStream out = new FileOutputStream(
125+
new File(this.home + "/.config/spring-boot", ".spring-boot-devtools.yaml"));
126+
properties.store(out, null);
127+
out.close();
128+
ConfigurableEnvironment environment = new MockEnvironment();
129+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
130+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
131+
assertThat(environment.getProperty("abc")).isEqualTo("def");
132+
}
133+
134+
@Test
135+
void loadFromConfigFolderWithPropertiesTakingPrecedence() throws Exception {
136+
Properties properties = new Properties();
137+
properties.put("abc", "def");
138+
new File(this.home + "/.config/spring-boot").mkdirs();
139+
OutputStream out = new FileOutputStream(
140+
new File(this.home + "/.config/spring-boot/", ".spring-boot-devtools.yaml"));
141+
properties.store(out, null);
142+
out.close();
143+
Properties properties2 = new Properties();
144+
properties2.put("abc", "jkl");
145+
OutputStream out2 = new FileOutputStream(
146+
new File(this.home + "/.config/spring-boot/", ".spring-boot-devtools.properties"));
147+
properties2.store(out2, null);
148+
out2.close();
149+
ConfigurableEnvironment environment = new MockEnvironment();
150+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
151+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
152+
assertThat(environment.getProperty("abc")).isEqualTo("jkl");
153+
}
154+
155+
@Test
156+
void loadFromHomeFolderWithPropertiesTakingPrecedence() throws Exception {
157+
Properties properties = new Properties();
158+
properties.put("abc", "def");
159+
new File(this.home + "/.config/spring-boot").mkdirs();
160+
OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.yaml"));
161+
properties.store(out, null);
162+
out.close();
163+
Properties properties2 = new Properties();
164+
properties2.put("abc", "jkl");
165+
OutputStream out2 = new FileOutputStream(new File(this.home, ".spring-boot-devtools.properties"));
166+
properties2.store(out2, null);
167+
out2.close();
168+
ConfigurableEnvironment environment = new MockEnvironment();
169+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
170+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
171+
assertThat(environment.getProperty("abc")).isEqualTo("jkl");
172+
}
173+
174+
@Test
175+
void loadFromConfigFolderTakesPrecedenceOverHomeFolder() throws Exception {
176+
Properties properties = new Properties();
177+
properties.put("abc", "def");
178+
new File(this.home + "/.config/spring-boot").mkdirs();
179+
OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.properties"));
180+
properties.store(out, null);
181+
out.close();
182+
Properties properties2 = new Properties();
183+
properties2.put("abc", "jkl");
184+
OutputStream out2 = new FileOutputStream(
185+
new File(this.home + "/.config/spring-boot/", ".spring-boot-devtools.properties"));
186+
properties2.store(out2, null);
187+
out2.close();
188+
ConfigurableEnvironment environment = new MockEnvironment();
189+
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
190+
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
191+
assertThat(environment.getProperty("abc")).isEqualTo("jkl");
192+
}
193+
62194
@Test
63195
void ignoresMissingHomeProperties() throws Exception {
64196
ConfigurableEnvironment environment = new MockEnvironment();

0 commit comments

Comments
 (0)