Skip to content

Commit

Permalink
Add support for customizable application properties
Browse files Browse the repository at this point in the history
Closes gh-1516
  • Loading branch information
mhalbritter committed Mar 8, 2024
1 parent 34c623d commit 564c524
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
Expand All @@ -26,17 +26,13 @@
* Configuration for application-related contributions to a generated project.
*
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
@ProjectGenerationConfiguration
public class ApplicationConfigurationProjectGenerationConfiguration {

@Bean
public ApplicationPropertiesContributor applicationPropertiesContributor() {
return new ApplicationPropertiesContributor();
}

@Bean
public WebFoldersContributor webFoldersContributor(Build build, InitializrMetadata metadata) {
WebFoldersContributor webFoldersContributor(Build build, InitializrMetadata metadata) {
return new WebFoldersContributor(build, metadata);
}

Expand Down

This file was deleted.

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);
}

}
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);
}
}

}
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);

}
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);
}

}
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;
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ io.spring.initializr.generator.spring.code.java.JavaProjectGenerationConfigurati
io.spring.initializr.generator.spring.code.kotlin.KotlinProjectGenerationConfiguration,\
io.spring.initializr.generator.spring.configuration.ApplicationConfigurationProjectGenerationConfiguration,\
io.spring.initializr.generator.spring.documentation.HelpDocumentProjectGenerationConfiguration,\
io.spring.initializr.generator.spring.properties.ApplicationPropertiesProjectGenerationConfiguration,\
io.spring.initializr.generator.spring.scm.git.GitProjectGenerationConfiguration

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* 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.
Expand All @@ -14,10 +14,9 @@
* limitations under the License.
*/

package io.spring.initializr.generator.spring.configuration;
package io.spring.initializr.generator.spring.properties;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import io.spring.initializr.generator.test.project.ProjectStructure;
Expand All @@ -30,6 +29,7 @@
* Tests for {@link ApplicationPropertiesContributor}.
*
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
class ApplicationPropertiesContributorTests {

Expand All @@ -38,11 +38,20 @@ class ApplicationPropertiesContributorTests {

@Test
void applicationConfigurationWithDefaultSettings() throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-");
new ApplicationPropertiesContributor().contribute(projectDir);
assertThat(new ProjectStructure(projectDir)).textFile("src/main/resources/application.properties")
.lines()
new ApplicationPropertiesContributor(new ApplicationProperties()).contribute(this.directory);
assertThat(new ProjectStructure(this.directory)).textFile("src/main/resources/application.properties")
.isEmpty();
}

@Test
void shouldAddStringProperty() throws IOException {
ApplicationProperties properties = new ApplicationProperties();
properties.add("spring.application.name", "test");
ApplicationPropertiesContributor contributor = new ApplicationPropertiesContributor(properties);
contributor.contribute(this.directory);
assertThat(new ProjectStructure(this.directory)).textFile("src/main/resources/application.properties")
.lines()
.contains("spring.application.name=test");
}

}
Loading

0 comments on commit 564c524

Please sign in to comment.