Collect and display build information

This commit updates the Maven plugin to generate a
`META-INF/boot/build.properties` file with various build-specific
settings (group, artifact, name, version and build time). Additionally,
the plugin can be configured to write an arbitrary number of additional
properties.

A new `BuildProperties` bean is automatically exposed when such a file is
present. If that bean is present, an `InfoContributor` is automatically
created to expose that information under the `build` key.

As for the git contributor, it is possible to only display the core
settings or everything using the `management.info.build.mode` property.

See gh-2559
This commit is contained in:
Stephane Nicoll
2016-03-04 13:25:23 +01:00
parent 3e6b584953
commit dddea70985
31 changed files with 1024 additions and 112 deletions

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>generate-build-info-additional-properties</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<name>Generate build info</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate-build-info</goal>
</goals>
<configuration>
<additionalProperties>
<foo>bar</foo>
<encoding>${project.build.sourceEncoding}</encoding>
<java.source>1.8</java.source>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring.version@</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>@servlet-api.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2012-2014 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.test;
public class SampleApplication {
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,14 @@
import org.springframework.boot.maven.Verify
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
def file = new File(basedir, "target/classes/META-INF/boot/build.properties")
println file.getAbsolutePath()
Properties properties = Verify.verifyBuildInfo(file,
'org.springframework.boot.maven.it', 'generate-build-info-additional-properties',
'Generate build info', '0.0.1.BUILD-SNAPSHOT')
assertTrue properties.containsKey('build.time')
assertEquals 'bar', properties.get('build.foo')
assertEquals 'UTF-8', properties.get('build.encoding')
assertEquals '1.8', properties.get('build.java.source')

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>generate-build-info-custom-file</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<name>Generate custom build info</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate-build-info</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/build.info</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring.version@</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>@servlet-api.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2012-2014 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.test;
public class SampleApplication {
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,10 @@
import org.springframework.boot.maven.Verify
import static org.junit.Assert.assertTrue
def file = new File(basedir, "target/build.info")
println file.getAbsolutePath()
Properties properties = Verify.verifyBuildInfo(file,
'org.springframework.boot.maven.it', 'generate-build-info-custom-file',
'Generate custom build info', '0.0.1.BUILD-SNAPSHOT')
assertTrue properties.containsKey('build.time')

View File

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>generate-build-info</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<name>Generate build info</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate-build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring.version@</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>@servlet-api.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2012-2014 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.test;
public class SampleApplication {
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,10 @@
import org.springframework.boot.maven.Verify
import static org.junit.Assert.assertTrue
def file = new File(basedir, "target/classes/META-INF/boot/build.properties")
println file.getAbsolutePath()
Properties properties = Verify.verifyBuildInfo(file,
'org.springframework.boot.maven.it', 'generate-build-info',
'Generate build info', '0.0.1.BUILD-SNAPSHOT')
assertTrue properties.containsKey('build.time')

View File

@@ -0,0 +1,126 @@
/*
* 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.maven;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
/**
* Generate a {@code build.properties} file based the content of the {@link MavenProject}.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
@Mojo(name = "generate-build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class GenerateBuildInfoMojo extends AbstractMojo {
/**
* The Maven project.
*/
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
/**
* The location of the generated build.properties.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/boot/build.properties")
private File outputFile;
/**
* Additional properties to store in the build.properties. Each entry is prefixed by
* {@code build.} in the generated build.properties.
*/
@Parameter
private Map<String, String> additionalProperties;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Properties properties = createBuildInfo();
FileOutputStream fos = null;
try {
createFileIfNecessary(this.outputFile);
fos = new FileOutputStream(this.outputFile);
properties.store(fos, "Properties");
}
catch (FileNotFoundException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
finally {
try {
if (fos != null) {
fos.close();
}
}
catch (IOException e) {
getLog().error("Error closing FileOutputStream: " + fos);
}
}
}
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() + "'");
}
}
}

View File

@@ -22,10 +22,13 @@ import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
public final class Verify {
@@ -69,6 +73,18 @@ public final class Verify {
new ModuleArchiveVerification(file).verify();
}
public static Properties verifyBuildInfo(File file, String group, String artifact,
String name, String version) throws IOException {
FileSystemResource resource = new FileSystemResource(file);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
assertThat(properties.get("build.group")).isEqualTo(group);
assertThat(properties.get("build.artifact")).isEqualTo(artifact);
assertThat(properties.get("build.name")).isEqualTo(name);
assertThat(properties.get("build.version")).isEqualTo(version);
return properties;
}
public static class ArchiveVerifier {
private final ZipFile zipFile;