Allow build info properties to be excluded
Update Maven and Gradle plugins to allow build info properties to be excluded. Prior to this commit, the `BuildPropertiesWriter` would fail with an NPE if the group, artifact, name or version properties were `null`. This was specifically problematic with the Gradle plugin, since its DSL allows `null` properties which would either be passed to the writer or, in the case of `artifact`, converted into a string value of "unspecified". See gh-27412
This commit is contained in:
committed by
Phillip Webb
parent
2034ad4827
commit
ea9faf8690
@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Integration tests for the Maven plugin's build info support.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
@ExtendWith(MavenBuildExtension.class)
|
||||
class BuildInfoIntegrationTests {
|
||||
@@ -63,6 +64,14 @@ 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")
|
||||
@@ -89,6 +98,13 @@ class BuildInfoIntegrationTests {
|
||||
.doesNotContainBuildTime()));
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void whenBuildPropertiesAreEmptyTheyDoNotAppearInGeneratedBuildInfo(MavenBuild mavenBuild) {
|
||||
mavenBuild.project("build-info-disable-build-properties").execute(
|
||||
buildInfo((buildInfo) -> assertThat(buildInfo).doesNotContainBuildGroup().doesNotContainBuildArtifact()
|
||||
.doesNotContainBuildName().doesNotContainBuildVersion().containsBuildTime()));
|
||||
}
|
||||
|
||||
private ProjectCallback buildInfo(Consumer<AssertProvider<BuildInfoAssert>> buildInfo) {
|
||||
return buildInfo("target/classes/META-INF/build-info.properties", buildInfo);
|
||||
}
|
||||
@@ -130,18 +146,34 @@ class BuildInfoIntegrationTests {
|
||||
return containsEntry("build.group", expected);
|
||||
}
|
||||
|
||||
BuildInfoAssert doesNotContainBuildGroup() {
|
||||
return doesNotContainKey("build.group");
|
||||
}
|
||||
|
||||
BuildInfoAssert hasBuildArtifact(String expected) {
|
||||
return containsEntry("build.artifact", expected);
|
||||
}
|
||||
|
||||
BuildInfoAssert doesNotContainBuildArtifact() {
|
||||
return doesNotContainKey("build.artifact");
|
||||
}
|
||||
|
||||
BuildInfoAssert hasBuildName(String expected) {
|
||||
return containsEntry("build.name", expected);
|
||||
}
|
||||
|
||||
BuildInfoAssert doesNotContainBuildName() {
|
||||
return doesNotContainKey("build.name");
|
||||
}
|
||||
|
||||
BuildInfoAssert hasBuildVersion(String expected) {
|
||||
return containsEntry("build.version", expected);
|
||||
}
|
||||
|
||||
BuildInfoAssert doesNotContainBuildVersion() {
|
||||
return doesNotContainKey("build.version");
|
||||
}
|
||||
|
||||
BuildInfoAssert containsBuildTime() {
|
||||
return containsKey("build.time");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.boot.maven.it</groupId>
|
||||
<artifactId>build-info-custom-build-properties</artifactId>
|
||||
<version>0.0.1.BUILD-SNAPSHOT</version>
|
||||
<name>Generate build info with custom build properties</name>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>@java.version@</maven.compiler.source>
|
||||
<maven.compiler.target>@java.version@</maven.compiler.target>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>@project.groupId@</groupId>
|
||||
<artifactId>@project.artifactId@</artifactId>
|
||||
<version>@project.version@</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<group>test-group</group>
|
||||
<artifact>test-artifact</artifact>
|
||||
<version>test-version</version>
|
||||
<name>test-name</name>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>build-info</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.test;
|
||||
|
||||
public class SampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.boot.maven.it</groupId>
|
||||
<artifactId>build-info-disable-build-properties</artifactId>
|
||||
<version>0.0.1.BUILD-SNAPSHOT</version>
|
||||
<name>Generate build info with disabled build properties</name>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>@java.version@</maven.compiler.source>
|
||||
<maven.compiler.target>@java.version@</maven.compiler.target>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>@project.groupId@</groupId>
|
||||
<artifactId>@project.artifactId@</artifactId>
|
||||
<version>@project.version@</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<group>off</group>
|
||||
<artifact>off</artifact>
|
||||
<version>off</version>
|
||||
<name>off</name>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>build-info</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.test;
|
||||
|
||||
public class SampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,6 +41,7 @@ import org.springframework.boot.loader.tools.BuildPropertiesWriter.ProjectDetail
|
||||
* {@link MavenProject}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Vedran Pavic
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
|
||||
@@ -55,18 +56,48 @@ public class BuildInfoMojo extends AbstractMojo {
|
||||
@Parameter(defaultValue = "${session}", readonly = true, required = true)
|
||||
private MavenSession session;
|
||||
|
||||
/**
|
||||
* The Maven project.
|
||||
*/
|
||||
@Parameter(defaultValue = "${project}", readonly = true, required = true)
|
||||
private MavenProject project;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.artifactId}")
|
||||
private String artifact;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.name}")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* The value used for the {@code build.time} property in a form suitable for
|
||||
* {@link Instant#parse(CharSequence)}. Defaults to
|
||||
@@ -88,8 +119,8 @@ public class BuildInfoMojo extends AbstractMojo {
|
||||
@Override
|
||||
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||
try {
|
||||
ProjectDetails details = new ProjectDetails(this.project.getGroupId(), this.project.getArtifactId(),
|
||||
this.project.getVersion(), this.project.getName(), getBuildTime(), this.additionalProperties);
|
||||
ProjectDetails details = new ProjectDetails(getGroup(), getArtifact(), getVersion(), getName(),
|
||||
getBuildTime(), this.additionalProperties);
|
||||
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details);
|
||||
this.buildContext.refresh(this.outputFile);
|
||||
}
|
||||
@@ -101,6 +132,34 @@ 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 String getVersion() {
|
||||
if ("off".equalsIgnoreCase(this.version)) {
|
||||
return null;
|
||||
}
|
||||
return this.version;
|
||||
}
|
||||
|
||||
private String getName() {
|
||||
if ("off".equalsIgnoreCase(this.name)) {
|
||||
return null;
|
||||
}
|
||||
return this.name;
|
||||
}
|
||||
|
||||
private Instant getBuildTime() {
|
||||
if (this.time == null || this.time.isEmpty()) {
|
||||
Date startTime = this.session.getRequest().getStartTime();
|
||||
|
||||
Reference in New Issue
Block a user