diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java index b81f7463c8..80612e34bd 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java @@ -25,7 +25,7 @@ import org.gradle.api.Action; import org.gradle.api.Project; import org.gradle.api.Task; 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.TaskAction; import org.gradle.api.tasks.TaskExecutionException; @@ -89,7 +89,7 @@ public class BuildInfo extends ConventionTask { * {@code build-info.properties} file. * @return the properties */ - @Input + @Nested public BuildInfoProperties getProperties() { return this.properties; } 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 5d2041a547..e9a2c96f3f 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 @@ -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"); * 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 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. @@ -32,23 +35,28 @@ import org.gradle.api.Project; @SuppressWarnings("serial") public class BuildInfoProperties implements Serializable { - private final transient Project project; + private final Property group; - private String group; + private final Property artifact; - private String artifact; + private final Property version; - private String version; + private final Property name; - private String name; - - private Instant time; + private final Property time; private Map additionalProperties = new HashMap<>(); BuildInfoProperties(Project project) { - this.project = project; - this.time = Instant.now(); + this.time = project.getObjects().property(Instant.class); + 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}. * @return the group */ + @Input + @Optional public String getGroup() { - if (this.group == null) { - this.group = this.project.getGroup().toString(); - } - return this.group; + return this.group.getOrNull(); } /** @@ -68,15 +75,17 @@ public class BuildInfoProperties implements Serializable { * @param group the group name */ public void setGroup(String group) { - this.group = group; + this.group.set(group); } /** * Returns the value used for the {@code build.artifact} property. * @return the artifact */ + @Input + @Optional public String getArtifact() { - return this.artifact; + return this.artifact.getOrNull(); } /** @@ -84,7 +93,7 @@ public class BuildInfoProperties implements Serializable { * @param artifact the 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}. * @return the version */ + @Input + @Optional public String getVersion() { - if (this.version == null) { - this.version = this.project.getVersion().toString(); - } - return this.version; + return this.version.getOrNull(); } /** @@ -104,7 +112,7 @@ public class BuildInfoProperties implements Serializable { * @param version the 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}. * @return the name */ + @Input + @Optional public String getName() { - if (this.name == null) { - this.name = this.project.getName(); - } - return this.name; + return this.name.getOrNull(); } /** @@ -124,7 +131,7 @@ public class BuildInfoProperties implements Serializable { * @param name the 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. * @return the time */ + @Input + @Optional public Instant getTime() { - return this.time; + return this.time.getOrNull(); } /** @@ -141,7 +150,7 @@ public class BuildInfoProperties implements Serializable { * @param time the build 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.}. * @return the additional properties */ + @Input + @Optional public Map getAdditional() { return this.additionalProperties; } @@ -162,46 +173,4 @@ public class BuildInfoProperties implements Serializable { 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; - } - } 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 5957464ab0..ac98fc5a5d 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 @@ -63,7 +63,7 @@ public class BuildInfoIntegrationTests { assertThat(buildInfoProperties).containsEntry("build.group", "foo"); assertThat(buildInfoProperties).containsEntry("build.additional", "foo"); assertThat(buildInfoProperties).containsEntry("build.name", "foo"); - assertThat(buildInfoProperties).containsEntry("build.version", "1.0"); + assertThat(buildInfoProperties).containsEntry("build.version", "0.1.0"); } @Test 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 16571fb1c2..2e242db579 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 @@ -14,9 +14,6 @@ task buildInfo(type: org.springframework.boot.gradle.tasks.buildinfo.BuildInfo) artifact = property('buildInfoArtifact', 'foo') 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