Fix up-to-date checking of build info properties

Closes gh-20135
pull/20429/head
Andy Wilkinson 5 years ago
parent 32c1dd45a9
commit c8907d46b4

@ -25,7 +25,7 @@ import org.gradle.api.Action;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.Task; import org.gradle.api.Task;
import org.gradle.api.internal.ConventionTask; import org.gradle.api.internal.ConventionTask;
import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskExecutionException; import org.gradle.api.tasks.TaskExecutionException;
@ -89,7 +89,7 @@ public class BuildInfo extends ConventionTask {
* {@code build-info.properties} file. * {@code build-info.properties} file.
* @return the properties * @return the properties
*/ */
@Input @Nested
public BuildInfoProperties getProperties() { public BuildInfoProperties getProperties() {
return this.properties; return this.properties;
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,9 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
/** /**
* The properties that are written into the {@code build-info.properties} file. * The properties that are written into the {@code build-info.properties} file.
@ -32,23 +35,28 @@ import org.gradle.api.Project;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class BuildInfoProperties implements Serializable { public class BuildInfoProperties implements Serializable {
private final transient Project project; private final Property<String> group;
private String group; private final Property<String> artifact;
private String artifact; private final Property<String> version;
private String version; private final Property<String> name;
private String name; private final Property<Instant> time;
private Instant time;
private Map<String, Object> additionalProperties = new HashMap<>(); private Map<String, Object> additionalProperties = new HashMap<>();
BuildInfoProperties(Project project) { BuildInfoProperties(Project project) {
this.project = project; this.time = project.getObjects().property(Instant.class);
this.time = Instant.now(); this.time.set(Instant.now());
this.group = project.getObjects().property(String.class);
this.group.set(project.provider(() -> project.getGroup().toString()));
this.artifact = project.getObjects().property(String.class);
this.version = project.getObjects().property(String.class);
this.version.set(project.provider(() -> project.getVersion().toString()));
this.name = project.getObjects().property(String.class);
this.name.set(project.provider(() -> project.getName().toString()));
} }
/** /**
@ -56,11 +64,10 @@ public class BuildInfoProperties implements Serializable {
* {@link Project#getGroup() Project's group}. * {@link Project#getGroup() Project's group}.
* @return the group * @return the group
*/ */
@Input
@Optional
public String getGroup() { public String getGroup() {
if (this.group == null) { return this.group.getOrNull();
this.group = this.project.getGroup().toString();
}
return this.group;
} }
/** /**
@ -68,15 +75,17 @@ public class BuildInfoProperties implements Serializable {
* @param group the group name * @param group the group name
*/ */
public void setGroup(String group) { public void setGroup(String group) {
this.group = group; this.group.set(group);
} }
/** /**
* Returns the value used for the {@code build.artifact} property. * Returns the value used for the {@code build.artifact} property.
* @return the artifact * @return the artifact
*/ */
@Input
@Optional
public String getArtifact() { public String getArtifact() {
return this.artifact; return this.artifact.getOrNull();
} }
/** /**
@ -84,7 +93,7 @@ public class BuildInfoProperties implements Serializable {
* @param artifact the artifact * @param artifact the artifact
*/ */
public void setArtifact(String artifact) { public void setArtifact(String artifact) {
this.artifact = artifact; this.artifact.set(artifact);
} }
/** /**
@ -92,11 +101,10 @@ public class BuildInfoProperties implements Serializable {
* {@link Project#getVersion() Project's version}. * {@link Project#getVersion() Project's version}.
* @return the version * @return the version
*/ */
@Input
@Optional
public String getVersion() { public String getVersion() {
if (this.version == null) { return this.version.getOrNull();
this.version = this.project.getVersion().toString();
}
return this.version;
} }
/** /**
@ -104,7 +112,7 @@ public class BuildInfoProperties implements Serializable {
* @param version the version * @param version the version
*/ */
public void setVersion(String version) { public void setVersion(String version) {
this.version = version; this.version.set(version);
} }
/** /**
@ -112,11 +120,10 @@ public class BuildInfoProperties implements Serializable {
* {@link Project#getDisplayName() Project's display name}. * {@link Project#getDisplayName() Project's display name}.
* @return the name * @return the name
*/ */
@Input
@Optional
public String getName() { public String getName() {
if (this.name == null) { return this.name.getOrNull();
this.name = this.project.getName();
}
return this.name;
} }
/** /**
@ -124,7 +131,7 @@ public class BuildInfoProperties implements Serializable {
* @param name the name * @param name the name
*/ */
public void setName(String name) { public void setName(String name) {
this.name = name; this.name.set(name);
} }
/** /**
@ -132,8 +139,10 @@ public class BuildInfoProperties implements Serializable {
* {@link Instant#now} when the {@code BuildInfoProperties} instance was created. * {@link Instant#now} when the {@code BuildInfoProperties} instance was created.
* @return the time * @return the time
*/ */
@Input
@Optional
public Instant getTime() { public Instant getTime() {
return this.time; return this.time.getOrNull();
} }
/** /**
@ -141,7 +150,7 @@ public class BuildInfoProperties implements Serializable {
* @param time the build time * @param time the build time
*/ */
public void setTime(Instant time) { public void setTime(Instant time) {
this.time = time; this.time.set(time);
} }
/** /**
@ -149,6 +158,8 @@ public class BuildInfoProperties implements Serializable {
* each additional property is prefixed with {@code build.}. * each additional property is prefixed with {@code build.}.
* @return the additional properties * @return the additional properties
*/ */
@Input
@Optional
public Map<String, Object> getAdditional() { public Map<String, Object> getAdditional() {
return this.additionalProperties; return this.additionalProperties;
} }
@ -162,46 +173,4 @@ public class BuildInfoProperties implements Serializable {
this.additionalProperties = additionalProperties; this.additionalProperties = additionalProperties;
} }
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BuildInfoProperties other = (BuildInfoProperties) obj;
boolean result = true;
result = result && nullSafeEquals(this.additionalProperties, other.additionalProperties);
result = result && nullSafeEquals(this.artifact, other.artifact);
result = result && nullSafeEquals(this.group, other.group);
result = result && nullSafeEquals(this.name, other.name);
result = result && nullSafeEquals(this.version, other.version);
result = result && nullSafeEquals(this.time, other.time);
return result;
}
private boolean nullSafeEquals(Object o1, Object o2) {
if (o1 == o2) {
return true;
}
if (o1 == null || o2 == null) {
return false;
}
return (o1.equals(o2));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.additionalProperties == null) ? 0 : this.additionalProperties.hashCode());
result = prime * result + ((this.artifact == null) ? 0 : this.artifact.hashCode());
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;
}
} }

