Skip to content

Commit 9640881

Browse files
committed
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
1 parent 3e4da3c commit 9640881

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public class BuildInfoProperties implements Serializable {
5757
* @return the group
5858
*/
5959
public String getGroup() {
60-
return this.group != null ? this.group : this.project.getGroup().toString();
60+
if (this.group == null) {
61+
this.group = this.project.getGroup().toString();
62+
}
63+
return this.group;
6164
}
6265

6366
/**
@@ -94,7 +97,10 @@ public void setArtifact(String artifact) {
9497
* @return the version
9598
*/
9699
public String getVersion() {
97-
return this.version != null ? this.version : this.project.getVersion().toString();
100+
if (this.version == null) {
101+
this.version = this.project.getVersion().toString();
102+
}
103+
return this.version;
98104
}
99105

100106
/**
@@ -113,7 +119,10 @@ public void setVersion(String version) {
113119
* @return the name
114120
*/
115121
public String getName() {
116-
return this.name != null ? this.name : this.project.getName();
122+
if (this.name == null) {
123+
this.name = this.project.getName();
124+
}
125+
return this.name;
117126
}
118127

119128
/**

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.util.Properties;
2323

24+
import org.gradle.testkit.runner.BuildResult;
2425
import org.gradle.testkit.runner.TaskOutcome;
2526
import org.junit.Rule;
2627
import org.junit.Test;
@@ -84,6 +85,16 @@ public void upToDateWhenExecutedTwiceWithFixedTime() {
8485
.getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
8586
}
8687

88+
@Test
89+
public void notUpToDateWhenExecutedTwiceWithFixedTimeAndChangedProjectVersion() {
90+
assertThat(this.gradleBuild.build("buildInfo", "-PnullTime").task(":buildInfo")
91+
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
92+
BuildResult result = this.gradleBuild.build("buildInfo", "-PnullTime",
93+
"-PprojectVersion=0.2.0");
94+
System.out.println(result.getOutput());
95+
assertThat(result.task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
96+
}
97+
8798
private Properties buildInfoProperties() {
8899
File file = new File(this.gradleBuild.getProjectDir(),
89100
"build/build-info.properties");

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ def property(String name, Object defaultValue) {
88
project.hasProperty(name) ? project.getProperty(name) : defaultValue
99
}
1010

11+
version = property('projectVersion', '0.1.0')
12+
1113
task buildInfo(type: org.springframework.boot.gradle.tasks.buildinfo.BuildInfo) {
1214
destinationDir file(property('buildInfoDestinationDir', project.buildDir))
1315
properties {
1416
artifact = property('buildInfoArtifact', 'foo')
15-
version = property('buildInfoVersion', '1.0')
1617
group = property('buildInfoGroup', 'foo')
1718
name = property('buildInfoName', 'foo')
19+
if (!project.hasProperty('projectVersion')) {
20+
version = property('buildInfoVersion', '1.0')
21+
}
1822
additional = ['additional': property('buildInfoAdditional', 'foo')]
1923
if (project.hasProperty('nullTime')) {
2024
time = null

0 commit comments

Comments
 (0)