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

@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure;
import org.springframework.boot.actuate.info.BuildInfoContributor;
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.actuate.info.InfoContributor;
@@ -26,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -75,4 +77,12 @@ public class InfoContributorAutoConfiguration {
return new GitInfoContributor(gitProperties, this.properties.getGit().getMode());
}
@Bean
@ConditionalOnEnabledInfoContributor("build")
@ConditionalOnSingleCandidate(BuildProperties.class)
@Order(DEFAULT_ORDER)
public InfoContributor buildInfoContributor(BuildProperties buildProperties) {
return new BuildInfoContributor(buildProperties, this.properties.getBuild().getMode());
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure;
import org.springframework.boot.actuate.info.BuildInfoContributor;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -28,12 +29,35 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("management.info")
public class InfoContributorProperties {
private final Build build = new Build();
private final Git git = new Git();
public Build getBuild() {
return this.build;
}
public Git getGit() {
return this.git;
}
public static class Build {
/**
* Mode to use to expose build information.
*/
private BuildInfoContributor.Mode mode = BuildInfoContributor.Mode.SIMPLE;
public BuildInfoContributor.Mode getMode() {
return this.mode;
}
public void setMode(BuildInfoContributor.Mode mode) {
this.mode = mode;
}
}
public static class Git {
/**

View File

@@ -0,0 +1,64 @@
/*
* 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.actuate.info;
import java.util.Map;
import java.util.Properties;
import org.springframework.boot.info.BuildProperties;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
/**
* An {@link InfoContributor} that exposes {@link BuildProperties}.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
public class BuildInfoContributor extends InfoPropertiesInfoContributor<BuildProperties> {
public BuildInfoContributor(BuildProperties properties, Mode mode) {
super(properties, mode);
}
public BuildInfoContributor(BuildProperties properties) {
this(properties, Mode.SIMPLE);
}
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("build", generateContent());
}
@Override
protected PropertySource<?> toSimplePropertySource() {
Properties props = new Properties();
copyIfSet(props, "group");
copyIfSet(props, "artifact");
copyIfSet(props, "name");
copyIfSet(props, "version");
copyIfSet(props, "time");
return new PropertiesPropertySource("build", props);
}
@Override
protected void postProcessContent(Map<String, Object> content) {
replaceValue(content, "time", getProperties().getTime());
}
}

View File

@@ -16,16 +16,13 @@
package org.springframework.boot.actuate.info;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import org.springframework.boot.info.GitProperties;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
/**
* An {@link InfoContributor} that exposes {@link GitProperties}.
@@ -33,44 +30,31 @@ import org.springframework.util.StringUtils;
* @author Stephane Nicoll
* @since 1.4.0
*/
public class GitInfoContributor implements InfoContributor {
private final GitProperties properties;
private final Mode mode;
public class GitInfoContributor extends InfoPropertiesInfoContributor<GitProperties> {
public GitInfoContributor(GitProperties properties, Mode mode) {
this.properties = properties;
this.mode = mode;
super(properties, mode);
}
public GitInfoContributor(GitProperties properties) {
this(properties, Mode.SIMPLE);
}
protected final GitProperties getProperties() {
return this.properties;
}
protected final Mode getMode() {
return this.mode;
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("git", generateContent());
}
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("git", extractContent());
}
/**
* Extract the content to contribute to the info endpoint.
* @return the content to expose
*/
protected Map<String, Object> extractContent() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(toPropertySources());
Map<String, Object> content = new PropertySourcesBinder(propertySources).extractAll("");
postProcess(content);
return content;
protected PropertySource<?> toSimplePropertySource() {
Properties props = new Properties();
copyIfSet(props, "branch");
String commitId = getProperties().getShortCommitId();
if (commitId != null) {
props.put("commit.id", commitId);
}
copyIfSet(props, "commit.time");
return new PropertiesPropertySource("git", props);
}
/**
@@ -78,76 +62,9 @@ public class GitInfoContributor implements InfoContributor {
* are converted to {@link Date} instances.
* @param content the content to expose
*/
protected void postProcess(Map<String, Object> content) {
replaceValue(getNestedMap(content, "commit"), "time", getProperties().getDate("commit.time"));
protected void postProcessContent(Map<String, Object> content) {
replaceValue(getNestedMap(content, "commit"), "time", getProperties().getCommitTime());
replaceValue(getNestedMap(content, "build"), "time", getProperties().getDate("build.time"));
}
/**
* Replace the {@code value} for the specified key if the value is not {@code null}.
* @param content the content to expose
* @param key the property to replace
* @param value the new value
*/
protected void replaceValue(Map<String, Object> content, String key, Object value) {
if (content.containsKey(key) && value != null) {
content.put(key, value);
}
}
/**
* Return the {@link PropertySource} to use.
* @return the property source
*/
protected PropertySource<?> toPropertySources() {
if (this.mode.equals(Mode.FULL)) {
return this.properties.toPropertySource();
}
else {
Properties props = new Properties();
copyIfSet(this.properties, props, "branch");
String commitId = this.properties.getShortCommitId();
if (commitId != null) {
props.put("commit.id", commitId);
}
copyIfSet(this.properties, props, "commit.time");
return new PropertiesPropertySource("git", props);
}
}
private void copyIfSet(GitProperties source, Properties target, String key) {
String value = source.get(key);
if (StringUtils.hasText(value)) {
target.put(key, value);
}
}
@SuppressWarnings("unchecked")
private Map<String, Object> getNestedMap(Map<String, Object> map, String key) {
Object o = map.get(key);
if (o == null) {
return Collections.emptyMap();
}
else {
return (Map<String, Object>) o;
}
}
/**
* Defines how git properties should be exposed.
*/
public enum Mode {
/**
* Expose all available data, including custom properties.
*/
FULL,
/**
* Expose a pre-defined set of core settings only.
*/
SIMPLE
}
}

View File

@@ -0,0 +1,173 @@
/*
* 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.actuate.info;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import org.springframework.boot.info.InfoProperties;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
/**
* A base {@link InfoContributor} to expose an {@link InfoProperties}.
*
* @param <T> the type of the {@link InfoProperties} to expose
* @author Stephane Nicoll
* @since 1.4.0
*/
public abstract class InfoPropertiesInfoContributor<T extends InfoProperties> implements InfoContributor {
private final T properties;
private final Mode mode;
protected InfoPropertiesInfoContributor(T properties, Mode mode) {
this.properties = properties;
this.mode = mode;
}
/**
* Return the properties that this instance manages.
* @return the info properties
*/
protected final T getProperties() {
return this.properties;
}
/**
* Return the mode that should be used to expose the content.
* @return the mode
*/
protected final Mode getMode() {
return this.mode;
}
/**
* Return a {@link PropertySource} for the {@link Mode#SIMPLE SIMPLE} mode.
* @return the property source for the simple model
* @see #toPropertySource()
*/
protected abstract PropertySource<?> toSimplePropertySource();
/**
* Extract the content to contribute to the info endpoint.
* @return the content to expose
* @see #extractContent(PropertySource)
* @see #postProcessContent(Map)
*/
protected Map<String, Object> generateContent() {
Map<String, Object> content = extractContent(toPropertySource());
postProcessContent(content);
return content;
}
/**
* Extract the raw content based on the specified {@link PropertySource}.
* @param propertySource the property source to use
* @return the raw content
*/
protected Map<String, Object> extractContent(PropertySource<?> propertySource) {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(propertySource);
return new PropertySourcesBinder(propertySources).extractAll("");
}
/**
* Post-process the content to expose. Elements can be added, changed or removed.
* @param content the content to expose
*/
protected void postProcessContent(Map<String, Object> content) {
}
/**
* Return the {@link PropertySource} to use based on the chosen {@link Mode}.
* @return the property source
*/
protected PropertySource<?> toPropertySource() {
if (this.mode.equals(Mode.FULL)) {
return this.properties.toPropertySource();
}
else {
return toSimplePropertySource();
}
}
/**
* Copy the specified key to the target {@link Properties} if it is set.
* @param target the target properties to update
* @param key the key
*/
protected void copyIfSet(Properties target, String key) {
String value = this.properties.get(key);
if (StringUtils.hasText(value)) {
target.put(key, value);
}
}
/**
* Replace the {@code value} for the specified key if the value is not {@code null}.
* @param content the content to expose
* @param key the property to replace
* @param value the new value
*/
protected void replaceValue(Map<String, Object> content, String key, Object value) {
if (content.containsKey(key) && value != null) {
content.put(key, value);
}
}
/**
* Return the nested map with the specified key or empty map if the specified map
* contains no mapping for the key.
* @param map the content
* @param key the key of a nested map
* @return the nested map
*/
@SuppressWarnings("unchecked")
protected Map<String, Object> getNestedMap(Map<String, Object> map, String key) {
Object o = map.get(key);
if (o == null) {
return Collections.emptyMap();
}
else {
return (Map<String, Object>) o;
}
}
/**
* Defines how properties should be exposed.
*/
public enum Mode {
/**
* Expose all available data, including custom properties.
*/
FULL,
/**
* Expose a pre-defined set of core settings only.
*/
SIMPLE
}
}

View File

@@ -123,6 +123,12 @@
"description": "Enable Mail health check.",
"defaultValue": true
},
{
"name": "management.info.build.enabled",
"type": "java.lang.Boolean",
"description": "Enable build info.",
"defaultValue": true
},
{
"name": "management.info.defaults.enabled",
"type": "java.lang.Boolean",

View File

@@ -22,9 +22,11 @@ import java.util.Properties;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.actuate.info.BuildInfoContributor;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -111,6 +113,41 @@ public class InfoContributorAutoConfigurationTests {
.isSameAs(this.context.getBean("customGitInfoContributor"));
}
@SuppressWarnings("unchecked")
@Test
public void buildPropertiesDefaultMode() {
load(BuildPropertiesConfiguration.class);
Map<String, InfoContributor> beans = this.context
.getBeansOfType(InfoContributor.class);
assertThat(beans).containsKeys("buildInfoContributor");
Map<String, Object> content =
invokeContributor(this.context.getBean("buildInfoContributor", InfoContributor.class));
Object build = content.get("build");
assertThat(build).isInstanceOf(Map.class);
Map<String, Object> gitInfo = (Map<String, Object>) build;
assertThat(gitInfo).containsOnlyKeys("group", "artifact");
}
@SuppressWarnings("unchecked")
@Test
public void buildPropertiesFullMode() {
load(BuildPropertiesConfiguration.class, "management.info.build.mode=full");
Map<String, Object> content =
invokeContributor(this.context.getBean("buildInfoContributor", InfoContributor.class));
Object build = content.get("build");
assertThat(build).isInstanceOf(Map.class);
Map<String, Object> gitInfo = (Map<String, Object>) build;
assertThat(gitInfo).containsOnlyKeys("group", "artifact", "foo");
assertThat(gitInfo.get("foo")).isEqualTo("bar");
}
@Test
public void customBuildInfoContributor() {
load(CustomBuildInfoProviderConfiguration.class);
assertThat(this.context.getBean(BuildInfoContributor.class))
.isSameAs(this.context.getBean("customBuildInfoContributor"));
}
private Map<String, Object> invokeContributor(InfoContributor contributor) {
Info.Builder builder = new Info.Builder();
contributor.contribute(builder);
@@ -146,6 +183,20 @@ public class InfoContributorAutoConfigurationTests {
}
@Configuration
static class BuildPropertiesConfiguration {
@Bean
public BuildProperties buildProperties() {
Properties properties = new Properties();
properties.put("group", "com.example");
properties.put("artifact", "demo");
properties.put("foo", "bar");
return new BuildProperties(properties);
}
}
@Configuration
static class CustomInfoProviderConfiguration {
@@ -170,4 +221,14 @@ public class InfoContributorAutoConfigurationTests {
}
@Configuration
static class CustomBuildInfoProviderConfiguration {
@Bean
public BuildInfoContributor customBuildInfoContributor() {
return new BuildInfoContributor(new BuildProperties(new Properties()));
}
}
}

View File

@@ -40,7 +40,7 @@ public class GitInfoContributorTests {
properties.put("branch", "master");
properties.put("commit.time", "2016-03-04T14:36:33+0100");
GitInfoContributor contributor = new GitInfoContributor(new GitProperties(properties));
Map<String, Object> content = contributor.extractContent();
Map<String, Object> content = contributor.generateContent();
assertThat(content.get("commit")).isInstanceOf(Map.class);
Map<String, Object> commit = (Map<String, Object>) content.get("commit");
Object commitTime = commit.get("time");
@@ -55,7 +55,7 @@ public class GitInfoContributorTests {
properties.put("branch", "master");
properties.put("commit.id", "8e29a0b0d423d2665c6ee5171947c101a5c15681");
GitInfoContributor contributor = new GitInfoContributor(new GitProperties(properties));
Map<String, Object> content = contributor.extractContent();
Map<String, Object> content = contributor.generateContent();
assertThat(content.get("commit")).isInstanceOf(Map.class);
Map<String, Object> commit = (Map<String, Object>) content.get("commit");
assertThat(commit.get("id")).isEqualTo("8e29a0b");