Skip to content

Commit 418d692

Browse files
committed
Allow to configure build info properties.
Prior to this commit there was no way to do this through the plugin. see gh-17294
1 parent 638b4b8 commit 418d692

File tree

8 files changed

+165
-16
lines changed

8 files changed

+165
-16
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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-properties</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<name>Generate build info with custom properties</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+
<group>spring-group</group>
24+
<artifact>spring-artifact</artifact>
25+
<version>spring-version</version>
26+
<name>spring-name</name>
27+
<time>2019-07-08T08:00:00Z</time>
28+
</configuration>
29+
<goals>
30+
<goal>build-info</goal>
31+
</goals>
32+
</execution>
33+
</executions>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-context</artifactId>
41+
<version>@spring-framework.version@</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>jakarta.servlet</groupId>
45+
<artifactId>jakarta.servlet-api</artifactId>
46+
<version>@jakarta-servlet.version@</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
</dependencies>
50+
</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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import org.springframework.boot.maven.Verify
22

3-
import static org.junit.Assert.assertTrue
3+
import static org.junit.Assert.assertEquals
44

55
def file = new File(basedir, "target/classes/META-INF/build-info.properties")
66
println file.getAbsolutePath()
77
Properties properties = Verify.verifyBuildInfo(file,
8-
'org.springframework.boot.maven.it', 'build-info',
9-
'Generate build info', '0.0.1.BUILD-SNAPSHOT')
10-
assertTrue properties.containsKey('build.time')
8+
'spring-group', 'spring-artifact',
9+
'spring-name', 'spring-version')
10+
assertEquals(properties.get('build.time'), '2019-07-08T08:00:00Z')

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/build-info/pom.xml renamed to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/build-info-disable-build-time/pom.xml

Lines changed: 6 additions & 3 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</artifactId>
6+
<artifactId>build-info-disable-build-time</artifactId>
77
<version>0.0.1.BUILD-SNAPSHOT</version>
8-
<name>Generate build info</name>
8+
<name>Generate build info with disabled build time</name>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1111
<maven.compiler.source>@java.version@</maven.compiler.source>
@@ -19,6 +19,9 @@
1919
<version>@project.version@</version>
2020
<executions>
2121
<execution>
22+
<configuration>
23+
<time>off</time>
24+
</configuration>
2225
<goals>
2326
<goal>build-info</goal>
2427
</goals>
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: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,53 @@ public class BuildInfoMojo extends AbstractMojo {
4747
@Component
4848
private BuildContext buildContext;
4949

50-
/**
51-
* The Maven project.
52-
*/
53-
@Parameter(defaultValue = "${project}", readonly = true, required = true)
54-
private MavenProject project;
55-
5650
/**
5751
* The location of the generated build-info.properties.
5852
*/
5953
@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
6054
private File outputFile;
6155

56+
/**
57+
* Sets the value used for the {@code build.group} property. Defaults to the
58+
* {@link MavenProject#getGroupId() Project's group}.
59+
* @since 2.2.0
60+
*/
61+
@Parameter(defaultValue = "${project.groupId}")
62+
private String group;
63+
64+
/**
65+
* Sets the value used for the {@code build.artifact} property. Defaults to the
66+
* {@link MavenProject#getArtifactId() Project's artifact}.
67+
* @since 2.2.0
68+
*/
69+
@Parameter(defaultValue = "${project.artifactId}")
70+
private String artifact;
71+
72+
/**
73+
* Sets the value used for the {@code build.version} property. Defaults to the
74+
* {@link MavenProject#getVersion() Project's version}.
75+
* @since 2.2.0
76+
*/
77+
@Parameter(defaultValue = "${project.version}")
78+
private String version;
79+
80+
/**
81+
* Sets the value used for the {@code build.name} property. Defaults to the
82+
* {@link MavenProject#getName() Project's display name}.
83+
* @since 2.2.0
84+
*/
85+
@Parameter(defaultValue = "${project.name}")
86+
private String name;
87+
88+
/**
89+
* Sets the value used for the {@code build.time} property. Defaults to
90+
* {@link Instant#now} when the {@code mojo} instance was created. To disable
91+
* {@code build.time} property, {@code 'off'} value should be used.
92+
* @since 2.2.0
93+
*/
94+
@Parameter
95+
private String time = Instant.now().toString();
96+
6297
/**
6398
* Additional properties to store in the build-info.properties. Each entry is prefixed
6499
* by {@code build.} in the generated build-info.properties.
@@ -69,9 +104,8 @@ public class BuildInfoMojo extends AbstractMojo {
69104
@Override
70105
public void execute() throws MojoExecutionException, MojoFailureException {
71106
try {
72-
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(new ProjectDetails(
73-
this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(),
74-
this.project.getName(), Instant.now(), this.additionalProperties));
107+
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(new ProjectDetails(this.group,
108+
this.artifact, this.version, this.name, getTime(), this.additionalProperties));
75109
this.buildContext.refresh(this.outputFile);
76110
}
77111
catch (NullAdditionalPropertyValueException ex) {
@@ -82,4 +116,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
82116
}
83117
}
84118

119+
private Instant getTime() {
120+
return "off".equals(this.time) ? null : Instant.parse(this.time);
121+
}
122+
85123
}

0 commit comments

Comments
 (0)