Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit 7a619b9

Browse files
committed
Refactor:
* added spring boot dependency to parse the spring boot properties * using spring boot run to make it compute env and retrieved props from it
1 parent ccf7b8c commit 7a619b9

File tree

10 files changed

+434
-220
lines changed

10 files changed

+434
-220
lines changed

core/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
<artifactId>mockwebserver</artifactId>
8585
</dependency>
8686

87+
<!-- Spring Boot Profiles handling -->
88+
<dependency>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot</artifactId>
91+
<version>${version.spring-boot}</version>
92+
</dependency>
93+
8794
</dependencies>
8895

8996
<build>

core/src/main/java/io/fabric8/maven/core/util/SpringBootUtil.java

Lines changed: 204 additions & 159 deletions
Large diffs are not rendered by default.

core/src/test/java/io/fabric8/maven/core/util/SpringBootUtilTest.java

Lines changed: 173 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,50 @@
1515
*/
1616
package io.fabric8.maven.core.util;
1717

18-
import java.util.*;
19-
2018
import io.fabric8.utils.PropertiesHelper;
21-
19+
import org.apache.maven.model.Build;
20+
import org.apache.maven.project.MavenProject;
21+
import org.codehaus.plexus.util.FileUtils;
2222
import org.junit.Test;
23-
24-
import static org.junit.Assert.assertEquals;
25-
import static org.junit.Assert.assertNotEquals;
26-
import static org.junit.Assert.assertNotNull;
23+
import org.springframework.boot.Banner;
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.builder.SpringApplicationBuilder;
26+
import org.springframework.context.ConfigurableApplicationContext;
27+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
28+
import org.springframework.core.env.MapPropertySource;
29+
import org.springframework.core.env.PropertySource;
30+
import org.springframework.util.ResourceUtils;
31+
32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.net.URL;
35+
import java.nio.file.Files;
36+
import java.util.Collections;
37+
import java.util.Properties;
38+
import java.util.UUID;
39+
40+
import static org.junit.Assert.*;
2741

2842
/**
2943
* Checking the behaviour of utility methods.
3044
*/
3145
public class SpringBootUtilTest {
3246

33-
3447
@Test
35-
public void testYamlToPropertiesParsing() {
48+
public void testYamlToPropertiesParsing() throws Exception {
49+
50+
MavenProject project = new MavenProject();
51+
Build build = new Build();
52+
53+
setMavenProject(project, build);
54+
55+
URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application.yml");
56+
57+
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
58+
"application.yml"));
59+
60+
Properties props = SpringBootUtil.getApplicationProperties(project, Collections.<String>emptyList());
3661

37-
Properties props = SpringBootUtil.getPropertiesFromYamlResource(
38-
SpringBootUtilTest.class.getResource("/util/test-application.yml"), Collections.<String>emptyList());
3962
assertNotEquals(0, props.size());
4063

4164
assertEquals(new Integer(8081), PropertiesHelper.getInteger(props, "management.port"));
@@ -48,55 +71,154 @@ public void testYamlToPropertiesParsing() {
4871
}
4972

5073
@Test
51-
public void testYamlToPropertiesParsingWithActiveProfiles() {
74+
public void testYamlToPropertiesMerge() throws Exception {
75+
76+
MavenProject project = new MavenProject();
77+
Build build = new Build();
78+
79+
setMavenProject(project, build);
80+
81+
URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-merge-multi.yml");
5282

53-
List<String> activeProfiles = new ArrayList<String>() {{
54-
add("dev");
55-
add("qa");
56-
}};
83+
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
84+
"application.yml"), "UTF-8", null, true);
85+
86+
Properties props = SpringBootUtil.getApplicationProperties(project, Collections.<String>emptyList());
5787

58-
Properties props = SpringBootUtil.getPropertiesFromYamlResource(
59-
SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"), activeProfiles);
6088
assertNotEquals(0, props.size());
6189

6290
assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port"));
6391
assertEquals("Hello", props.getProperty("my.name"));
64-
assertEquals("Hola!", props.getProperty("their.name"));
92+
assertEquals("Foo", props.getProperty("their.name"));
6593
}
6694

6795
@Test
68-
public void testYamlToPropertiesParsingWithActiveProfiles2() {
96+
public void testWithDifferentConfigName() throws Exception {
97+
98+
System.setProperty("spring.config.name", "foo");
99+
100+
MavenProject project = new MavenProject();
101+
Build build = new Build();
69102

70-
List<String> activeProfiles = new ArrayList<String>() {{
71-
add("qa");
72-
add("dev");
73-
}};
103+
setMavenProject(project, build);
104+
105+
URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-named.yml");
106+
107+
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
108+
"foo.yml"), "UTF-8", null, true);
109+
110+
Properties props = SpringBootUtil.getApplicationProperties(project, Collections.<String>emptyList());
74111

75-
Properties props = SpringBootUtil.getPropertiesFromYamlResource(
76-
SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"), activeProfiles);
77112
assertNotEquals(0, props.size());
78113

114+
assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port"));
115+
assertEquals("Foo", props.getProperty("their.name"));
116+
117+
System.getProperties().remove("spring.config.name");
118+
}
119+
120+
@Test
121+
public void testPropertiesInclude() throws Exception {
122+
123+
MavenProject project = new MavenProject();
124+
Build build = new Build();
125+
126+
setMavenProject(project, build);
127+
128+
URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-include.yml");
129+
130+
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
131+
"application.yml"), "UTF-8", null, true);
132+
133+
Properties props = SpringBootUtil.getApplicationProperties(project,Collections.<String>emptyList());
134+
135+
assertNotEquals(0, props.size());
136+
137+
assertEquals(new Integer(2020), PropertiesHelper.getInteger(props, "my.port"));
138+
assertEquals("bar", props.getProperty("my.name"));
139+
assertEquals("foo", props.getProperty("name"));
140+
}
141+
142+
143+
@Test
144+
public void testProfilePropertiesForDev() throws Exception {
145+
146+
MavenProject project = new MavenProject();
147+
Build build = new Build();
148+
149+
setMavenProject(project, build);
150+
151+
URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-multi.yml");
152+
153+
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
154+
"application.yml"), "UTF-8", null, true);
155+
156+
Properties props = SpringBootUtil.getApplicationProperties(project,"dev");
157+
79158
assertEquals(new Integer(8080), PropertiesHelper.getInteger(props, "server.port"));
80159
assertEquals("Hello", props.getProperty("my.name"));
81-
assertEquals("Hola!", props.getProperty("their.name"));
82160
}
83161

