diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/BuildProjectGenerationConfiguration.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/BuildProjectGenerationConfiguration.java index aaf35a7c0d..33b2623086 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/BuildProjectGenerationConfiguration.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/BuildProjectGenerationConfiguration.java @@ -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; @@ -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, diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/DefaultMavenBuildCustomizer.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/DefaultMavenBuildCustomizer.java index 47c4cd8e4b..197b18135f 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/DefaultMavenBuildCustomizer.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/DefaultMavenBuildCustomizer.java @@ -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"); diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/MavenProjectGenerationConfiguration.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/MavenProjectGenerationConfiguration.java index efe9c93fe6..db031e05c7 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/MavenProjectGenerationConfiguration.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/MavenProjectGenerationConfiguration.java @@ -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; @@ -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"); @@ -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 mavenWarPackagingConfigurer() { diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/ParentOverridesHelpDocumentCustomizer.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/ParentOverridesHelpDocumentCustomizer.java new file mode 100644 index 0000000000..5510158177 --- /dev/null +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/ParentOverridesHelpDocumentCustomizer.java @@ -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()); + } + } + +} diff --git a/initializr-generator-spring/src/main/resources/templates/documentation/parent-overrides.mustache b/initializr-generator-spring/src/main/resources/templates/documentation/parent-overrides.mustache new file mode 100644 index 0000000000..b43ca2b86b --- /dev/null +++ b/initializr-generator-spring/src/main/resources/templates/documentation/parent-overrides.mustache @@ -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 `` and `` 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. diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java index 35e311d1ba..6d6ac40a64 100644 --- a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java +++ b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java @@ -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() { diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/BuildComplianceTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/BuildComplianceTests.java index d6ebd7a36b..b10a290a01 100755 --- a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/BuildComplianceTests.java +++ b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/BuildComplianceTests.java @@ -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 @@ -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 @@ -122,9 +125,8 @@ 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 @@ -132,9 +134,8 @@ private void testCurrentGenerationWar(Language language, BuildSystem build, Stri 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) { diff --git a/initializr-generator-spring/src/test/resources/project/groovy/next/pom.xml.gen b/initializr-generator-spring/src/test/resources/project/groovy/next/pom.xml.gen index 07250e2949..9d89401a04 100644 --- a/initializr-generator-spring/src/test/resources/project/groovy/next/pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/groovy/next/pom.xml.gen @@ -13,6 +13,19 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot + + + + + + + + + + + + + 17 diff --git a/initializr-generator-spring/src/test/resources/project/groovy/standard/pom.xml.gen b/initializr-generator-spring/src/test/resources/project/groovy/standard/pom.xml.gen index 5ec0de9390..0807c50e70 100644 --- a/initializr-generator-spring/src/test/resources/project/groovy/standard/pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/groovy/standard/pom.xml.gen @@ -13,11 +13,22 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/groovy/standard/war-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/groovy/standard/war-pom.xml.gen index 260ccde45c..f71b12cc75 100644 --- a/initializr-generator-spring/src/test/resources/project/groovy/standard/war-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/groovy/standard/war-pom.xml.gen @@ -14,11 +14,22 @@ war demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/java/standard/pom.xml.gen b/initializr-generator-spring/src/test/resources/project/java/standard/pom.xml.gen index eede359dae..03f05a3412 100644 --- a/initializr-generator-spring/src/test/resources/project/java/standard/pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/java/standard/pom.xml.gen @@ -13,11 +13,22 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/java/standard/war-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/java/standard/war-pom.xml.gen index 293351fc68..cb62dbdbc9 100644 --- a/initializr-generator-spring/src/test/resources/project/java/standard/war-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/java/standard/war-pom.xml.gen @@ -14,11 +14,22 @@ war demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/kotlin/standard/pom.xml.gen b/initializr-generator-spring/src/test/resources/project/kotlin/standard/pom.xml.gen index 5b355f4bb8..b660458cc8 100644 --- a/initializr-generator-spring/src/test/resources/project/kotlin/standard/pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/kotlin/standard/pom.xml.gen @@ -13,12 +13,23 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 1.1.1 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-pom.xml.gen index 8c4f5b0abf..16af434ed5 100644 --- a/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-pom.xml.gen @@ -14,12 +14,23 @@ war demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 1.1.1 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/maven/annotation-processor-dependency-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/annotation-processor-dependency-pom.xml.gen index 891a84b8ed..3b1abc8dab 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/annotation-processor-dependency-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/annotation-processor-dependency-pom.xml.gen @@ -13,11 +13,22 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/maven/bom-ordering-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/bom-ordering-pom.xml.gen index 274a457ec4..8806119dd5 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/bom-ordering-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/bom-ordering-pom.xml.gen @@ -13,11 +13,22 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.acme @@ -30,7 +41,6 @@ test - diff --git a/initializr-generator-spring/src/test/resources/project/maven/bom-property-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/bom-property-pom.xml.gen index 983c92ef24..4aea65292c 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/bom-property-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/bom-property-pom.xml.gen @@ -13,12 +13,23 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 1.3.3 - org.acme @@ -31,7 +42,6 @@ test - diff --git a/initializr-generator-spring/src/test/resources/project/maven/compile-only-dependency-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/compile-only-dependency-pom.xml.gen index 568be247b2..7de8361e3e 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/compile-only-dependency-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/compile-only-dependency-pom.xml.gen @@ -13,11 +13,22 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/maven/kotlin-java11-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/kotlin-java11-pom.xml.gen index 6c2f432cbf..e2219aaad0 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/kotlin-java11-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/kotlin-java11-pom.xml.gen @@ -13,12 +13,23 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 11 1.1.1 - org.springframework.boot diff --git a/initializr-generator-spring/src/test/resources/project/maven/repositories-milestone-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/repositories-milestone-pom.xml.gen index 37af8650f9..dffdbeb881 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/repositories-milestone-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/repositories-milestone-pom.xml.gen @@ -13,7 +13,19 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 diff --git a/initializr-generator-spring/src/test/resources/project/maven/repositories-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/repositories-pom.xml.gen index 9a6991e82d..35c8643221 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/repositories-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/repositories-pom.xml.gen @@ -13,11 +13,22 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 - org.acme @@ -43,7 +54,6 @@ - foo-repository diff --git a/initializr-generator-spring/src/test/resources/project/maven/version-override-pom.xml.gen b/initializr-generator-spring/src/test/resources/project/maven/version-override-pom.xml.gen index 1914c97c9a..b2379baf35 100644 --- a/initializr-generator-spring/src/test/resources/project/maven/version-override-pom.xml.gen +++ b/initializr-generator-spring/src/test/resources/project/maven/version-override-pom.xml.gen @@ -13,13 +13,24 @@ 0.0.1-SNAPSHOT demo Demo project for Spring Boot - + + + + + + + + + + + + + 1.8 0.2.0.RELEASE 0.1.0.RELEASE - org.springframework.boot diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildSettings.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildSettings.java index a6940dddc9..bdde438573 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildSettings.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildSettings.java @@ -55,6 +55,10 @@ public class MavenBuildSettings extends BuildSettings { private final String testSourceDirectory; + private final String url; + + private final boolean addOverrideIfEmpty; + protected MavenBuildSettings(Builder builder) { super(builder); this.parent = builder.parent; @@ -68,6 +72,8 @@ protected MavenBuildSettings(Builder builder) { this.finalName = builder.finalName; this.sourceDirectory = builder.sourceDirectory; this.testSourceDirectory = builder.testSourceDirectory; + this.url = builder.url; + this.addOverrideIfEmpty = builder.addOverrideIfEmpty; } /** @@ -162,6 +168,23 @@ public String getTestSourceDirectory() { return this.testSourceDirectory; } + /** + * The URL to the project. + * @return the url to the project + */ + public String getUrl() { + return this.url; + } + + /** + * Whether to add overrides for empty elements so that they are not inherited from the + * parent. + * @return whether to add overrides for empty elements + */ + public boolean isAddOverrideIfEmpty() { + return this.addOverrideIfEmpty; + } + /** * Builder for {@link BuildSettings}. */ @@ -189,6 +212,10 @@ public static class Builder extends BuildSettings.Builder { private String testSourceDirectory; + private String url; + + private boolean addOverrideIfEmpty; + public Builder() { } @@ -312,7 +339,7 @@ public Builder defaultGoal(String defaultGoal) { } /** - * Set the the location of main source code. Can use Maven properties such as + * Set the location of main source code. Can use Maven properties such as * {@code ${basedir}}. * @param sourceDirectory the location of main source code or {@code null} to use * the default @@ -324,7 +351,7 @@ public Builder sourceDirectory(String sourceDirectory) { } /** - * Set the the location of test source code. Can use Maven properties such as + * Set the location of test source code. Can use Maven properties such as * {@code ${basedir}}. * @param testSourceDirectory the location of test source code or {@code null} to * use the default @@ -335,6 +362,27 @@ public Builder testSourceDirectory(String testSourceDirectory) { return self(); } + /** + * Set the url of the project. + * @param url the url of the project + * @return this for method chaining + */ + public Builder url(String url) { + this.url = url; + return self(); + } + + /** + * Whether to add overrides for empty elements so that they are not inherited from + * the parent. + * @param addOverrideIfEmpty whether to add overrides for empty elements + * @return this for method chaining + */ + public Builder addOverrideIfEmpty(boolean addOverrideIfEmpty) { + this.addOverrideIfEmpty = addOverrideIfEmpty; + return self(); + } + @Override public MavenBuildSettings build() { return new MavenBuildSettings(this); diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java index e56a749a2f..5e45164a22 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java @@ -48,6 +48,7 @@ import io.spring.initializr.generator.version.VersionProperty; import io.spring.initializr.generator.version.VersionReference; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -76,9 +77,10 @@ public void writeTo(IndentingWriter writer, MavenBuild build) { writeProjectCoordinates(writer, settings); writePackaging(writer, settings); writeProjectName(writer, settings); - writeCollectionElement(writer, "licenses", settings.getLicenses(), this::writeLicense); - writeCollectionElement(writer, "developers", settings.getDevelopers(), this::writeDeveloper); - writeScm(writer, settings.getScm()); + writeUrl(writer, settings); + writeLicenses(writer, settings); + writeDevelopers(writer, settings); + writeScm(writer, settings); writeProperties(writer, build.properties()); writeDependencies(writer, build.dependencies()); writeDependencyManagement(writer, build.boms()); @@ -152,6 +154,14 @@ private void writeProjectName(IndentingWriter writer, MavenBuildSettings setting writeSingleElement(writer, "description", settings.getDescription()); } + private void writeUrl(IndentingWriter writer, MavenBuildSettings settings) { + String url = settings.getUrl(); + if (url == null && settings.isAddOverrideIfEmpty()) { + url = ""; + } + writeSingleElement(writer, "url", url); + } + private void writeProperties(IndentingWriter writer, PropertyContainer properties) { if (properties.isEmpty()) { return; @@ -196,13 +206,48 @@ private void writeDeveloper(IndentingWriter writer, MavenDeveloper developer) { }); } - private void writeScm(IndentingWriter writer, MavenScm mavenScm) { - if (!mavenScm.isEmpty()) { + private void writeDevelopers(IndentingWriter writer, MavenBuildSettings settings) { + List developers = settings.getDevelopers(); + if (CollectionUtils.isEmpty(developers)) { + if (settings.isAddOverrideIfEmpty()) { + writeElement(writer, "developers", () -> writeSingleElement(writer, "developer", "")); + } + } + else { + writeCollectionElement(writer, "developers", settings.getDevelopers(), this::writeDeveloper); + } + } + + private void writeLicenses(IndentingWriter writer, MavenBuildSettings settings) { + List licenses = settings.getLicenses(); + if (CollectionUtils.isEmpty(licenses)) { + if (settings.isAddOverrideIfEmpty()) { + writeElement(writer, "licenses", () -> writeSingleElement(writer, "license", "")); + } + } + else { + writeCollectionElement(writer, "licenses", licenses, this::writeLicense); + } + } + + private void writeScm(IndentingWriter writer, MavenBuildSettings settings) { + MavenScm scm = settings.getScm(); + if (scm.isEmpty()) { + if (settings.isAddOverrideIfEmpty()) { + writeElement(writer, "scm", () -> { + writeSingleElement(writer, "connection", ""); + writeSingleElement(writer, "developerConnection", ""); + writeSingleElement(writer, "tag", ""); + writeSingleElement(writer, "url", ""); + }); + } + } + else { writeElement(writer, "scm", () -> { - writeSingleElement(writer, "connection", mavenScm.getConnection()); - writeSingleElement(writer, "developerConnection", mavenScm.getDeveloperConnection()); - writeSingleElement(writer, "tag", mavenScm.getTag()); - writeSingleElement(writer, "url", mavenScm.getUrl()); + writeSingleElement(writer, "connection", scm.getConnection()); + writeSingleElement(writer, "developerConnection", scm.getDeveloperConnection()); + writeSingleElement(writer, "tag", scm.getTag()); + writeSingleElement(writer, "url", scm.getUrl()); }); } }