From 9640881f38d2a5a70652f02de080e272464af965 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 27 Feb 2018 21:13:39 +0000 Subject: [PATCH] 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 --- .../tasks/buildinfo/BuildInfoProperties.java | 15 ++++++++++++--- .../buildinfo/BuildInfoIntegrationTests.java | 11 +++++++++++ .../buildinfo/BuildInfoIntegrationTests.gradle | 6 +++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoProperties.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoProperties.java index 322686b18c..a7df83bfc4 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoProperties.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoProperties.java @@ -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; } /** diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java index e18ffcc8d2..d68f075c6e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java @@ -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"); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.gradle b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.gradle index e97674aee0..11b31a26d4 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.gradle +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.gradle @@ -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