84162
@Test
85-
public void testNonExistentYamlToPropertiesParsing() {
163+
public void testProfilePropertiesForQa() throws Exception {
164+
165+
MavenProject project = new MavenProject();
166+
Build build = new Build();
167+
168+
setMavenProject(project, build);
169+
170+
URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-multi.yml");
86171

87-
Properties props = SpringBootUtil.getPropertiesFromYamlResource(
88-
SpringBootUtilTest.class.getResource("/this-file-does-not-exist")
89-
, Collections.<String>emptyList());
90-
assertNotNull(props);
91-
assertEquals(0, props.size());
172+
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
173+
"application.yml"), "UTF-8", null, true);
92174

175+
Properties props = SpringBootUtil.getApplicationProperties(project,"qa");
176+
177+
assertNotEquals(0, props.size());
178+
179+
assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port"));
180+
assertEquals("Hola!", props.getProperty("their.name"));
93181
}
94182

183+
// // @Test TODO SK Remove this after Roland Review - this will not happen at all
184+
// public void testNonExistentYamlToPropertiesParsing() throws Exception {
185+
//
186+
// Properties props = SpringBootUtil.getPropertiesFromYamlResource(
187+
// SpringBootUtilTest.class.getResource("/this-file-does-not-exist")
188+
// , null);
189+
//
190+
// MavenProject project = new MavenProject();
191+
// Build build = new Build();
192+
//
193+
// setMavenProject(project, build);
194+
//
195+
// URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/this-file-does-not-exist");
196+
//
197+
// FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
198+
// "application.yml"), "UTF-8", null, true);
199+
//
200+
// Properties props = SpringBootUtil.getApplicationProperties(project,"qa");
201+
// assertNotNull(props);
202+
// assertEquals(0, props.size());
203+
//
204+
// }
205+
95206
@Test
96-
public void testPropertiesParsing() {
207+
public void testPropertiesParsing() throws Exception {
208+
209+
MavenProject project = new MavenProject();
210+
Build build = new Build();
211+
212+
setMavenProject(project, build);
213+
214+
URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application.properties");
215+
216+
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
217+
"application.properties"), "UTF-8", null, true);
218+
219+
Properties props = SpringBootUtil.getApplicationProperties(project,Collections.<String>emptyList());
220+
97221

98-
Properties props = SpringBootUtil.getPropertiesResource(
99-
SpringBootUtilTest.class.getResource("/util/test-application.properties"));
100222
assertNotEquals(0, props.size());
101223

102224
assertEquals(new Integer(8081), PropertiesHelper.getInteger(props, "management.port"));
@@ -106,13 +228,21 @@ public void testPropertiesParsing() {
106228

107229
}
108230

