forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 spring-projectsgh-2559
- Loading branch information
1 parent
4167469
commit 58ca9a1
Showing
6 changed files
with
328 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/buildinfo/BuildInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2012-2016 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.gradle.buildinfo; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.plugins.JavaPlugin; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.gradle.api.tasks.TaskExecutionException; | ||
import org.gradle.api.tasks.bundling.Jar; | ||
|
||
import org.springframework.boot.loader.tools.BuildPropertiesWriter; | ||
import org.springframework.boot.loader.tools.BuildPropertiesWriter.ProjectDetails; | ||
|
||
/** | ||
* {@link DefaultTask} for generating a {@code build.properties} file from a | ||
* {@code Project}. | ||
* <p> | ||
* By default, the {@code build.properties} file is generated in | ||
* project.buildDir/resources/main/META-INF/boot. | ||
* </p> | ||
* | ||
* @author Andy Wilkinson | ||
*/ | ||
public class BuildInfo extends DefaultTask { | ||
|
||
@OutputFile | ||
private File outputFile = getProject().file(new File(getProject().getBuildDir(), | ||
"resources/main/META-INF/boot/build.properties")); | ||
|
||
@Input | ||
private String projectGroup = getProject().getGroup().toString(); | ||
|
||
@Input | ||
private String projectArtifact = ((Jar) getProject().getTasks() | ||
.getByName(JavaPlugin.JAR_TASK_NAME)).getBaseName(); | ||
|
||
@Input | ||
private String projectVersion = getProject().getVersion().toString(); | ||
|
||
@Input | ||
private String projectName = getProject().getName(); | ||
|
||
@Input | ||
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | ||
|
||
@TaskAction | ||
public void generateBuildProperties() { | ||
try { | ||
new BuildPropertiesWriter(this.outputFile) | ||
.writeBuildProperties(new ProjectDetails(this.projectGroup, | ||
this.projectArtifact, this.projectVersion, this.projectName, | ||
coerceToStringValues(this.additionalProperties))); | ||
} | ||
catch (IOException ex) { | ||
throw new TaskExecutionException(this, ex); | ||
} | ||
} | ||
|
||
public String getProjectGroup() { | ||
return this.projectGroup; | ||
} | ||
|
||
public void setProjectGroup(String projectGroup) { | ||
this.projectGroup = projectGroup; | ||
} | ||
|
||
public String getProjectArtifact() { | ||
return this.projectArtifact; | ||
} | ||
|
||
public void setProjectArtifact(String projectArtifact) { | ||
this.projectArtifact = projectArtifact; | ||
} | ||
|
||
public String getProjectVersion() { | ||
return this.projectVersion; | ||
} | ||
|
||
public void setProjectVersion(String projectVersion) { | ||
this.projectVersion = projectVersion; | ||
} | ||
|
||
public String getProjectName() { | ||
return this.projectName; | ||
} | ||
|
||
public void setProjectName(String projectName) { | ||
this.projectName = projectName; | ||
} | ||
|
||
public File getOutputFile() { | ||
return this.outputFile; | ||
} | ||
|
||
public void setOutputFile(File outputFile) { | ||
this.outputFile = outputFile; | ||
} | ||
|
||
public Map<String, Object> getAdditionalProperties() { | ||
return this.additionalProperties; | ||
} | ||
|
||
public void setAdditionalProperties(Map<String, Object> additionalProperties) { | ||
this.additionalProperties = additionalProperties; | ||
} | ||
|
||
private Map<String, String> coerceToStringValues(Map<String, Object> input) { | ||
Map<String, String> output = new HashMap<String, String>(); | ||
for (Entry<String, Object> entry : this.additionalProperties.entrySet()) { | ||
output.put(entry.getKey(), entry.getValue().toString()); | ||
} | ||
return output; | ||
} | ||
|
||
} |
146 changes: 146 additions & 0 deletions
146
...ader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2012-2016 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.loader.tools; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* A {@code BuildPropertiesWriter} writes the {@code build.properties} for consumption by | ||
* the Actuator. | ||
* | ||
* @author Andy Wilkinson | ||
* @author Stephane Nicoll | ||
*/ | ||
public final class BuildPropertiesWriter { | ||
|
||
private final File outputFile; | ||
|
||
/** | ||
* Creates a new {@code BuildPropertiesWriter} that will write to the given | ||
* {@code outputFile}. | ||
* | ||
* @param outputFile the output file | ||
*/ | ||
public BuildPropertiesWriter(File outputFile) { | ||
this.outputFile = outputFile; | ||
} | ||
|
||
public void writeBuildProperties(ProjectDetails projectDetails) throws IOException { | ||
Properties properties = createBuildInfo(projectDetails); | ||
createFileIfNecessary(this.outputFile); | ||
FileOutputStream outputStream = new FileOutputStream(this.outputFile); | ||
try { | ||
properties.store(outputStream, "Properties"); | ||
} | ||
finally { | ||
try { | ||
outputStream.close(); | ||
} | ||
catch (IOException ex) { | ||
// Continue | ||
} | ||
} | ||
} | ||
|
||
private void createFileIfNecessary(File file) throws IOException { | ||
if (file.exists()) { | ||
return; | ||
} | ||
File parent = file.getParentFile(); | ||
if (!parent.isDirectory() && !parent.mkdirs()) { | ||
throw new IllegalStateException("Cannot create parent directory for '" | ||
+ this.outputFile.getAbsolutePath() + "'"); | ||
} | ||
if (!file.createNewFile()) { | ||
throw new IllegalStateException("Cannot create target file '" | ||
+ this.outputFile.getAbsolutePath() + "'"); | ||
} | ||
} | ||
|
||
protected Properties createBuildInfo(ProjectDetails project) { | ||
Properties properties = new Properties(); | ||
properties.put("build.group", project.getGroup()); | ||
properties.put("build.artifact", project.getArtifact()); | ||
properties.put("build.name", project.getName()); | ||
properties.put("build.version", project.getVersion()); | ||
properties.put("build.time", formatDate(new Date())); | ||
if (project.getAdditionalProperties() != null) { | ||
for (Map.Entry<String, String> entry : project.getAdditionalProperties() | ||
.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); | ||
} | ||
|
||
/** | ||
* Build-system agnostic details of a project. | ||
*/ | ||
public static final class ProjectDetails { | ||
|
||
private final String group; | ||
|
||
private final String artifact; | ||
|
||
private final String name; | ||
|
||
private final String version; | ||
|
||
private final Map<String, String> additionalProperties; | ||
|
||
public ProjectDetails(String group, String artifact, String version, String name, | ||
Map<String, String> additionalProperties) { | ||
this.group = group; | ||
this.artifact = artifact; | ||
this.name = name; | ||
this.version = version; | ||
this.additionalProperties = additionalProperties; | ||
} | ||
|
||
public String getGroup() { | ||
return this.group; | ||
} | ||
|
||
public String getArtifact() { | ||
return this.artifact; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public String getVersion() { | ||
return this.version; | ||
} | ||
|
||
public Map<String, String> getAdditionalProperties() { | ||
return this.additionalProperties; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.