-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for customizable application properties
Closes gh-1516
- Loading branch information
1 parent
34c623d
commit 564c524
Showing
11 changed files
with
338 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
...io/spring/initializr/generator/spring/configuration/ApplicationPropertiesContributor.java
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
...src/main/java/io/spring/initializr/generator/spring/properties/ApplicationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.initializr.generator.spring.properties; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Application properties. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
public class ApplicationProperties { | ||
|
||
private final Map<String, Object> properties = new HashMap<>(); | ||
|
||
/** | ||
* Adds a new property. | ||
* @param key the key of the property | ||
* @param value the value of the property | ||
*/ | ||
public void add(String key, long value) { | ||
add(key, (Object) value); | ||
} | ||
|
||
/** | ||
* Adds a new property. | ||
* @param key the key of the property | ||
* @param value the value of the property | ||
*/ | ||
public void add(String key, boolean value) { | ||
add(key, (Object) value); | ||
} | ||
|
||
/** | ||
* Adds a new property. | ||
* @param key the key of the property | ||
* @param value the value of the property | ||
*/ | ||
public void add(String key, double value) { | ||
add(key, (Object) value); | ||
} | ||
|
||
/** | ||
* Adds a new property. | ||
* @param key the key of the property | ||
* @param value the value of the property | ||
*/ | ||
public void add(String key, String value) { | ||
add(key, (Object) value); | ||
} | ||
|
||
void writeTo(PrintWriter writer) { | ||
for (Map.Entry<String, Object> entry : this.properties.entrySet()) { | ||
writer.printf("%s=%s%n", entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
private void add(String key, Object value) { | ||
Assert.state(!this.properties.containsKey(key), () -> "Property '%s' already exists".formatted(key)); | ||
this.properties.put(key, value); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...va/io/spring/initializr/generator/spring/properties/ApplicationPropertiesContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.initializr.generator.spring.properties; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
|
||
import io.spring.initializr.generator.project.contributor.ProjectContributor; | ||
|
||
/** | ||
* A {@link ProjectContributor} that contributes a {@code application.properties} file to | ||
* a project. | ||
* | ||
* @author Stephane Nicoll | ||
* @author Moritz Halbritter | ||
*/ | ||
public class ApplicationPropertiesContributor implements ProjectContributor { | ||
|
||
private static final String FILE = "src/main/resources/application.properties"; | ||
|
||
private final ApplicationProperties properties; | ||
|
||
public ApplicationPropertiesContributor(ApplicationProperties properties) { | ||
this.properties = properties; | ||
} | ||
|
||
@Override | ||
public void contribute(Path projectRoot) throws IOException { | ||
Path output = projectRoot.resolve(FILE); | ||
if (!Files.exists(output)) { | ||
Files.createDirectories(output.getParent()); | ||
Files.createFile(output); | ||
} | ||
try (PrintWriter writer = new PrintWriter(Files.newOutputStream(output, StandardOpenOption.APPEND), false, | ||
StandardCharsets.UTF_8)) { | ||
this.properties.writeTo(writer); | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...ava/io/spring/initializr/generator/spring/properties/ApplicationPropertiesCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.initializr.generator.spring.properties; | ||
|
||
/** | ||
* Callback for customizing a project's {@link ApplicationProperties}. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
@FunctionalInterface | ||
public interface ApplicationPropertiesCustomizer { | ||
|
||
/** | ||
* Customizes the project's {@link ApplicationProperties}. | ||
* @param properties the properties to customize | ||
*/ | ||
void customize(ApplicationProperties properties); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...lizr/generator/spring/properties/ApplicationPropertiesProjectGenerationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.initializr.generator.spring.properties; | ||
|
||
import io.spring.initializr.generator.project.ProjectGenerationConfiguration; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* Configuration for application properties related contributions to a generated project. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
@ProjectGenerationConfiguration | ||
class ApplicationPropertiesProjectGenerationConfiguration { | ||
|
||
@Bean | ||
ApplicationProperties applicationProperties(ObjectProvider<ApplicationPropertiesCustomizer> customizers) { | ||
ApplicationProperties properties = new ApplicationProperties(); | ||
customizers.orderedStream().forEach((customizer) -> customizer.customize(properties)); | ||
return properties; | ||
} | ||
|
||
@Bean | ||
ApplicationPropertiesContributor applicationPropertiesContributor(ApplicationProperties applicationProperties) { | ||
return new ApplicationPropertiesContributor(applicationProperties); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...r-spring/src/main/java/io/spring/initializr/generator/spring/properties/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Support for application properties. | ||
*/ | ||
package io.spring.initializr.generator.spring.properties; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
initializr-generator-spring/src/main/resources/configuration/application.properties
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.