@ -63,7 +63,7 @@ public class BuildInfoIntegrationTests {
assertThat(buildInfoProperties).containsEntry("build.group", "foo"); assertThat(buildInfoProperties).containsEntry("build.group", "foo");
assertThat(buildInfoProperties).containsEntry("build.additional", "foo"); assertThat(buildInfoProperties).containsEntry("build.additional", "foo");
assertThat(buildInfoProperties).containsEntry("build.name", "foo"); assertThat(buildInfoProperties).containsEntry("build.name", "foo");
assertThat(buildInfoProperties).containsEntry("build.version", "1.0"); assertThat(buildInfoProperties).containsEntry("build.version", "0.1.0");
} }
@Test @Test

@ -14,9 +14,6 @@ task buildInfo(type: org.springframework.boot.gradle.tasks.buildinfo.BuildInfo)
artifact = property('buildInfoArtifact', 'foo') artifact = property('buildInfoArtifact', 'foo')
group = property('buildInfoGroup', 'foo') group = property('buildInfoGroup', 'foo')
name = property('buildInfoName', 'foo') name = property('buildInfoName', 'foo')
if (!project.hasProperty('projectVersion')) {
version = property('buildInfoVersion', '1.0')
}
additional = ['additional': property('buildInfoAdditional', 'foo')] additional = ['additional': property('buildInfoAdditional', 'foo')]
if (project.hasProperty('nullTime')) { if (project.hasProperty('nullTime')) {
time = null time = null

Loading…
Cancel
Save