Browse Source

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 5 years ago
parent
commit
f4bd8956af
  1. 7
      spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java
  2. 22
      spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java
  3. 20
      spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildInfoIntegrationTests.java
  4. 14
      spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-properties/pom.xml
  5. 0
      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
  6. 26
      spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-time/pom.xml
  7. 0
      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
  8. 95
      spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildInfoMojo.java

7
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 { @@ -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);

22
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 { @@ -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 { @@ -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.
*/

20
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 { @@ -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 { @@ -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()));
}

14
spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-disable-build-properties/pom.xml → spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-properties/pom.xml

@ -3,9 +3,9 @@ @@ -3,9 +3,9 @@
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-disable-build-properties</artifactId>
<artifactId>build-info-exclude-build-properties</artifactId>
<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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
@ -20,10 +20,12 @@ @@ -20,10 +20,12 @@
<executions>
<execution>
<configuration>
<group>off</group>
<artifact>off</artifact>
<version>off</version>
<name>off</name>
<excludeInfoProperties>
<excludeInfoProperty>group</excludeInfoProperty>
<excludeInfoProperty>artifact</excludeInfoProperty>
<excludeInfoProperty>version</excludeInfoProperty>
<excludeInfoProperty>name</excludeInfoProperty>
</excludeInfoProperties>
</configuration>
<goals>
<goal>build-info</goal>

0
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 → 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

26
spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-custom-build-properties/pom.xml → spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-info-exclude-build-time/pom.xml

@ -1,11 +1,11 @@ @@ -1,11 +1,11 @@
<?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">
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-custom-build-properties</artifactId>
<artifactId>build-info-exclude-build-time</artifactId>
<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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
@ -20,10 +20,9 @@ @@ -20,10 +20,9 @@
<executions>
<execution>
<configuration>
<group>test-group</group>
<artifact>test-artifact</artifact>
<version>test-version</version>
<name>test-name</name>
<excludeInfoProperties>
<excludeInfoProperty>time</excludeInfoProperty>
</excludeInfoProperties>
</configuration>
<goals>
<goal>build-info</goal>
@ -33,4 +32,17 @@ @@ -33,4 +32,17 @@
</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>

0
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 → 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

95
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; @@ -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 { @@ -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 { @@ -116,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(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 { @@ -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<String, String> 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> 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<String, String> applyExclusions(Map<String, String> source) {
if (source == null || this.excludeInfoProperties == null) {
return source;
}
return this.name;
Map<String, String> result = new LinkedHashMap<>(source);
this.excludeInfoProperties.forEach(result::remove);
return result;
}
private Instant getBuildTime() {

Loading…
Cancel
Save