Add a BuildInfo task for generating build.properties with Gradle

The commit adds a new BuildInfo task that can be used to generate
a build.properties file, intended for inclusion in the Actuator's
info endpoint.

A default instance of the task can be configure using the plugin's
DSL:

springBoot {
	buildInfo()
}

Additional properties can also be configured using the DSL:

springBoot {
	buildInfo {
		additionalProperties = [
			'foo': 'bar'
		]
	}
}

When configured via the DSL, the Java plugin's classes task is
configured to depend on the build info task. Alternatively, if more
control is required, the task can be declared and configured manually:

task buildInfo(type: org.springframework.boot.gradle.buildinfo.BuildInfo) {
	additionalProperties = [
		'foo': 'bar'
	]
}

classes {
	dependsOn buildInfo
}

See gh-2559
This commit is contained in:
Andy Wilkinson
2016-03-23 17:58:54 +00:00
parent 4167469781
commit 58ca9a1c5d
6 changed files with 328 additions and 68 deletions

View File

@@ -17,14 +17,7 @@
package org.springframework.boot.maven;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
@@ -34,6 +27,9 @@ import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.springframework.boot.loader.tools.BuildPropertiesWriter;
import org.springframework.boot.loader.tools.BuildPropertiesWriter.ProjectDetails;
/**
* Generate a {@code build.properties} file based the content of the current
* {@link MavenProject}.
@@ -65,68 +61,15 @@ public class BuildInfoMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Properties properties = createBuildInfo();
try {
createFileIfNecessary(this.outputFile);
FileOutputStream outputStream = new FileOutputStream(this.outputFile);
try {
properties.store(outputStream, "Properties");
}
finally {
closeQuietly(outputStream);
}
new BuildPropertiesWriter(this.outputFile)
.writeBuildProperties(new ProjectDetails(this.project.getGroupId(),
this.project.getArtifactId(), this.project.getVersion(),
this.project.getName(), this.additionalProperties));
}
catch (FileNotFoundException ex) {
catch (Exception ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
}
catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
private void closeQuietly(OutputStream outputStream) {
try {
outputStream.close();
}
catch (IOException ex) {
getLog().error("Error closing FileOutputStream: " + outputStream);
}
}
protected Properties createBuildInfo() {
Properties properties = new Properties();
properties.put("build.group", this.project.getGroupId());
properties.put("build.artifact", this.project.getArtifactId());
properties.put("build.name", this.project.getName());
properties.put("build.version", this.project.getVersion());
properties.put("build.time", formatDate(new Date()));
if (this.additionalProperties != null) {
for (Map.Entry<String, String> entry : this.additionalProperties.entrySet()) {
properties.put("build." + entry.getKey(), entry.getValue());
}
}
return properties;
}
private String formatDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
return sdf.format(date);
}
private void createFileIfNecessary(File file)
throws MojoExecutionException, IOException {
if (file.exists()) {
return;
}
File parent = file.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new MojoExecutionException("Cannot create parent directory for '"
+ this.outputFile.getAbsolutePath() + "'");
}
if (!file.createNewFile()) {
throw new MojoExecutionException("Cannot create target file '"
+ this.outputFile.getAbsolutePath() + "'");
}
}
}