Consider time in BuildInfo up-to-date checks and allow it to be set

Closes gh-12111
Closes gh-12266
This commit is contained in:
Andy Wilkinson
2018-02-27 19:58:17 +00:00
parent 6b3b7909f3
commit fdb9a1ea8f
8 changed files with 96 additions and 49 deletions

View File

@@ -38,6 +38,9 @@ By default, the generated build information is derived from the project:
| `build.version`
| The version of the project
| `build.time`
| The time at which the project is being built
|===
The properties can be customized using the DSL:
@@ -47,6 +50,13 @@ The properties can be customized using the DSL:
include::../gradle/integrating-with-actuator/build-info-custom-values.gradle[tags=custom-values]
----
The default value for `build.time` is the instant at which the project is being built. A
side-effect of this is that the task will never be up-to-date and, therefore, builds will
take slighly longer as more tasks will have to be executed. Another side-effect is that
the task's output will always change and, therefore, the build will not be truly
repeatable. If you value build performance or repeatability more highly than the accuracy
of the `build.time` property, set `time` to `null` or a fixed value.
Additional properties can also be added to the build information:
[source,groovy,indent=0,subs="verbatim"]

View File

@@ -61,7 +61,8 @@ public class BuildInfo extends ConventionTask {
? "unspecified"
: this.properties.getArtifact(),
this.properties.getVersion(),
this.properties.getName(), coerceToStringValues(
this.properties.getName(), this.properties.getTime(),
coerceToStringValues(
this.properties.getAdditional())));
}
catch (IOException ex) {

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.gradle.tasks.buildinfo;
import java.io.Serializable;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
@@ -40,10 +41,13 @@ public class BuildInfoProperties implements Serializable {
private String name;
private Instant time;
private Map<String, Object> additionalProperties = new HashMap<>();
BuildInfoProperties(Project project) {
this.project = project;
this.time = Instant.now();
}
/**
@@ -121,6 +125,23 @@ public class BuildInfoProperties implements Serializable {
this.name = name;
}
/**
* Returns the value used for the {@code build.time} property. Defaults to
* {@link Instant#now} when the {@code BuildInfoProperties} instance was created.
* @return the time
*/
public Instant getTime() {
return this.time;
}
/**
* Sets the value used for the {@code build.time} property.
* @param time the build time
*/
public void setTime(Instant time) {
this.time = time;
}
/**
* Returns the additional properties that will be included. When written, the name of
* each additional property is prefixed with {@code build.}.
@@ -152,6 +173,7 @@ public class BuildInfoProperties implements Serializable {
result = prime * result + ((this.group == null) ? 0 : this.group.hashCode());
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
result = prime * result + ((this.version == null) ? 0 : this.version.hashCode());
result = prime * result + ((this.time == null) ? 0 : this.time.hashCode());
return result;
}
@@ -207,6 +229,14 @@ public class BuildInfoProperties implements Serializable {
else if (!this.version.equals(other.version)) {
return false;
}
if (this.time == null) {
if (other.time != null) {
return false;
}
}
else if (!this.time.equals(other.time)) {
return false;
}
return true;
}