Skip to content

Commit 8dd9c6b

Browse files
nosanwilkinsona
authored andcommitted
Allow build.time to be disabled so BuildInfoMojo's output is repeatable
See gh-17390
1 parent 1fffe0a commit 8dd9c6b

File tree

7 files changed

+174
-1
lines changed

7 files changed

+174
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>build-info-custom-build-time</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<name>Generate build info with custom build time</name>
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<maven.compiler.source>@java.version@</maven.compiler.source>
12+
<maven.compiler.target>@java.version@</maven.compiler.target>
13+
</properties>
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>@project.groupId@</groupId>
18+
<artifactId>@project.artifactId@</artifactId>
19+
<version>@project.version@</version>
20+
<executions>
21+
<execution>
22+
<configuration>
23+
<time>2019-07-08T08:00:00Z</time>
24+
</configuration>
25+
<goals>
26+
<goal>build-info</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-context</artifactId>
37+
<version>@spring-framework.version@</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>jakarta.servlet</groupId>
41+
<artifactId>jakarta.servlet-api</artifactId>
42+
<version>@jakarta-servlet.version@</version>
43+
<scope>provided</scope>
44+
</dependency>
45+
</dependencies>
46+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) {
22+
}
23+
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.springframework.boot.maven.Verify
2+
3+
import static org.junit.Assert.assertEquals
4+
5+
def file = new File(basedir, "target/classes/META-INF/build-info.properties")
6+
println file.getAbsolutePath()
7+
Properties properties = Verify.verifyBuildInfo(file,
8+
'org.springframework.boot.maven.it', 'build-info-custom-build-time',
9+
'Generate build info with custom build time', '0.0.1.BUILD-SNAPSHOT')
10+
assertEquals(properties.get('build.time'), '2019-07-08T08:00:00Z')
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>build-info-disable-build-time</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<name>Generate build info with disabled build time</name>
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<maven.compiler.source>@java.version@</maven.compiler.source>
12+
<maven.compiler.target>@java.version@</maven.compiler.target>
13+
</properties>
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>@project.groupId@</groupId>
18+
<artifactId>@project.artifactId@</artifactId>
19+
<version>@project.version@</version>
20+
<executions>
21+
<execution>
22+
<configuration>
23+
<time>off</time>
24+
</configuration>
25+
<goals>
26+
<goal>build-info</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-context</artifactId>
37+
<version>@spring-framework.version@</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>jakarta.servlet</groupId>
41+
<artifactId>jakarta.servlet-api</artifactId>
42+
<version>@jakarta-servlet.version@</version>
43+
<scope>provided</scope>
44+
</dependency>
45+
</dependencies>
46+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) {
22+
}
23+
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.springframework.boot.maven.Verify
2+
3+
import static org.junit.Assert.assertFalse
4+
5+
def file = new File(basedir, "target/classes/META-INF/build-info.properties")
6+
println file.getAbsolutePath()
7+
Properties properties = Verify.verifyBuildInfo(file,
8+
'org.springframework.boot.maven.it', 'build-info-disable-build-time',
9+
'Generate build info with disabled build time', '0.0.1.BUILD-SNAPSHOT')
10+
assertFalse 'build time must not be present', properties.containsKey('build.time')

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public class BuildInfoMojo extends AbstractMojo {
5959
@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
6060
private File outputFile;
6161

62+
/**
63+
* Sets the value used for the {@code build.time} property. Defaults to
64+
* {@link Instant#now} when the {@code mojo} instance was created. To disable
65+
* {@code build.time} property, {@code 'off'} value should be used.
66+
* @since 2.2.0
67+
*/
68+
@Parameter
69+
private String time = Instant.now().toString();
70+
6271
/**
6372
* Additional properties to store in the build-info.properties. Each entry is prefixed
6473
* by {@code build.} in the generated build-info.properties.
@@ -71,7 +80,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7180
try {
7281
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(new ProjectDetails(
7382
this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(),
74-
this.project.getName(), Instant.now(), this.additionalProperties));
83+
this.project.getName(), getBuildTime(), this.additionalProperties));
7584
this.buildContext.refresh(this.outputFile);
7685
}
7786
catch (NullAdditionalPropertyValueException ex) {
@@ -82,4 +91,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8291
}
8392
}
8493

94+
private Instant getBuildTime() {
95+
return "off".equals(this.time) ? null : Instant.parse(this.time);
96+
}
97+
8598
}

0 commit comments

Comments
 (0)