From f4bd8956afe5ffd3dd7fb668da8fa283661991e2 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 13 Oct 2021 14:04:13 -0700 Subject: [PATCH] Polish "Allow build info properties to be excluded" Update the Maven plugin to use an alternative syntax to exclude the info properties and apply some minor polishing. See gh-27412 --- .../gradle/tasks/buildinfo/BuildInfo.java | 7 +- .../loader/tools/BuildPropertiesWriter.java | 22 ++--- .../boot/maven/BuildInfoIntegrationTests.java | 20 ++-- .../pom.xml | 14 +-- .../main/java/org/test/SampleApplication.java | 0 .../pom.xml | 26 +++-- .../main/java/org/test/SampleApplication.java | 0 .../boot/maven/BuildInfoMojo.java | 95 +++++++------------ 8 files changed, 86 insertions(+), 98 deletions(-) rename spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/{build-info-disable-build-properties => build-info-exclude-build-properties}/pom.xml (70%) rename spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/{build-info-custom-build-properties => build-info-exclude-build-properties}/src/main/java/org/test/SampleApplication.java (100%) rename spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/{build-info-custom-build-properties => build-info-exclude-build-time}/pom.xml (54%) rename spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/{build-info-disable-build-properties => build-info-exclude-build-time}/src/main/java/org/test/SampleApplication.java (100%) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java index b5cd12aa3b..f48226e5d8 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java @@ -59,10 +59,11 @@ public class BuildInfo extends ConventionTask { @TaskAction public void generateBuildProperties() { try { + ProjectDetails details = new ProjectDetails(this.properties.getGroup(), this.properties.getArtifact(), + this.properties.getVersion(), this.properties.getName(), this.properties.getTime(), + coerceToStringValues(this.properties.getAdditional())); new BuildPropertiesWriter(new File(getDestinationDir(), "build-info.properties")) - .writeBuildProperties(new ProjectDetails(this.properties.getGroup(), this.properties.getArtifact(), - this.properties.getVersion(), this.properties.getName(), this.properties.getTime(), - coerceToStringValues(this.properties.getAdditional()))); + .writeBuildProperties(details); } catch (IOException ex) { throw new TaskExecutionException(this, ex); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java index a250cee1bc..fe103f2101 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java @@ -73,18 +73,10 @@ public final class BuildPropertiesWriter { protected Properties createBuildInfo(ProjectDetails project) { Properties properties = CollectionFactory.createSortedProperties(true); - if (StringUtils.hasText(project.getGroup())) { - properties.put("build.group", project.getGroup()); - } - if (StringUtils.hasText(project.getArtifact())) { - properties.put("build.artifact", project.getArtifact()); - } - if (StringUtils.hasText(project.getName())) { - properties.put("build.name", project.getName()); - } - if (StringUtils.hasText(project.getVersion())) { - properties.put("build.version", project.getVersion()); - } + addIfHasValue(properties, "build.group", project.getGroup()); + addIfHasValue(properties, "build.artifact", project.getArtifact()); + addIfHasValue(properties, "build.name", project.getName()); + addIfHasValue(properties, "build.version", project.getVersion()); if (project.getTime() != null) { properties.put("build.time", DateTimeFormatter.ISO_INSTANT.format(project.getTime())); } @@ -94,6 +86,12 @@ public final class BuildPropertiesWriter { return properties; } + private void addIfHasValue(Properties properties, String name, String value) { + if (StringUtils.hasText(value)) { + properties.put(name, value); + } + } + /** * Build-system agnostic details of a project. */ diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildInfoIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildInfoIntegrationTests.java index 26982cae39..3c648a0d98 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildInfoIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildInfoIntegrationTests.java @@ -64,14 +64,6 @@ class BuildInfoIntegrationTests { .hasBuildTime("2019-07-08T08:00:00Z"))); } - @TestTemplate - void generatedBuildInfoUsesCustomBuildProperties(MavenBuild mavenBuild) { - mavenBuild.project("build-info-custom-build-properties") - .execute(buildInfo((buildInfo) -> assertThat(buildInfo).hasBuildGroup("test-group") - .hasBuildArtifact("test-artifact").hasBuildName("test-name").hasBuildVersion("test-version") - .containsBuildTime())); - } - @TestTemplate void generatedBuildInfoReproducible(MavenBuild mavenBuild) { mavenBuild.project("build-info-reproducible") @@ -99,8 +91,16 @@ class BuildInfoIntegrationTests { } @TestTemplate - void whenBuildPropertiesAreEmptyTheyDoNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) { - mavenBuild.project("build-info-disable-build-properties").execute( + void whenBuildTimeIsExcludedIfDoesNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) { + mavenBuild.project("build-info-exclude-build-time").execute(buildInfo((buildInfo) -> assertThat(buildInfo) + .hasBuildGroup("org.springframework.boot.maven.it").hasBuildArtifact("build-info-exclude-build-time") + .hasBuildName("Generate build info with excluded build time").hasBuildVersion("0.0.1.BUILD-SNAPSHOT") + .doesNotContainBuildTime())); + } + + @TestTemplate + void whenBuildPropertiesAreExcludedTheyDoNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) { + mavenBuild.project("build-info-exclude-build-properties").execute( buildInfo((buildInfo) -> assertThat(buildInfo).doesNotContainBuildGroup().doesNotContainBuildArtifact() .doesNotContainBuildName().doesNotContainBuildVersion().containsBuildTime())); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-disable-build-properties/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-properties/pom.xml similarity index 70% rename from spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-disable-build-properties/pom.xml rename to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-properties/pom.xml index 095515d796..f58f33a49a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-disable-build-properties/pom.xml +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-properties/pom.xml @@ -3,9 +3,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot.maven.it - build-info-disable-build-properties + build-info-exclude-build-properties 0.0.1.BUILD-SNAPSHOT - Generate build info with disabled build properties + Generate build info with excluded build properties UTF-8 @java.version@ @@ -20,10 +20,12 @@ - off - off - off - off + + group + artifact + version + name + build-info diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-custom-build-properties/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-properties/src/main/java/org/test/SampleApplication.java similarity index 100% rename from spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-custom-build-properties/src/main/java/org/test/SampleApplication.java rename to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-properties/src/main/java/org/test/SampleApplication.java diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-custom-build-properties/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-time/pom.xml similarity index 54% rename from spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-custom-build-properties/pom.xml rename to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-time/pom.xml index d8fddf6223..e2d702508e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-custom-build-properties/pom.xml +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-time/pom.xml @@ -1,11 +1,11 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot.maven.it - build-info-custom-build-properties + build-info-exclude-build-time 0.0.1.BUILD-SNAPSHOT - Generate build info with custom build properties + Generate build info with excluded build time UTF-8 @java.version@ @@ -20,10 +20,9 @@ - test-group - test-artifact - test-version - test-name + + time + build-info @@ -33,4 +32,17 @@ + + + org.springframework + spring-context + @spring-framework.version@ + + + jakarta.servlet + jakarta.servlet-api + @jakarta-servlet.version@ + provided + + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-disable-build-properties/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-time/src/main/java/org/test/SampleApplication.java similarity index 100% rename from spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-disable-build-properties/src/main/java/org/test/SampleApplication.java rename to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-time/src/main/java/org/test/SampleApplication.java diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildInfoMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildInfoMojo.java index a2a6d3c9d5..c4b3a111ae 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildInfoMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildInfoMojo.java @@ -19,6 +19,8 @@ package org.springframework.boot.maven; import java.io.File; import java.time.Instant; import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import org.apache.maven.execution.MavenSession; @@ -57,53 +59,23 @@ public class BuildInfoMojo extends AbstractMojo { private MavenSession session; /** - * The location of the generated {@code build-info.properties} file. - */ - @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties") - private File outputFile; - - /** - * The value used for the {@code build.group} property. Defaults to - * {@code project.groupId}. To disable the {@code build.group} property entirely, use - * {@code 'off'}. - * @since 2.6.0 - */ - @Parameter(defaultValue = "${project.groupId}") - private String group; - - /** - * The value used for the {@code build.artifact} property. Defaults to - * {@code project.artifactId}. To disable the {@code build.artifact} property - * entirely, use {@code 'off'}. - * @since 2.6.0 + * The Maven project. */ - @Parameter(defaultValue = "${project.artifactId}") - private String artifact; + @Parameter(defaultValue = "${project}", readonly = true, required = true) + private MavenProject project; /** - * The value used for the {@code build.version} property. Defaults to - * {@code project.version}. To disable the {@code build.version} property entirely, - * use {@code 'off'}. - * @since 2.6.0 - */ - @Parameter(defaultValue = "${project.version}") - private String version; - - /** - * The value used for the {@code build.name} property. Defaults to - * {@code project.name}. To disable the {@code build.name} property entirely, use - * {@code 'off'}. - * @since 2.6.0 + * The location of the generated {@code build-info.properties} file. */ - @Parameter(defaultValue = "${project.name}") - private String name; + @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties") + private File outputFile; /** * The value used for the {@code build.time} property in a form suitable for * {@link Instant#parse(CharSequence)}. Defaults to * {@code project.build.outputTimestamp} or {@code session.request.startTime} if the * former is not set. To disable the {@code build.time} property entirely, use - * {@code 'off'}. + * {@code 'off'} or add it to {@code excludeInfoProperties}. * @since 2.2.0 */ @Parameter(defaultValue = "${project.build.outputTimestamp}") @@ -116,11 +88,19 @@ public class BuildInfoMojo extends AbstractMojo { @Parameter private Map additionalProperties; + /** + * Properties that should be excluded {@code build-info.properties} file. Can be used + * to exclude the standard {@code group}, {@code artifact}, {@code name}, + * {@code version} or {@code time} properties as well as items from + * {@code additionalProperties}. + */ + @Parameter + private List excludeInfoProperties; + @Override public void execute() throws MojoExecutionException, MojoFailureException { try { - ProjectDetails details = new ProjectDetails(getGroup(), getArtifact(), getVersion(), getName(), - getBuildTime(), this.additionalProperties); + ProjectDetails details = getProjectDetails(); new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details); this.buildContext.refresh(this.outputFile); } @@ -132,32 +112,27 @@ public class BuildInfoMojo extends AbstractMojo { } } - private String getGroup() { - if ("off".equalsIgnoreCase(this.group)) { - return null; - } - return this.group; - } - - private String getArtifact() { - if ("off".equalsIgnoreCase(this.artifact)) { - return null; - } - return this.artifact; + private ProjectDetails getProjectDetails() { + String group = getIfNotExcluded("group", this.project.getGroupId()); + String artifact = getIfNotExcluded("artifact", this.project.getArtifactId()); + String version = getIfNotExcluded("version", this.project.getVersion()); + String name = getIfNotExcluded("name", this.project.getName()); + Instant time = getIfNotExcluded("time", getBuildTime()); + Map additionalProperties = applyExclusions(this.additionalProperties); + return new ProjectDetails(group, artifact, version, name, time, additionalProperties); } - private String getVersion() { - if ("off".equalsIgnoreCase(this.version)) { - return null; - } - return this.version; + private T getIfNotExcluded(String name, T value) { + return (this.excludeInfoProperties == null || !this.excludeInfoProperties.contains(name)) ? value : null; } - private String getName() { - if ("off".equalsIgnoreCase(this.name)) { - return null; + private Map applyExclusions(Map source) { + if (source == null || this.excludeInfoProperties == null) { + return source; } - return this.name; + Map result = new LinkedHashMap<>(source); + this.excludeInfoProperties.forEach(result::remove); + return result; } private Instant getBuildTime() {