109-
@Test
110-
public void testNonExistentPropertiesParsing() {
111-
112-
Properties props = SpringBootUtil.getPropertiesResource(SpringBootUtilTest.class.getResource("/this-file-does-not-exist"));
113-
assertNotNull(props);
114-
assertEquals(0, props.size());
115-
231+
// @Test TODO SK Remove this after Roland Review
232+
// public void testNonExistentPropertiesParsing() throws IOException {
233+
//
234+
// Properties props = SpringBootUtil.getPropertiesResource(SpringBootUtilTest.class.getResource(
235+
// "/this-file-does-not-exist"), null);
236+
// assertNotNull(props);
237+
// assertEquals(0, props.size());
238+
// }
239+
240+
public void setMavenProject(final MavenProject project, final Build build) throws IOException {
241+
//Set Build Dir
242+
final String outputTempDir = Files.createTempDirectory(UUID.randomUUID().toString()).toFile().getAbsolutePath();
243+
new File(outputTempDir).mkdirs();
244+
build.setOutputDirectory(outputTempDir);
245+
project.setBuild(build);
116246
}
117247

118248
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
spring:
3+
active:
4+
profiles: foo
5+
profiles:
6+
include: bar
7+
name: foo
8+
9+
---
10+
my:
11+
name: bar
12+
port: 2020
13+
spring:
14+
profiles: bar
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
my:
3+
name: "Hello"
4+
5+
---
6+
server:
7+
port: 9090
8+
their:
9+
name: Foo

core/src/test/resources/util/test-application-multi.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
spring:
2-
profiles:
3-
active: dev,qa
4-
51
---
62

73
spring:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
server:
2+
port: 9090
3+
their:
4+
name: Foo

generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public class SpringBootGenerator extends JavaExecGenerator {
5252
private static final String DEFAULT_SERVER_PORT = "8080";
5353

5454
public enum Config implements Configs.Key {
55-
color {{ d = "false"; }},
55+
color {{
56+
d = "false";
57+
}},
5658

5759
// comma separated list of spring boot profile(s) that would be passed set as -Dspring.profiles.active
5860
activeProfiles;
@@ -89,8 +91,7 @@ public List<ImageConfiguration> customize(List<ImageConfiguration> configs, bool
8991
protected Map<String, String> getEnv(boolean prePackagePhase) throws MojoExecutionException {
9092
Map<String, String> res = super.getEnv(prePackagePhase);
9193
if (getContext().isWatchMode()) {
92-
String strActiveProfiles = getContext().getConfig().getConfig("spring-boot", "activeProfiles");
93-
Properties properties = SpringBootUtil.getApplicationProperties(getProject(), strActiveProfiles);
94+
Properties properties = getSpringBootProperties();
9495
// adding dev tools token to env variables to prevent override during recompile
9596
String secret = properties.getProperty(SpringBootProperties.DEV_TOOLS_REMOTE_SECRET);
9697
if (secret != null) {
@@ -127,12 +128,8 @@ protected boolean isFatJar() throws MojoExecutionException {
127128
protected List<String> extractPorts() {
128129
List<String> answer = new ArrayList<>();
129130

130-
String strActiveProfiles = getConfig(activeProfiles);
131-
132-
Properties properties = SpringBootUtil.getApplicationProperties(getContext().getProject(),
133-
SpringBootUtil.getActiveProfiles(strActiveProfiles));
134-
135-
//TODO SK - do we need to handle the parsin of port properties like ${PORT:1234}
131+
Properties properties = getSpringBootProperties();
132+
//TODO SK - do we need to handle the parsing of port properties like ${PORT:1234}
136133
String port = properties.getProperty(SpringBootProperties.SERVER_PORT, DEFAULT_SERVER_PORT);
137134

138135
addPortIfValid(answer, getConfig(JavaExecGenerator.Config.webPort, port));
@@ -144,10 +141,7 @@ protected List<String> extractPorts() {
144141
// =============================================================================
145142

146143
private void ensureSpringDevToolSecretToken() throws MojoExecutionException {
147-
String strActiveProfiles = getConfig(activeProfiles);
148-
149-
Properties properties = SpringBootUtil.getApplicationProperties(getContext().getProject(),
150-
SpringBootUtil.getActiveProfiles(strActiveProfiles));
144+
Properties properties = getSpringBootProperties();
151145
String remoteSecret = properties.getProperty(DEV_TOOLS_REMOTE_SECRET);
152146
if (Strings.isNullOrEmpty(remoteSecret)) {
153147
addSecretTokenToApplicationProperties();
@@ -295,6 +289,15 @@ private boolean isSpringBootRepackage() {
295289
return false;
296290
}
297291

292+
private Properties getSpringBootProperties() {
293+
try {
294+
String strActiveProfiles = getContext().getConfig().getConfig("spring-boot", "activeProfiles");
295+
return SpringBootUtil.getApplicationProperties(getProject(), strActiveProfiles);
296+
} catch (IOException e) {
297+
throw new IllegalStateException("Failed to load Spring Boot properties", e);
298+
}
299+
}
300+
298301
private File getSpringBootDevToolsJar() throws IOException {
299302
String version = SpringBootUtil.getSpringBootDevToolsVersion(getProject());
300303
if (version == null) {

parent/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<version.jacoco>0.7.8</version.jacoco>
6767
<version.fabric8>2.2.211</version.fabric8>
6868
<version.kubernetes-client>2.3.1</version.kubernetes-client>
69+
<version.spring-boot>1.5.3.RELEASE</version.spring-boot>
6970
<version.mockwebserver>0.0.13</version.mockwebserver>
7071
<version.docker-maven-plugin>0.21.0</version.docker-maven-plugin>
7172

0 commit comments

Comments
 (0)