Skip to content

Commit

Permalink
Merge branch '2.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Feb 14, 2020
2 parents ddeac66 + d61b035 commit b9c2d77
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 77 deletions.
Expand Up @@ -25,7 +25,7 @@
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;
Expand Down Expand Up @@ -89,7 +89,7 @@ public void setDestinationDir(File destinationDir) {
* {@code build-info.properties} file.
* @return the properties
*/
@Input
@Nested
public BuildInfoProperties getProperties() {
return this.properties;
}
Expand Down
@@ -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.
Expand All @@ -22,6 +22,9 @@
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.
Expand All @@ -32,123 +35,131 @@
@SuppressWarnings("serial")
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 Instant time;
private final Property<Instant> time;

private Map<String, Object> 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()));
}

/**
* Returns the value used for the {@code build.group} property. Defaults to the
* {@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();
}

/**
* Sets the value used for the {@code build.group} property.
* @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();
}

/**
* Sets the value used for the {@code build.artifact} property.
* @param artifact the artifact
*/
public void setArtifact(String artifact) {
this.artifact = artifact;
this.artifact.set(artifact);
}

/**
* Returns the value used for the {@code build.version} property. Defaults to the
* {@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();
}

/**
* Sets the value used for the {@code build.version} property.
* @param version the version
*/
public void setVersion(String version) {
this.version = version;
this.version.set(version);
}

/**
* Returns the value used for the {@code build.name} property. Defaults to the
* {@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();
}

/**
* Sets the value used for the {@code build.name} property.
* @param name the name
*/
public void setName(String name) {
this.name = name;
this.name.set(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
*/
@Input
@Optional
public Instant getTime() {
return this.time;
return this.time.getOrNull();
}

/**
* Sets the value used for the {@code build.time} property.
* @param time the build time
*/
public void setTime(Instant time) {
this.time = time;
this.time.set(time);
}

/**
* Returns the additional properties that will be included. When written, the name of
* each additional property is prefixed with {@code build.}.
* @return the additional properties
*/
@Input
@Optional
public Map<String, Object> getAdditional() {
return this.additionalProperties;
}
Expand All @@ -162,46 +173,4 @@ public void setAdditional(Map<String, Object> 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;
}

}
Expand Up @@ -64,7 +64,7 @@ void basicExecution() {
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");
}

@TestTemplate
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit b9c2d77

Please sign in to comment.