mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
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:
parent
3e6b584953
commit
dddea70985
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 {
|
||||
|
||||
/**
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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",
|
||||
|
@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -22,9 +22,11 @@ import java.util.Properties;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
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.ConditionContext;
|
||||
@ -60,6 +62,13 @@ public class ProjectInfoAutoConfiguration {
|
||||
return new GitProperties(loadFrom(this.properties.getGit().getLocation(), "git"));
|
||||
}
|
||||
|
||||
@ConditionalOnResource(resources = "${spring.info.build.location:classpath:META-INF/boot/build.properties}")
|
||||
@ConditionalOnMissingBean
|
||||
@Bean
|
||||
public BuildProperties buildProperties() throws Exception {
|
||||
return new BuildProperties(loadFrom(this.properties.getBuild().getLocation(), "build"));
|
||||
}
|
||||
|
||||
protected Properties loadFrom(Resource location, String prefix) throws IOException {
|
||||
String p = prefix.endsWith(".") ? prefix : prefix + ".";
|
||||
Properties source = PropertiesLoaderUtils.loadProperties(location);
|
||||
@ -72,7 +81,6 @@ public class ProjectInfoAutoConfiguration {
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
static class GitResourceAvailableCondition extends SpringBootCondition {
|
||||
|
||||
private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader();
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.info;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
@ -30,8 +31,14 @@ import org.springframework.core.io.Resource;
|
||||
@ConfigurationProperties("spring.info")
|
||||
public class ProjectInfoProperties {
|
||||
|
||||
private final Build build = new Build();
|
||||
|
||||
private final Git git = new Git();
|
||||
|
||||
public Build getBuild() {
|
||||
return this.build;
|
||||
}
|
||||
|
||||
public Git getGit() {
|
||||
return this.git;
|
||||
}
|
||||
@ -46,13 +53,34 @@ public class ProjectInfoProperties {
|
||||
getGit().setLocation(defaultGitLocation);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Build specific info properties.
|
||||
*/
|
||||
public static class Build {
|
||||
|
||||
/**
|
||||
* Location of the generated build.properties file.
|
||||
*/
|
||||
private Resource location = new ClassPathResource("META-INF/boot/build.properties");
|
||||
|
||||
public Resource getLocation() {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
public void setLocation(Resource location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Git specific info properties.
|
||||
*/
|
||||
public static class Git {
|
||||
|
||||
/**
|
||||
* Location of the generated git info properties file.
|
||||
* Location of the generated git.properties file.
|
||||
*/
|
||||
private Resource location;
|
||||
|
||||
|
@ -23,6 +23,7 @@ import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.info.BuildProperties;
|
||||
import org.springframework.boot.info.GitProperties;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@ -82,12 +83,48 @@ public class ProjectInfoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void gitPropertiesFallbackWithGitPropertiesBean() {
|
||||
load(CustomGitPropertiesConfiguration.class,
|
||||
load(CustomInfoPropertiesConfiguration.class,
|
||||
"spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties");
|
||||
GitProperties gitProperties = this.context.getBean(GitProperties.class);
|
||||
assertThat(gitProperties).isSameAs(this.context.getBean("customGitProperties"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildPropertiesDefaultLocation() {
|
||||
load();
|
||||
BuildProperties buildProperties = this.context.getBean(BuildProperties.class);
|
||||
assertThat(buildProperties.getGroup()).isEqualTo("com.example");
|
||||
assertThat(buildProperties.getArtifact()).isEqualTo("demo");
|
||||
assertThat(buildProperties.getName()).isEqualTo("Demo Project");
|
||||
assertThat(buildProperties.getVersion()).isEqualTo("0.0.1-SNAPSHOT");
|
||||
assertThat(buildProperties.getTime().getTime()).isEqualTo(1457100965000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildPropertiesCustomLocation() {
|
||||
load("spring.info.build.location=classpath:/org/springframework/boot/autoconfigure/info/build.properties");
|
||||
BuildProperties buildProperties = this.context.getBean(BuildProperties.class);
|
||||
assertThat(buildProperties.getGroup()).isEqualTo("com.example.acme");
|
||||
assertThat(buildProperties.getArtifact()).isEqualTo("acme");
|
||||
assertThat(buildProperties.getName()).isEqualTo("acme");
|
||||
assertThat(buildProperties.getVersion()).isEqualTo("1.0.1-SNAPSHOT");
|
||||
assertThat(buildProperties.getTime().getTime()).isEqualTo(1457088120000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildPropertiesCustomInvalidLocation() {
|
||||
load("spring.info.build.location=classpath:/org/acme/no-build.properties");
|
||||
Map<String, BuildProperties> beans = this.context.getBeansOfType(BuildProperties.class);
|
||||
assertThat(beans).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildPropertiesFallbackWithBuildInfoBean() {
|
||||
load(CustomInfoPropertiesConfiguration.class);
|
||||
BuildProperties buildProperties = this.context.getBean(BuildProperties.class);
|
||||
assertThat(buildProperties).isSameAs(this.context.getBean("customBuildProperties"));
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
@ -105,13 +142,18 @@ public class ProjectInfoAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomGitPropertiesConfiguration {
|
||||
static class CustomInfoPropertiesConfiguration {
|
||||
|
||||
@Bean
|
||||
public GitProperties customGitProperties() {
|
||||
return new GitProperties(new Properties());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BuildProperties customBuildProperties() {
|
||||
return new BuildProperties(new Properties());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
build.group=com.example
|
||||
build.artifact=demo
|
||||
build.name=Demo Project
|
||||
build.version=0.0.1-SNAPSHOT
|
||||
build.time=2016-03-04T15\:16\:05+0100
|
@ -0,0 +1,5 @@
|
||||
build.group=com.example.acme
|
||||
build.artifact=acme
|
||||
build.name=acme
|
||||
build.version=1.0.1-SNAPSHOT
|
||||
build.time=2016-03-04T11:42:00+0100
|
@ -80,7 +80,8 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.hazelcast.config= # The location of the configuration file to use to initialize Hazelcast.
|
||||
|
||||
# PROJECT INFORMATION ({sc-spring-boot-autoconfigure}/info/ProjectInfoProperties.{sc-ext}[ProjectInfoProperties])
|
||||
spring.info.git.location=classpath:git.properties # Location of the generated git info properties file
|
||||
spring.info.build.location=classpath:META-INF/boot/build.properties # Location of the generated build.properties file.
|
||||
spring.info.git.location=classpath:git.properties # Location of the generated git.properties file.
|
||||
|
||||
# JMX
|
||||
spring.jmx.default-domain= # JMX domain name.
|
||||
@ -888,6 +889,8 @@ content into your application; rather pick only the properties that you need.
|
||||
management.health.status.order=DOWN, OUT_OF_SERVICE, UNKNOWN, UP # Comma-separated list of health statuses in order of severity.
|
||||
|
||||
# INFO CONTRIBUTORS
|
||||
management.info.build.enabled=true # Enable build info.
|
||||
management.info.build.mode=simple # Mode to use to expose build information.
|
||||
management.info.defaults.enabled=true # Enable default health indicators.
|
||||
management.info.env.enabled=true # Enable environment info.
|
||||
management.info.git.enabled=true # Enable git info.
|
||||
|
@ -71,6 +71,22 @@
|
||||
<configuration>
|
||||
<executable>true</executable>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generate build info</id>
|
||||
<goals>
|
||||
<goal>generate-build-info</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<additionalProperties>
|
||||
<encoding.source>${project.build.sourceEncoding}</encoding.source>
|
||||
<encoding.reporting>${project.reporting.outputEncoding}</encoding.reporting>
|
||||
<java.source>${maven.compiler.source}</java.source>
|
||||
<java.target>${maven.compiler.target}</java.target>
|
||||
</additionalProperties>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
@ -18,10 +18,10 @@ shell.auth: spring
|
||||
#shell.auth.key.path: ${user.home}/test/id_rsa.pub.pem
|
||||
#shell.auth: simple
|
||||
spring.jmx.enabled: true
|
||||
info.group: @project.groupId@
|
||||
info.artifact: @project.artifactId@
|
||||
info.name: @project.name@
|
||||
info.version: @project.version@
|
||||
|
||||
spring.jackson.serialization.write_dates_as_timestamps=false
|
||||
|
||||
management.info.build.mode=full
|
||||
|
||||
management.trace.include=REQUEST_HEADERS,RESPONSE_HEADERS,ERRORS,PATH_INFO,\
|
||||
PATH_TRANSLATED,CONTEXT_PATH,USER_PRINCIPAL,PARAMETERS,QUERY_STRING,AUTH_TYPE,\
|
||||
|
@ -147,6 +147,10 @@ public class SampleActuatorApplicationTests {
|
||||
assertThat(entity.getBody())
|
||||
.contains("\"artifact\":\"spring-boot-sample-actuator\"");
|
||||
assertThat(entity.getBody()).contains("\"someKey\":\"someValue\"");
|
||||
assertThat(entity.getBody())
|
||||
.contains("\"java\":{", "\"source\":\"1.8\"", "\"target\":\"1.8\"");
|
||||
assertThat(entity.getBody())
|
||||
.contains("\"encoding\":{", "\"source\":\"UTF-8\"", "\"reporting\":\"UTF-8\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -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>
|
@ -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) {
|
||||
}
|
||||
|
||||
}
|
@ -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')
|
@ -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>
|
@ -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) {
|
||||
}
|
||||
|
||||
}
|
@ -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')
|
@ -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>
|
@ -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) {
|
||||
}
|
||||
|
||||
}
|
@ -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')
|
@ -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() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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.info;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Provide build-related information such as group and artifact.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class BuildProperties extends InfoProperties {
|
||||
|
||||
/**
|
||||
* Create an instance with the specified entries.
|
||||
* @param entries the information to expose
|
||||
*/
|
||||
public BuildProperties(Properties entries) {
|
||||
super(processEntries(entries));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the groupId of the project or {@code null}.
|
||||
* @return the group
|
||||
*/
|
||||
public String getGroup() {
|
||||
return get("group");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the artifactId of the project or {@code null}.
|
||||
* @return the artifact
|
||||
*/
|
||||
public String getArtifact() {
|
||||
return get("artifact");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the project or {@code null}.
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return get("name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the version of the project or {@code null}.
|
||||
* @return the version
|
||||
*/
|
||||
public String getVersion() {
|
||||
return get("version");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the timestamp of the build or {@code null}.
|
||||
* <p>
|
||||
* If the original value could not be parsed properly, it is still available with
|
||||
* the {@code time} key.
|
||||
* @return the build time
|
||||
* @see #get(String)
|
||||
*/
|
||||
public Date getTime() {
|
||||
return getDate("time");
|
||||
}
|
||||
|
||||
private static Properties processEntries(Properties properties) {
|
||||
coerceDate(properties, "time");
|
||||
return properties;
|
||||
}
|
||||
|
||||
private static void coerceDate(Properties properties, String key) {
|
||||
String value = properties.getProperty(key);
|
||||
if (value != null) {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
||||
try {
|
||||
String updatedValue = String.valueOf(format.parse(value).getTime());
|
||||
properties.setProperty(key, updatedValue);
|
||||
}
|
||||
catch (ParseException ex) {
|
||||
// Ignore and store the original value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -19,7 +19,6 @@ package org.springframework.boot.info;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
@ -98,7 +97,7 @@ public class GitProperties extends InfoProperties {
|
||||
if (epoch != null) {
|
||||
return String.valueOf(epoch);
|
||||
}
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
||||
try {
|
||||
return String.valueOf(format.parse(s).getTime());
|
||||
}
|
||||
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.info;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link BuildProperties}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class BuildPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void basicInfo() {
|
||||
BuildProperties properties = new BuildProperties(createProperties(
|
||||
"com.example", "demo", "0.0.1", "2016-03-04T14:36:33+0100"));
|
||||
assertThat(properties.getGroup()).isEqualTo("com.example");
|
||||
assertThat(properties.getArtifact()).isEqualTo("demo");
|
||||
assertThat(properties.getVersion()).isEqualTo("0.0.1");
|
||||
assertThat(properties.getTime()).isNotNull();
|
||||
assertThat(properties.get("time")).isEqualTo("1457098593000");
|
||||
assertThat(properties.getTime().getTime()).isEqualTo(1457098593000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noInfo() {
|
||||
BuildProperties properties = new BuildProperties(new Properties());
|
||||
assertThat(properties.getGroup()).isNull();
|
||||
assertThat(properties.getArtifact()).isNull();
|
||||
assertThat(properties.getVersion()).isNull();
|
||||
assertThat(properties.getTime()).isNull();
|
||||
}
|
||||
|
||||
private static Properties createProperties(String group, String artifact,
|
||||
String version, String buildTime) {
|
||||
Properties properties = new Properties();
|
||||
properties.put("group", group);
|
||||
properties.put("artifact", artifact);
|
||||
properties.put("version", version);
|
||||
properties.put("time", buildTime);
|
||||
return properties;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user