Merge pull request #27412 from vpavic

* pr/27412:
  Polish "Allow build info properties to be excluded"
  Allow build info properties to be excluded

Closes gh-27412
This commit is contained in:
Phillip Webb
2021-10-13 21:15:07 -07:00
13 changed files with 338 additions and 21 deletions

View File

@@ -30,8 +30,7 @@ By default, the generated build information is derived from the project:
| Property | Default value
| `build.artifact`
| The base name of the `bootJar` or `bootWar` task, or `unspecified` if no such task
exists
| The base name of the `bootJar` or `bootWar` task
| `build.group`
| The group of the project
@@ -61,6 +60,8 @@ include::../gradle/integrating-with-actuator/build-info-custom-values.gradle[tag
include::../gradle/integrating-with-actuator/build-info-custom-values.gradle.kts[tags=custom-values]
----
NOTE: To omit any of the default properties from the generated build information, set its value to `null`.
The default value for `build.time` is the instant at which the project is being built.
A side-effect of this is that the task will never be up-to-date.
As a result, builds will take longer as more tasks, including the project's tests, will have to be executed.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -59,13 +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() != null) ? this.properties.getArtifact()
: "unspecified",
this.properties.getVersion(), this.properties.getName(), this.properties.getTime(),
coerceToStringValues(this.properties.getAdditional())));
.writeBuildProperties(details);
}
catch (IOException ex) {
throw new TaskExecutionException(this, ex);

View File

@@ -39,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Integration tests for the {@link BuildInfo} task.
*
* @author Andy Wilkinson
* @author Vedran Pavic
*/
@GradleCompatibility(configurationCache = true)
class BuildInfoIntegrationTests {
@@ -50,8 +51,8 @@ class BuildInfoIntegrationTests {
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
Properties buildInfoProperties = buildInfoProperties();
assertThat(buildInfoProperties).containsKey("build.time");
assertThat(buildInfoProperties).containsEntry("build.artifact", "unspecified");
assertThat(buildInfoProperties).containsEntry("build.group", "");
assertThat(buildInfoProperties).doesNotContainKey("build.artifact");
assertThat(buildInfoProperties).doesNotContainKey("build.group");
assertThat(buildInfoProperties).containsEntry("build.name", this.gradleBuild.getProjectDir().getName());
assertThat(buildInfoProperties).containsEntry("build.version", "unspecified");
}
@@ -122,6 +123,26 @@ class BuildInfoIntegrationTests {
assertThat(firstHash).isEqualTo(secondHash);
}
@TestTemplate
void removePropertiesUsingNulls() {
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
Properties buildInfoProperties = buildInfoProperties();
assertThat(buildInfoProperties).doesNotContainKey("build.group");
assertThat(buildInfoProperties).doesNotContainKey("build.artifact");
assertThat(buildInfoProperties).doesNotContainKey("build.version");
assertThat(buildInfoProperties).doesNotContainKey("build.name");
}
@TestTemplate
void removePropertiesUsingEmptyStrings() {
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
Properties buildInfoProperties = buildInfoProperties();
assertThat(buildInfoProperties).doesNotContainKey("build.group");
assertThat(buildInfoProperties).doesNotContainKey("build.artifact");
assertThat(buildInfoProperties).doesNotContainKey("build.version");
assertThat(buildInfoProperties).doesNotContainKey("build.name");
}
private Properties buildInfoProperties() {
File file = new File(this.gradleBuild.getProjectDir(), "build/build-info.properties");
assertThat(file).isFile();

View File

@@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link BuildInfo}.
*
* @author Andy Wilkinson
* @author Vedran Pavic
*/
@ClassPathExclusions("kotlin-daemon-client-*")
class BuildInfoTests {
@@ -49,8 +50,8 @@ class BuildInfoTests {
void basicExecution() {
Properties properties = buildInfoProperties(createTask(createProject("test")));
assertThat(properties).containsKey("build.time");
assertThat(properties).containsEntry("build.artifact", "unspecified");
assertThat(properties).containsEntry("build.group", "");
assertThat(properties).doesNotContainKey("build.artifact");
assertThat(properties).doesNotContainKey("build.group");
assertThat(properties).containsEntry("build.name", "test");
assertThat(properties).containsEntry("build.version", "unspecified");
}
@@ -62,6 +63,20 @@ class BuildInfoTests {
assertThat(buildInfoProperties(task)).containsEntry("build.artifact", "custom");
}
@Test
void artifactCanBeRemovedFromPropertiesUsingNull() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setArtifact(null);
assertThat(buildInfoProperties(task)).doesNotContainKey("build.artifact");
}
@Test
void artifactCanBeRemovedFromPropertiesUsingEmptyString() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setArtifact("");
assertThat(buildInfoProperties(task)).doesNotContainKey("build.artifact");
}
@Test
void projectGroupIsReflectedInProperties() {
BuildInfo task = createTask(createProject("test"));
@@ -76,6 +91,20 @@ class BuildInfoTests {
assertThat(buildInfoProperties(task)).containsEntry("build.group", "com.example");
}
@Test
void groupCanBeRemovedFromPropertiesUsingNull() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setGroup(null);
assertThat(buildInfoProperties(task)).doesNotContainKey("build.group");
}
@Test
void groupCanBeRemovedFromPropertiesUsingEmptyString() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setGroup("");
assertThat(buildInfoProperties(task)).doesNotContainKey("build.group");
}
@Test
void customNameIsReflectedInProperties() {
BuildInfo task = createTask(createProject("test"));
@@ -83,6 +112,20 @@ class BuildInfoTests {
assertThat(buildInfoProperties(task)).containsEntry("build.name", "Example");
}
@Test
void nameCanBeRemovedFromPropertiesUsingNull() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setName(null);
assertThat(buildInfoProperties(task)).doesNotContainKey("build.name");
}
@Test
void nameCanBeRemovedFromPropertiesUsingEmptyString() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setName("");
assertThat(buildInfoProperties(task)).doesNotContainKey("build.name");
}
@Test
void projectVersionIsReflectedInProperties() {
BuildInfo task = createTask(createProject("test"));
@@ -97,6 +140,20 @@ class BuildInfoTests {
assertThat(buildInfoProperties(task)).containsEntry("build.version", "2.3.4");
}
@Test
void versionCanBeRemovedFromPropertiesUsingNull() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setVersion(null);
assertThat(buildInfoProperties(task)).doesNotContainKey("build.version");
}
@Test
void versionCanBeRemovedFromPropertiesUsingEmptyString() {
BuildInfo task = createTask(createProject("test"));
task.getProperties().setVersion("");
assertThat(buildInfoProperties(task)).doesNotContainKey("build.version");
}
@Test
void timeIsSetInProperties() {
BuildInfo task = createTask(createProject("test"));

View File

@@ -0,0 +1,16 @@
plugins {
id 'org.springframework.boot' version '{version}' apply false
}
group = 'foo'
version = '0.1.0'
task buildInfo(type: org.springframework.boot.gradle.tasks.buildinfo.BuildInfo) {
destinationDir project.buildDir
properties {
group = ''
artifact = ''
version = ''
name = ''
}
}

View File

@@ -0,0 +1,16 @@
plugins {
id 'org.springframework.boot' version '{version}' apply false
}
group = 'foo'
version = '0.1.0'
task buildInfo(type: org.springframework.boot.gradle.tasks.buildinfo.BuildInfo) {
destinationDir project.buildDir
properties {
group = null
artifact = null
version = null
name = null
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -25,6 +25,7 @@ import java.util.Map;
import java.util.Properties;
import org.springframework.core.CollectionFactory;
import org.springframework.util.StringUtils;
/**
* A {@code BuildPropertiesWriter} writes the {@code build-info.properties} for
@@ -32,6 +33,7 @@ import org.springframework.core.CollectionFactory;
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.0.0
*/
public final class BuildPropertiesWriter {
@@ -71,10 +73,10 @@ public final class BuildPropertiesWriter {
protected Properties createBuildInfo(ProjectDetails project) {
Properties properties = CollectionFactory.createSortedProperties(true);
properties.put("build.group", project.getGroup());
properties.put("build.artifact", project.getArtifact());
properties.put("build.name", project.getName());
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()));
}
@@ -84,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.
*/

View File

@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Integration tests for the Maven plugin's build info support.
*
* @author Andy Wilkinson
* @author Vedran Pavic
*/
@ExtendWith(MavenBuildExtension.class)
class BuildInfoIntegrationTests {
@@ -89,6 +90,21 @@ class BuildInfoIntegrationTests {
.doesNotContainBuildTime()));
}
@TestTemplate
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()));
}
private ProjectCallback buildInfo(Consumer<AssertProvider<BuildInfoAssert>> buildInfo) {
return buildInfo("target/classes/META-INF/build-info.properties", buildInfo);
}
@@ -130,18 +146,34 @@ class BuildInfoIntegrationTests {
return containsEntry("build.group", expected);
}
BuildInfoAssert doesNotContainBuildGroup() {
return doesNotContainKey("build.group");
}
BuildInfoAssert hasBuildArtifact(String expected) {
return containsEntry("build.artifact", expected);
}
BuildInfoAssert doesNotContainBuildArtifact() {
return doesNotContainKey("build.artifact");
}
BuildInfoAssert hasBuildName(String expected) {
return containsEntry("build.name", expected);
}
BuildInfoAssert doesNotContainBuildName() {
return doesNotContainKey("build.name");
}
BuildInfoAssert hasBuildVersion(String expected) {
return containsEntry("build.version", expected);
}
BuildInfoAssert doesNotContainBuildVersion() {
return doesNotContainKey("build.version");
}
BuildInfoAssert containsBuildTime() {
return containsKey("build.time");
}

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-info-exclude-build-properties</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<name>Generate build info with excluded build properties</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<configuration>
<excludeInfoProperties>
<excludeInfoProperty>group</excludeInfoProperty>
<excludeInfoProperty>artifact</excludeInfoProperty>
<excludeInfoProperty>version</excludeInfoProperty>
<excludeInfoProperty>name</excludeInfoProperty>
</excludeInfoProperties>
</configuration>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2012-2021 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 org.test;
public class SampleApplication {
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-info-exclude-build-time</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<name>Generate build info with excluded build time</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<configuration>
<excludeInfoProperties>
<excludeInfoProperty>time</excludeInfoProperty>
</excludeInfoProperties>
</configuration>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring-framework.version@</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>@jakarta-servlet.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2012-2021 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 org.test;
public class SampleApplication {
public static void main(String[] args) {
}
}

View File

@@ -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;
@@ -41,6 +43,7 @@ import org.springframework.boot.loader.tools.BuildPropertiesWriter.ProjectDetail
* {@link MavenProject}.
*
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.4.0
*/
@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
@@ -72,7 +75,7 @@ public class BuildInfoMojo extends AbstractMojo {
* {@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}")
@@ -85,11 +88,19 @@ public class BuildInfoMojo extends AbstractMojo {
@Parameter
private Map<String, String> 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<String> excludeInfoProperties;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
ProjectDetails details = new ProjectDetails(this.project.getGroupId(), this.project.getArtifactId(),
this.project.getVersion(), this.project.getName(), getBuildTime(), this.additionalProperties);
ProjectDetails details = getProjectDetails();
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details);
this.buildContext.refresh(this.outputFile);
}
@@ -101,6 +112,29 @@ public class BuildInfoMojo extends AbstractMojo {
}
}
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<String, String> additionalProperties = applyExclusions(this.additionalProperties);
return new ProjectDetails(group, artifact, version, name, time, additionalProperties);
}
private <T> T getIfNotExcluded(String name, T value) {
return (this.excludeInfoProperties == null || !this.excludeInfoProperties.contains(name)) ? value : null;
}
private Map<String, String> applyExclusions(Map<String, String> source) {
if (source == null || this.excludeInfoProperties == null) {
return source;
}
Map<String, String> result = new LinkedHashMap<>(source);
this.excludeInfoProperties.forEach(result::remove);
return result;
}
private Instant getBuildTime() {
if (this.time == null || this.time.isEmpty()) {
Date startTime = this.session.getRequest().getStartTime();