Fix BuildInfo up-to-date check when group, name, or version changes

Previously, if the project's group, name, or version changed the
BuildInfo task would still be considered up-to-date as the values of
the project's properties were not reflected in the fields of the
BuildInfo instance.

This commit updates BuildInfo to copy the value of the project's
property to the corresponding BuildInfo field when the property is
read using its getter method on BuildInfo.

Closes gh-12266
This commit is contained in:
Andy Wilkinson 2018-02-27 21:13:39 +00:00
parent 3e4da3cc37
commit 9640881f38
3 changed files with 28 additions and 4 deletions

View File

@ -57,7 +57,10 @@ public class BuildInfoProperties implements Serializable {
* @return the group
*/
public String getGroup() {
return this.group != null ? this.group : this.project.getGroup().toString();
if (this.group == null) {
this.group = this.project.getGroup().toString();
}
return this.group;
}
/**
@ -94,7 +97,10 @@ public class BuildInfoProperties implements Serializable {
* @return the version
*/
public String getVersion() {
return this.version != null ? this.version : this.project.getVersion().toString();
if (this.version == null) {
this.version = this.project.getVersion().toString();
}
return this.version;
}
/**
@ -113,7 +119,10 @@ public class BuildInfoProperties implements Serializable {
* @return the name
*/
public String getName() {
return this.name != null ? this.name : this.project.getName();
if (this.name == null) {
this.name = this.project.getName();
}
return this.name;
}
/**

View File

@ -21,6 +21,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule;
import org.junit.Test;
@ -84,6 +85,16 @@ public class BuildInfoIntegrationTests {
.getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
}
@Test
public void notUpToDateWhenExecutedTwiceWithFixedTimeAndChangedProjectVersion() {
assertThat(this.gradleBuild.build("buildInfo", "-PnullTime").task(":buildInfo")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
BuildResult result = this.gradleBuild.build("buildInfo", "-PnullTime",
"-PprojectVersion=0.2.0");
System.out.println(result.getOutput());
assertThat(result.task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}
private Properties buildInfoProperties() {
File file = new File(this.gradleBuild.getProjectDir(),
"build/build-info.properties");

View File

@ -8,13 +8,17 @@ def property(String name, Object defaultValue) {
project.hasProperty(name) ? project.getProperty(name) : defaultValue
}
version = property('projectVersion', '0.1.0')
task buildInfo(type: org.springframework.boot.gradle.tasks.buildinfo.BuildInfo) {
destinationDir file(property('buildInfoDestinationDir', project.buildDir))
properties {
artifact = property('buildInfoArtifact', 'foo')
version = property('buildInfoVersion', '1.0')
group = property('buildInfoGroup', 'foo')
name = property('buildInfoName', 'foo')
if (!project.hasProperty('projectVersion')) {
version = property('buildInfoVersion', '1.0')
}
additional = ['additional': property('buildInfoAdditional', 'foo')]
if (project.hasProperty('nullTime')) {
time = null