Skip to content

Commit f4bd895

Browse files
committed
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
1 parent ea9faf8 commit f4bd895

File tree

8 files changed

+86
-98
lines changed

8 files changed

+86
-98
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ public BuildInfo() {
5959
@TaskAction
6060
public void generateBuildProperties() {
6161
try {
62+
ProjectDetails details = new ProjectDetails(this.properties.getGroup(), this.properties.getArtifact(),
63+
this.properties.getVersion(), this.properties.getName(), this.properties.getTime(),
64+
coerceToStringValues(this.properties.getAdditional()));
6265
new BuildPropertiesWriter(new File(getDestinationDir(), "build-info.properties"))
63-
.writeBuildProperties(new ProjectDetails(this.properties.getGroup(), this.properties.getArtifact(),
64-
this.properties.getVersion(), this.properties.getName(), this.properties.getTime(),
65-
coerceToStringValues(this.properties.getAdditional())));
66+
.writeBuildProperties(details);
6667
}
6768
catch (IOException ex) {
6869
throw new TaskExecutionException(this, ex);

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,10 @@ private void createFileIfNecessary(File file) throws IOException {
7373

7474
protected Properties createBuildInfo(ProjectDetails project) {
7575
Properties properties = CollectionFactory.createSortedProperties(true);
76-
if (StringUtils.hasText(project.getGroup())) {
77-
properties.put("build.group", project.getGroup());
78-
}
79-
if (StringUtils.hasText(project.getArtifact())) {
80-
properties.put("build.artifact", project.getArtifact());
81-
}
82-
if (StringUtils.hasText(project.getName())) {
83-
properties.put("build.name", project.getName());
84-
}
85-
if (StringUtils.hasText(project.getVersion())) {
86-
properties.put("build.version", project.getVersion());
87-
}
76+
addIfHasValue(properties, "build.group", project.getGroup());
77+
addIfHasValue(properties, "build.artifact", project.getArtifact());
78+
addIfHasValue(properties, "build.name", project.getName());
79+
addIfHasValue(properties, "build.version", project.getVersion());
8880
if (project.getTime() != null) {
8981
properties.put("build.time", DateTimeFormatter.ISO_INSTANT.format(project.getTime()));
9082
}
@@ -94,6 +86,12 @@ protected Properties createBuildInfo(ProjectDetails project) {
9486
return properties;
9587
}
9688

89+
private void addIfHasValue(Properties properties, String name, String value) {
90+
if (StringUtils.hasText(value)) {
91+
properties.put(name, value);
92+
}
93+
}
94+
9795
/**
9896
* Build-system agnostic details of a project.
9997
*/

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildInfoIntegrationTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ void generatedBuildInfoUsesCustomBuildTime(MavenBuild mavenBuild) {
6464
.hasBuildTime("2019-07-08T08:00:00Z")));
6565
}
6666

67-
@TestTemplate
68-
void generatedBuildInfoUsesCustomBuildProperties(MavenBuild mavenBuild) {
69-
mavenBuild.project("build-info-custom-build-properties")
70-
.execute(buildInfo((buildInfo) -> assertThat(buildInfo).hasBuildGroup("test-group")
71-
.hasBuildArtifact("test-artifact").hasBuildName("test-name").hasBuildVersion("test-version")
72-
.containsBuildTime()));
73-
}
74-
7567
@TestTemplate
7668
void generatedBuildInfoReproducible(MavenBuild mavenBuild) {
7769
mavenBuild.project("build-info-reproducible")
@@ -99,8 +91,16 @@ void whenBuildTimeIsDisabledIfDoesNotAppearInGeneratedBuildInfo(MavenBuild maven
9991
}
10092

10193
@TestTemplate
102-
void whenBuildPropertiesAreEmptyTheyDoNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) {
103-
mavenBuild.project("build-info-disable-build-properties").execute(
94+
void whenBuildTimeIsExcludedIfDoesNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) {
95+
mavenBuild.project("build-info-exclude-build-time").execute(buildInfo((buildInfo) -> assertThat(buildInfo)
96+
.hasBuildGroup("org.springframework.boot.maven.it").hasBuildArtifact("build-info-exclude-build-time")
97+
.hasBuildName("Generate build info with excluded build time").hasBuildVersion("0.0.1.BUILD-SNAPSHOT")
98+
.doesNotContainBuildTime()));
99+
}
100+
101+
@TestTemplate
102+
void whenBuildPropertiesAreExcludedTheyDoNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) {
103+
mavenBuild.project("build-info-exclude-build-properties").execute(
104104
buildInfo((buildInfo) -> assertThat(buildInfo).doesNotContainBuildGroup().doesNotContainBuildArtifact()
105105
.doesNotContainBuildName().doesNotContainBuildVersion().containsBuildTime()));
106106
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.springframework.boot.maven.it</groupId>
6-
<artifactId>build-info-disable-build-properties</artifactId>
6+
<artifactId>build-info-exclude-build-properties</artifactId>
77
<version>0.0.1.BUILD-SNAPSHOT</version>
8-
<name>Generate build info with disabled build properties</name>
8+
<name>Generate build info with excluded build properties</name>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1111
<maven.compiler.source>@java.version@</maven.compiler.source>
@@ -20,10 +20,12 @@
2020
<executions>
2121
<execution>
2222
<configuration>
23-
<group>off</group>
24-
<artifact>off</artifact>
25-
<version>off</version>
26-
<name>off</name>
23+
<excludeInfoProperties>
24+
<excludeInfoProperty>group</excludeInfoProperty>
25+
<excludeInfoProperty>artifact</excludeInfoProperty>
26+
<excludeInfoProperty>version</excludeInfoProperty>
27+
<excludeInfoProperty>name</excludeInfoProperty>
28+
</excludeInfoProperties>
2729
</configuration>
2830
<goals>
2931
<goal>build-info</goal>
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.springframework.boot.maven.it</groupId>
6-
<artifactId>build-info-custom-build-properties</artifactId>
6+
<artifactId>build-info-exclude-build-time</artifactId>
77
<version>0.0.1.BUILD-SNAPSHOT</version>
8-
<name>Generate build info with custom build properties</name>
8+
<name>Generate build info with excluded build time</name>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1111
<maven.compiler.source>@java.version@</maven.compiler.source>
@@ -20,10 +20,9 @@
2020
<executions>
2121
<execution>
2222
<configuration>
23-
<group>test-group</group>
24-
<artifact>test-artifact</artifact>
25-
<version>test-version</version>
26-
<name>test-name</name>
23+
<excludeInfoProperties>
24+
<excludeInfoProperty>time</excludeInfoProperty>
25+
</excludeInfoProperties>
2726
</configuration>
2827
<goals>
2928
<goal>build-info</goal>
@@ -33,4 +32,17 @@
3332
</plugin>
3433
</plugins>
3534
</build>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-context</artifactId>
39+
<version>@spring-framework.version@</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>jakarta.servlet</groupId>
43+
<artifactId>jakarta.servlet-api</artifactId>
44+
<version>@jakarta-servlet.version@</version>
45+
<scope>provided</scope>
46+
</dependency>
47+
</dependencies>
3648
</project>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildInfoMojo.java

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.File;
2020
import java.time.Instant;
2121
import java.util.Date;
22+
import java.util.LinkedHashMap;
23+
import java.util.List;
2224
import java.util.Map;
2325

2426
import org.apache.maven.execution.MavenSession;
@@ -57,53 +59,23 @@ public class BuildInfoMojo extends AbstractMojo {
5759
private MavenSession session;
5860

5961
/**
60-
* The location of the generated {@code build-info.properties} file.
61-
*/
62-
@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
63-
private File outputFile;
64-
65-
/**
66-
* The value used for the {@code build.group} property. Defaults to
67-
* {@code project.groupId}. To disable the {@code build.group} property entirely, use
68-
* {@code 'off'}.
69-
* @since 2.6.0
70-
*/
71-
@Parameter(defaultValue = "${project.groupId}")
72-
private String group;
73-
74-
/**
75-
* The value used for the {@code build.artifact} property. Defaults to
76-
* {@code project.artifactId}. To disable the {@code build.artifact} property
77-
* entirely, use {@code 'off'}.
78-
* @since 2.6.0
62+
* The Maven project.
7963
*/
80-
@Parameter(defaultValue = "${project.artifactId}")
81-
private String artifact;
64+
@Parameter(defaultValue = "${project}", readonly = true, required = true)
65+
private MavenProject project;
8266

8367
/**
84-
* The value used for the {@code build.version} property. Defaults to
85-
* {@code project.version}. To disable the {@code build.version} property entirely,
86-
* use {@code 'off'}.
87-
* @since 2.6.0
88-
*/
89-
@Parameter(defaultValue = "${project.version}")
90-
private String version;
91-
92-
/**
93-
* The value used for the {@code build.name} property. Defaults to
94-
* {@code project.name}. To disable the {@code build.name} property entirely, use
95-
* {@code 'off'}.
96-
* @since 2.6.0
68+
* The location of the generated {@code build-info.properties} file.
9769
*/
98-
@Parameter(defaultValue = "${project.name}")
99-
private String name;
70+
@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
71+
private File outputFile;
10072

10173
/**
10274
* The value used for the {@code build.time} property in a form suitable for
10375
* {@link Instant#parse(CharSequence)}. Defaults to
10476
* {@code project.build.outputTimestamp} or {@code session.request.startTime} if the
10577
* former is not set. To disable the {@code build.time} property entirely, use
106-
* {@code 'off'}.
78+
* {@code 'off'} or add it to {@code excludeInfoProperties}.
10779
* @since 2.2.0
10880
*/
10981
@Parameter(defaultValue = "${project.build.outputTimestamp}")
@@ -116,11 +88,19 @@ public class BuildInfoMojo extends AbstractMojo {
11688
@Parameter
11789
private Map<String, String> additionalProperties;
11890

91+
/**
92+
* Properties that should be excluded {@code build-info.properties} file. Can be used
93+
* to exclude the standard {@code group}, {@code artifact}, {@code name},
94+
* {@code version} or {@code time} properties as well as items from
95+
* {@code additionalProperties}.
96+
*/
97+
@Parameter
98+
private List<String> excludeInfoProperties;
99+
119100
@Override
120101
public void execute() throws MojoExecutionException, MojoFailureException {
121102
try {
122-
ProjectDetails details = new ProjectDetails(getGroup(), getArtifact(), getVersion(), getName(),
123-
getBuildTime(), this.additionalProperties);
103+
ProjectDetails details = getProjectDetails();
124104
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details);
125105
this.buildContext.refresh(this.outputFile);
126106
}
@@ -132,32 +112,27 @@ public void execute() throws MojoExecutionException, MojoFailureException {
132112
}
133113
}
134114

135-
private String getGroup() {
136-
if ("off".equalsIgnoreCase(this.group)) {
137-
return null;
138-
}
139-
return this.group;
140-
}
141-
142-
private String getArtifact() {
143-
if ("off".equalsIgnoreCase(this.artifact)) {
144-
return null;
145-
}
146-
return this.artifact;
115+
private ProjectDetails getProjectDetails() {
116+
String group = getIfNotExcluded("group", this.project.getGroupId());
117+
String artifact = getIfNotExcluded("artifact", this.project.getArtifactId());
118+
String version = getIfNotExcluded("version", this.project.getVersion());
119+
String name = getIfNotExcluded("name", this.project.getName());
120+
Instant time = getIfNotExcluded("time", getBuildTime());
121+
Map<String, String> additionalProperties = applyExclusions(this.additionalProperties);
122+
return new ProjectDetails(group, artifact, version, name, time, additionalProperties);
147123
}
148124

149-
private String getVersion() {
150-
if ("off".equalsIgnoreCase(this.version)) {
151-
return null;
152-
}
153-
return this.version;
125+
private <T> T getIfNotExcluded(String name, T value) {
126+
return (this.excludeInfoProperties == null || !this.excludeInfoProperties.contains(name)) ? value : null;
154127
}
155128

156-
private String getName() {
157-
if ("off".equalsIgnoreCase(this.name)) {
158-
return null;
129+
private Map<String, String> applyExclusions(Map<String, String> source) {
130+
if (source == null || this.excludeInfoProperties == null) {
131+
return source;
159132
}
160-
return this.name;
133+
Map<String, String> result = new LinkedHashMap<>(source);
134+
this.excludeInfoProperties.forEach(result::remove);
135+
return result;
161136
}
162137

163138
private Instant getBuildTime() {

0 commit comments

Comments
 (0)