Skip to content

Commit 564c524

Browse files
committed
Add support for customizable application properties
Closes gh-1516
1 parent 34c623d commit 564c524

File tree

11 files changed

+338
-52
lines changed

11 files changed

+338
-52
lines changed

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/configuration/ApplicationConfigurationProjectGenerationConfiguration.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,17 +26,13 @@
2626
* Configuration for application-related contributions to a generated project.
2727
*
2828
* @author Stephane Nicoll
29+
* @author Moritz Halbritter
2930
*/
3031
@ProjectGenerationConfiguration
3132
public class ApplicationConfigurationProjectGenerationConfiguration {
3233

3334
@Bean
34-
public ApplicationPropertiesContributor applicationPropertiesContributor() {
35-
return new ApplicationPropertiesContributor();
36-
}
37-
38-
@Bean
39-
public WebFoldersContributor webFoldersContributor(Build build, InitializrMetadata metadata) {
35+
WebFoldersContributor webFoldersContributor(Build build, InitializrMetadata metadata) {
4036
return new WebFoldersContributor(build, metadata);
4137
}
4238

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/configuration/ApplicationPropertiesContributor.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.properties;
18+
19+
import java.io.PrintWriter;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* Application properties.
27+
*
28+
* @author Moritz Halbritter
29+
*/
30+
public class ApplicationProperties {
31+
32+
private final Map<String, Object> properties = new HashMap<>();
33+
34+
/**
35+
* Adds a new property.
36+
* @param key the key of the property
37+
* @param value the value of the property
38+
*/
39+
public void add(String key, long value) {
40+
add(key, (Object) value);
41+
}
42+
43+
/**
44+
* Adds a new property.
45+
* @param key the key of the property
46+
* @param value the value of the property
47+
*/
48+
public void add(String key, boolean value) {
49+
add(key, (Object) value);
50+
}
51+
52+
/**
53+
* Adds a new property.
54+
* @param key the key of the property
55+
* @param value the value of the property
56+
*/
57+
public void add(String key, double value) {
58+
add(key, (Object) value);
59+
}
60+
61+
/**
62+
* Adds a new property.
63+
* @param key the key of the property
64+
* @param value the value of the property
65+
*/
66+
public void add(String key, String value) {
67+
add(key, (Object) value);
68+
}
69+
70+
void writeTo(PrintWriter writer) {
71+
for (Map.Entry<String, Object> entry : this.properties.entrySet()) {
72+
writer.printf("%s=%s%n", entry.getKey(), entry.getValue());
73+
}
74+
}
75+
76+
private void add(String key, Object value) {
77+
Assert.state(!this.properties.containsKey(key), () -> "Property '%s' already exists".formatted(key));
78+
this.properties.put(key, value);
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.properties;
18+
19+
import java.io.IOException;
20+
import java.io.PrintWriter;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.StandardOpenOption;
25+
26+
import io.spring.initializr.generator.project.contributor.ProjectContributor;
27+
28+
/**
29+
* A {@link ProjectContributor} that contributes a {@code application.properties} file to
30+
* a project.
31+
*
32+
* @author Stephane Nicoll
33+
* @author Moritz Halbritter
34+
*/
35+
public class ApplicationPropertiesContributor implements ProjectContributor {
36+
37+
private static final String FILE = "src/main/resources/application.properties";
38+
39+
private final ApplicationProperties properties;
40+
41+
public ApplicationPropertiesContributor(ApplicationProperties properties) {
42+
this.properties = properties;
43+
}
44+
45+
@Override
46+
public void contribute(Path projectRoot) throws IOException {
47+
Path output = projectRoot.resolve(FILE);
48+
if (!Files.exists(output)) {
49+
Files.createDirectories(output.getParent());
50+
Files.createFile(output);
51+
}
52+
try (PrintWriter writer = new PrintWriter(Files.newOutputStream(output, StandardOpenOption.APPEND), false,
53+
StandardCharsets.UTF_8)) {
54+
this.properties.writeTo(writer);
55+
}
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.properties;
18+
19+
/**
20+
* Callback for customizing a project's {@link ApplicationProperties}.
21+
*
22+
* @author Moritz Halbritter
23+
*/
24+
@FunctionalInterface
25+
public interface ApplicationPropertiesCustomizer {
26+
27+
/**
28+
* Customizes the project's {@link ApplicationProperties}.
29+
* @param properties the properties to customize
30+
*/
31+
void customize(ApplicationProperties properties);
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.properties;
18+
19+
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
20+
21+
import org.springframework.beans.factory.ObjectProvider;
22+
import org.springframework.context.annotation.Bean;
23+
24+
/**
25+
* Configuration for application properties related contributions to a generated project.
26+
*
27+
* @author Moritz Halbritter
28+
*/
29+
@ProjectGenerationConfiguration
30+
class ApplicationPropertiesProjectGenerationConfiguration {
31+
32+
@Bean
33+
ApplicationProperties applicationProperties(ObjectProvider<ApplicationPropertiesCustomizer> customizers) {
34+
ApplicationProperties properties = new ApplicationProperties();
35+
customizers.orderedStream().forEach((customizer) -> customizer.customize(properties));
36+
return properties;
37+
}
38+
39+
@Bean
40+
ApplicationPropertiesContributor applicationPropertiesContributor(ApplicationProperties applicationProperties) {
41+
return new ApplicationPropertiesContributor(applicationProperties);
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Support for application properties.
19+
*/
20+
package io.spring.initializr.generator.spring.properties;

initializr-generator-spring/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ io.spring.initializr.generator.spring.code.java.JavaProjectGenerationConfigurati
88
io.spring.initializr.generator.spring.code.kotlin.KotlinProjectGenerationConfiguration,\
99
io.spring.initializr.generator.spring.configuration.ApplicationConfigurationProjectGenerationConfiguration,\
1010
io.spring.initializr.generator.spring.documentation.HelpDocumentProjectGenerationConfiguration,\
11+
io.spring.initializr.generator.spring.properties.ApplicationPropertiesProjectGenerationConfiguration,\
1112
io.spring.initializr.generator.spring.scm.git.GitProjectGenerationConfiguration

initializr-generator-spring/src/main/resources/configuration/application.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,10 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.spring.initializr.generator.spring.configuration;
17+
package io.spring.initializr.generator.spring.properties;
1818

1919
import java.io.IOException;
20-
import java.nio.file.Files;
2120
import java.nio.file.Path;
2221

2322
import io.spring.initializr.generator.test.project.ProjectStructure;
@@ -30,6 +29,7 @@
3029
* Tests for {@link ApplicationPropertiesContributor}.
3130
*
3231
* @author Stephane Nicoll
32+
* @author Moritz Halbritter
3333
*/
3434
class ApplicationPropertiesContributorTests {
3535

@@ -38,11 +38,20 @@ class ApplicationPropertiesContributorTests {
3838

3939
@Test
4040
void applicationConfigurationWithDefaultSettings() throws IOException {
41-
Path projectDir = Files.createTempDirectory(this.directory, "project-");
42-
new ApplicationPropertiesContributor().contribute(projectDir);
43-
assertThat(new ProjectStructure(projectDir)).textFile("src/main/resources/application.properties")
44-
.lines()
41+
new ApplicationPropertiesContributor(new ApplicationProperties()).contribute(this.directory);
42+
assertThat(new ProjectStructure(this.directory)).textFile("src/main/resources/application.properties")
4543
.isEmpty();
4644
}
4745

46+
@Test
47+
void shouldAddStringProperty() throws IOException {
48+
ApplicationProperties properties = new ApplicationProperties();
49+
properties.add("spring.application.name", "test");
50+
ApplicationPropertiesContributor contributor = new ApplicationPropertiesContributor(properties);
51+
contributor.contribute(this.directory);
52+
assertThat(new ProjectStructure(this.directory)).textFile("src/main/resources/application.properties")
53+
.lines()
54+
.contains("spring.application.name=test");
55+
}
56+
4857
}

0 commit comments

Comments
 (0)