Skip to content

Commit

Permalink
Add spring.application.name to application.properties
Browse files Browse the repository at this point in the history
Closes gh-1427
  • Loading branch information
mhalbritter committed Mar 11, 2024
1 parent d408fb9 commit d3e96c1
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.start.site.extension.properties;

import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;

import org.springframework.context.annotation.Bean;

/**
* {@link ProjectGenerationConfiguration} for customizations relevant to the application
* properties.
*
* @author Moritz Halbritter
*/
@ProjectGenerationConfiguration
class ApplicationPropertiesProjectGenerationConfiguration {

@Bean
DefaultApplicationPropertiesCustomizer defaultApplicationPropertiesContributorCustomizer(
ProjectDescription projectDescription) {
return new DefaultApplicationPropertiesCustomizer(projectDescription);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.start.site.extension.properties;

import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.properties.ApplicationProperties;
import io.spring.initializr.generator.spring.properties.ApplicationPropertiesCustomizer;

import org.springframework.util.StringUtils;

/**
* {@link ApplicationPropertiesCustomizer} to add default application properties.
*
* @author Moritz Halbritter
*/
class DefaultApplicationPropertiesCustomizer implements ApplicationPropertiesCustomizer {

private final ProjectDescription projectDescription;

DefaultApplicationPropertiesCustomizer(ProjectDescription projectDescription) {
this.projectDescription = projectDescription;
}

@Override
public void customize(ApplicationProperties properties) {
String name = this.projectDescription.getName();
if (StringUtils.hasLength(name)) {
properties.add("spring.application.name", name);
}
}

}
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.
*/

/**
* Extensions for customization of the application properties.
*/
package io.spring.start.site.extension.properties;
3 changes: 2 additions & 1 deletion start-site/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
io.spring.initializr.generator.project.ProjectGenerationConfiguration=\
io.spring.start.site.extension.build.gradle.GradleProjectGenerationConfiguration,\
io.spring.start.site.extension.build.maven.MavenProjectGenerationConfiguration,\
io.spring.start.site.extension.code.kotlin.KotlinProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.DependencyProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.activemq.ActiveMQProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.activemq.ArtemisProjectGenerationConfiguration,\
Expand Down Expand Up @@ -38,4 +39,4 @@ io.spring.start.site.extension.dependency.testcontainers.TestcontainersProjectGe
io.spring.start.site.extension.dependency.vaadin.VaadinProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.zipkin.ZipkinProjectGenerationConfiguration,\
io.spring.start.site.extension.description.DescriptionProjectGenerationConfiguration,\
io.spring.start.site.extension.code.kotlin.KotlinProjectGenerationConfiguration
io.spring.start.site.extension.properties.ApplicationPropertiesProjectGenerationConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.start.site.extension.properties;

import io.spring.initializr.generator.test.io.TextAssert;
import io.spring.initializr.generator.test.project.ProjectStructure;
import io.spring.initializr.web.project.ProjectRequest;
import io.spring.start.site.extension.AbstractExtensionTests;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link DefaultApplicationPropertiesCustomizer}.
*
* @author Moritz Halbritter
*/
class DefaultApplicationPropertiesCustomizerTests extends AbstractExtensionTests {

@Test
void shouldAddSpringApplicationName() {
ProjectRequest request = createProjectRequest("web");
request.setBootVersion("3.1.0");
request.setJavaVersion("21");
request.setName("test");
assertApplicationProperties(request).lines().contains("spring.application.name=test");
}

private TextAssert assertApplicationProperties(ProjectRequest request) {
ProjectStructure project = generateProject(request);
return new TextAssert(project.getProjectDirectory().resolve("src/main/resources/application.properties"));
}

}

0 comments on commit d3e96c1

Please sign in to comment.