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
pull/28344/head
Phillip Webb 3 years ago
parent ea9faf8690
commit f4bd8956af

@ -59,10 +59,11 @@ public class BuildInfo extends ConventionTask {
@TaskAction @TaskAction
public void generateBuildProperties() { public void generateBuildProperties() {
try { 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")) new BuildPropertiesWriter(new File(getDestinationDir(), "build-info.properties"))
.writeBuildProperties(new ProjectDetails(this.properties.getGroup(), this.properties.getArtifact(), .writeBuildProperties(details);
this.properties.getVersion(), this.properties.getName(), this.properties.getTime(),
coerceToStringValues(this.properties.getAdditional())));
} }
catch (IOException ex) { catch (IOException ex) {
throw new TaskExecutionException(this, ex); throw new TaskExecutionException(this, ex);

@ -73,18 +73,10 @@ public final class BuildPropertiesWriter {
protected Properties createBuildInfo(ProjectDetails project) { protected Properties createBuildInfo(ProjectDetails project) {
Properties properties = CollectionFactory.createSortedProperties(true); Properties properties = CollectionFactory.createSortedProperties(true);
if (StringUtils.hasText(project.getGroup())) { addIfHasValue(properties, "build.group", project.getGroup());
properties.put("build.group", project.getGroup()); addIfHasValue(properties, "build.artifact", project.getArtifact());
} addIfHasValue(properties, "build.name", project.getName());
if (StringUtils.hasText(project.getArtifact())) { addIfHasValue(properties, "build.version", project.getVersion());
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());
}
if (project.getTime() != null) { if (project.getTime() != null) {
properties.put("build.time", DateTimeFormatter.ISO_INSTANT.format(project.getTime())); properties.put("build.time", DateTimeFormatter.ISO_INSTANT.format(project.getTime()));
} }
@ -94,6 +86,12 @@ public final class BuildPropertiesWriter {
return properties; 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. * Build-system agnostic details of a project.
*/ */

@ -64,14 +64,6 @@ class BuildInfoIntegrationTests {
.hasBuildTime("2019-07-08T08:00:00Z"))); .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 @TestTemplate
void generatedBuildInfoReproducible(MavenBuild mavenBuild) { void generatedBuildInfoReproducible(MavenBuild mavenBuild) {
mavenBuild.project("build-info-reproducible") mavenBuild.project("build-info-reproducible")
@ -99,8 +91,16 @@ class BuildInfoIntegrationTests {
} }
@TestTemplate @TestTemplate
void whenBuildPropertiesAreEmptyTheyDoNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) { void whenBuildTimeIsExcludedIfDoesNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) {
mavenBuild.project("build-info-disable-build-properties").execute( 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() buildInfo((buildInfo) -> assertThat(buildInfo).doesNotContainBuildGroup().doesNotContainBuildArtifact()
.doesNotContainBuildName().doesNotContainBuildVersion().containsBuildTime())); .doesNotContainBuildName().doesNotContainBuildVersion().containsBuildTime()));
} }

@ -3,9 +3,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId> <groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-info-disable-build-properties</artifactId> <artifactId>build-info-exclude-build-properties</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version> <version>0.0.1.BUILD-SNAPSHOT</version>
<name>Generate build info with disabled build properties</name> <name>Generate build info with excluded build properties</name>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source> <maven.compiler.source>@java.version@</maven.compiler.source>
@ -20,10 +20,12 @@
<executions> <executions>
<execution> <execution>
<configuration> <configuration>
<group>off</group> <excludeInfoProperties>
<artifact>off</artifact> <excludeInfoProperty>group</excludeInfoProperty>
<version>off</version> <excludeInfoProperty>artifact</excludeInfoProperty>
<name>off</name> <excludeInfoProperty>version</excludeInfoProperty>
<excludeInfoProperty>name</excludeInfoProperty>
</excludeInfoProperties>
</configuration> </configuration>
<goals> <goals>
<goal>build-info</goal> <goal>build-info</goal>

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?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" <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"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId> <groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-info-custom-build-properties</artifactId> <artifactId>build-info-exclude-build-time</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version> <version>0.0.1.BUILD-SNAPSHOT</version>
<name>Generate build info with custom build properties</name> <name>Generate build info with excluded build time</name>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source> <maven.compiler.source>@java.version@</maven.compiler.source>
@ -20,10 +20,9 @@
<executions> <executions>
<execution> <execution>
<configuration> <configuration>
<group>test-group</group> <excludeInfoProperties>
<artifact>test-artifact</artifact> <excludeInfoProperty>time</excludeInfoProperty>
<version>test-version</version> </excludeInfoProperties>
<name>test-name</name>
</configuration> </configuration>
<goals> <goals>
<goal>build-info</goal> <goal>build-info</goal>
@ -33,4 +32,17 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </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> </project>

@ -19,6 +19,8 @@ package org.springframework.boot.maven;
import java.io.File; import java.io.File;
import java.time.Instant; import java.time.Instant;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
@ -57,53 +59,23 @@ public class BuildInfoMojo extends AbstractMojo {
private MavenSession session; private MavenSession session;
/** /**
* The location of the generated {@code build-info.properties} file. * The Maven project.
*/
@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
*/ */
@Parameter(defaultValue = "${project.artifactId}") @Parameter(defaultValue = "${project}", readonly = true, required = true)
private String artifact; private MavenProject project;
/** /**
* The value used for the {@code build.version} property. Defaults to * The location of the generated {@code build-info.properties} file.
* {@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
*/ */
@Parameter(defaultValue = "${project.name}") @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
private String name; private File outputFile;
/** /**
* The value used for the {@code build.time} property in a form suitable for * The value used for the {@code build.time} property in a form suitable for
* {@link Instant#parse(CharSequence)}. Defaults to * {@link Instant#parse(CharSequence)}. Defaults to
* {@code project.build.outputTimestamp} or {@code session.request.startTime} if the * {@code project.build.outputTimestamp} or {@code session.request.startTime} if the
* former is not set. To disable the {@code build.time} property entirely, use * 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 * @since 2.2.0
*/ */
@Parameter(defaultValue = "${project.build.outputTimestamp}") @Parameter(defaultValue = "${project.build.outputTimestamp}")
@ -116,11 +88,19 @@ public class BuildInfoMojo extends AbstractMojo {
@Parameter @Parameter
private Map<String, String> additionalProperties; 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 @Override
public void execute() throws MojoExecutionException, MojoFailureException { public void execute() throws MojoExecutionException, MojoFailureException {
try { try {
ProjectDetails details = new ProjectDetails(getGroup(), getArtifact(), getVersion(), getName(), ProjectDetails details = getProjectDetails();
getBuildTime(), this.additionalProperties);
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details); new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details);
this.buildContext.refresh(this.outputFile); this.buildContext.refresh(this.outputFile);
} }
@ -132,32 +112,27 @@ public class BuildInfoMojo extends AbstractMojo {
} }
} }
private String getGroup() { private ProjectDetails getProjectDetails() {
if ("off".equalsIgnoreCase(this.group)) { String group = getIfNotExcluded("group", this.project.getGroupId());
return null; String artifact = getIfNotExcluded("artifact", this.project.getArtifactId());
} String version = getIfNotExcluded("version", this.project.getVersion());
return this.group; String name = getIfNotExcluded("name", this.project.getName());
} Instant time = getIfNotExcluded("time", getBuildTime());
Map<String, String> additionalProperties = applyExclusions(this.additionalProperties);
private String getArtifact() { return new ProjectDetails(group, artifact, version, name, time, additionalProperties);
if ("off".equalsIgnoreCase(this.artifact)) {
return null;
}
return this.artifact;
} }
private String getVersion() { private <T> T getIfNotExcluded(String name, T value) {
if ("off".equalsIgnoreCase(this.version)) { return (this.excludeInfoProperties == null || !this.excludeInfoProperties.contains(name)) ? value : null;
return null;
}
return this.version;
} }
private String getName() { private Map<String, String> applyExclusions(Map<String, String> source) {
if ("off".equalsIgnoreCase(this.name)) { if (source == null || this.excludeInfoProperties == null) {
return null; return source;
} }
return this.name; Map<String, String> result = new LinkedHashMap<>(source);
this.excludeInfoProperties.forEach(result::remove);
return result;
} }
private Instant getBuildTime() { private Instant getBuildTime() {

Loading…
Cancel
Save