Skip to content

Commit

Permalink
Add overrides for url, scm, licenses and developers to POM
Browse files Browse the repository at this point in the history
This behavior is controlled via the addOverrideIfEmpty flag on the
MavenBuildSettings. It defaults to false, but is switched to true
in the DefaultMavenBuildCustomizer.

Closes gh-1242
  • Loading branch information
mhalbritter committed Jun 13, 2024
1 parent f41b879 commit 0c7882e
Show file tree
Hide file tree
Showing 24 changed files with 389 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.spring.initializr.generator.packaging.war.WarPackaging;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
import io.spring.initializr.generator.spring.build.maven.DefaultMavenBuildCustomizer;
import io.spring.initializr.metadata.InitializrMetadata;

import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -74,12 +73,6 @@ public DefaultStarterBuildCustomizer defaultStarterContributor(InitializrMetadat
return new DefaultStarterBuildCustomizer(metadata, projectDescription);
}

@Bean
public DefaultMavenBuildCustomizer initializrMetadataMavenBuildCustomizer(ProjectDescription description,
InitializrMetadata metadata) {
return new DefaultMavenBuildCustomizer(description, metadata);
}

@Bean
@ConditionalOnPackaging(WarPackaging.ID)
public WarPackagingWebStarterBuildCustomizer warPackagingWebStarterBuildCustomizer(InitializrMetadata metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public DefaultMavenBuildCustomizer(ProjectDescription description, InitializrMet

@Override
public void customize(MavenBuild build) {
build.settings().addOverrideIfEmpty(true);
build.settings().name(this.description.getName()).description(this.description.getDescription());
build.properties().property("java.version", this.description.getLanguage().jvmVersion());
build.plugins().add("org.springframework.boot", "spring-boot-maven-plugin");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import io.spring.initializr.generator.condition.ConditionalOnPackaging;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.packaging.war.WarPackaging;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.spring.util.LambdaSafe;
import io.spring.initializr.metadata.InitializrMetadata;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
Expand All @@ -43,6 +45,12 @@
@ConditionalOnBuildSystem(MavenBuildSystem.ID)
public class MavenProjectGenerationConfiguration {

@Bean
DefaultMavenBuildCustomizer initializrMetadataMavenBuildCustomizer(ProjectDescription description,
InitializrMetadata metadata) {
return new DefaultMavenBuildCustomizer(description, metadata);
}

@Bean
MavenWrapperContributor mavenWrapperContributor() {
return new MavenWrapperContributor("3");
Expand All @@ -69,6 +77,11 @@ public MavenBuildProjectContributor mavenBuildProjectContributor(MavenBuild buil
return new MavenBuildProjectContributor(build, indentingWriterFactory);
}

@Bean
ParentOverridesHelpDocumentCustomizer parentOverridesHelpDocumentCustomizer(MavenBuild build) {
return new ParentOverridesHelpDocumentCustomizer(build.getSettings());
}

@Bean
@ConditionalOnPackaging(WarPackaging.ID)
public BuildCustomizer<MavenBuild> mavenWarPackagingConfigurer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.build.maven;

import java.util.Collections;

import io.spring.initializr.generator.buildsystem.maven.MavenBuildSettings;
import io.spring.initializr.generator.spring.documentation.HelpDocument;
import io.spring.initializr.generator.spring.documentation.HelpDocumentCustomizer;

/**
* {@link HelpDocumentCustomizer} to add a section about the parent overrides in place.
*
* @author Moritz Halbritter
*/
class ParentOverridesHelpDocumentCustomizer implements HelpDocumentCustomizer {

private final MavenBuildSettings buildSettings;

ParentOverridesHelpDocumentCustomizer(MavenBuildSettings buildSettings) {
this.buildSettings = buildSettings;
}

@Override
public void customize(HelpDocument document) {
if (this.buildSettings.isAddOverrideIfEmpty()) {
document.addSection("documentation/parent-overrides", Collections.emptyMap());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Maven Parent overrides

Due to Maven's design, elements are inherited from the parent POM to the project POM.
While most of the inheritance is fine, it also inherits unwanted elements like `<license>` and `<developers>` from the parent.
To prevent this, the project POM contains empty overrides for these elements.
If you manually switch to a different parent and actually want the inheritance, you need to remove those overrides.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void customBaseDirectoryIsUsedWhenGeneratingProject() {
"test/demo-app/mvnw.cmd", "test/demo-app/.mvn/wrapper/maven-wrapper.properties",
"test/demo-app/src/main/java/com/example/demo/DemoApplication.java",
"test/demo-app/src/main/resources/application.properties",
"test/demo-app/src/test/java/com/example/demo/DemoApplicationTests.java");
"test/demo-app/src/test/java/com/example/demo/DemoApplicationTests.java", "test/demo-app/HELP.md");
}

private MutableProjectDescription initProjectDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ void currentGenerationJarKotlin(BuildSystem build, String fileName) {
}

private void testCurrentGenerationJar(Language language, BuildSystem build, String fileName) {
String path = "project/" + language + "/standard/" + getAssertFileName(fileName);
assertThat(generateProject(language, build, "2.4.1")).textFile(fileName)
.hasSameContentAs(
new ClassPathResource("project/" + language + "/standard/" + getAssertFileName(fileName)));
.as("Resource " + path)
.hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -95,8 +96,10 @@ void nextGenerationJarGroovy(BuildSystem build, String fileName) {
}

private void testNextGenerationJar(Language language, BuildSystem build, String fileName) {
String path = "project/" + language + "/next/" + getAssertFileName(fileName);
assertThat(generateProject(language, build, "3.0.0")).textFile(fileName)
.hasSameContentAs(new ClassPathResource("project/" + language + "/next/" + getAssertFileName(fileName)));
.as("Resource " + path)
.hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -122,19 +125,17 @@ private void testCurrentGenerationWar(Language language, BuildSystem build, Stri
description.addDependency("web", MetadataBuildItemMapper.toDependency(WEB));
description.setPackaging(Packaging.forId("war"));
});
assertThat(project).textFile(fileName)
.hasSameContentAs(
new ClassPathResource("project/" + language + "/standard/war-" + getAssertFileName(fileName)));
String path = "project/" + language + "/standard/war-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
@MethodSource("parameters")
void kotlinJava11(BuildSystem build, String fileName) {
ProjectStructure project = generateProject(kotlin, build, "2.4.1",
(description) -> description.setLanguage(Language.forId(kotlin.id(), "11")));
assertThat(project).textFile(fileName)
.hasSameContentAs(
new ClassPathResource("project/" + build + "/kotlin-java11-" + getAssertFileName(fileName)));
String path = "project/" + build + "/kotlin-java11-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -146,9 +147,8 @@ void versionOverride(BuildSystem build, String fileName) {
() -> (projectBuild) -> projectBuild.properties()
.version(VersionProperty.of("spring-foo.version", false), "0.1.0.RELEASE")
.version(VersionProperty.of("spring-bar.version"), "0.2.0.RELEASE")));
assertThat(project).textFile(fileName)
.hasSameContentAs(
new ClassPathResource("project/" + build + "/version-override-" + getAssertFileName(fileName)));
String path = "project/" + build + "/version-override-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -164,9 +164,8 @@ void bomWithVersionProperty(BuildSystem build, String fileName) {
.build();
ProjectStructure project = generateProject(java, build, "2.4.1",
(description) -> description.addDependency("foo", MetadataBuildItemMapper.toDependency(foo)), metadata);
assertThat(project).textFile(fileName)
.hasSameContentAs(
new ClassPathResource("project/" + build + "/bom-property-" + getAssertFileName(fileName)));
String path = "project/" + build + "/bom-property-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -184,9 +183,8 @@ void compileOnlyDependency(BuildSystem build, String fileName) {
description.addDependency("web", MetadataBuildItemMapper.toDependency(WEB));
description.addDependency("data-jpa", MetadataBuildItemMapper.toDependency(dataJpa));
}, metadata);
assertThat(project).textFile(fileName)
.hasSameContentAs(new ClassPathResource(
"project/" + build + "/compile-only-dependency-" + getAssertFileName(fileName)));
String path = "project/" + build + "/compile-only-dependency-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -206,9 +204,8 @@ void annotationProcessorDependency(BuildSystem build, String fileName) {
description.addDependency("web", MetadataBuildItemMapper.toDependency(WEB));
description.addDependency("data-jpa", MetadataBuildItemMapper.toDependency(dataJpa));
}, metadata);
assertThat(project).textFile(fileName)
.hasSameContentAs(new ClassPathResource(
"project/" + build + "/annotation-processor-dependency-" + getAssertFileName(fileName)));
String path = "project/" + build + "/annotation-processor-dependency-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -233,9 +230,8 @@ void bomWithOrdering(BuildSystem build, String fileName) {
.build();
ProjectStructure project = generateProject(java, build, "2.4.1",
(description) -> description.addDependency("foo", MetadataBuildItemMapper.toDependency(foo)), metadata);
assertThat(project).textFile(fileName)
.hasSameContentAs(
new ClassPathResource("project/" + build + "/bom-ordering-" + getAssertFileName(fileName)));
String path = "project/" + build + "/bom-ordering-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -254,9 +250,8 @@ void repositories(BuildSystem build, String fileName) {
description.addDependency("foo", MetadataBuildItemMapper.toDependency(foo));
description.addDependency("bar", MetadataBuildItemMapper.toDependency(bar));
}, metadata);
assertThat(project).textFile(fileName)
.hasSameContentAs(
new ClassPathResource("project/" + build + "/repositories-" + getAssertFileName(fileName)));
String path = "project/" + build + "/repositories-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

@ParameterizedTest
Expand All @@ -270,9 +265,8 @@ void repositoriesMilestone(BuildSystem build, String fileName) {
description.setPlatformVersion(Version.parse("2.4.0-M1"));
description.addDependency("foo", MetadataBuildItemMapper.toDependency(foo));
}, metadata);
assertThat(project).textFile(fileName)
.hasSameContentAs(new ClassPathResource(
"project/" + build + "/repositories-milestone-" + getAssertFileName(fileName)));
String path = "project/" + build + "/repositories-milestone-" + getAssertFileName(fileName);
assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path));
}

private String getAssertFileName(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Loading

0 comments on commit 0c7882e

Please sign in to comment.