diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfiguration.java index 2bf3ffc1a0..2a46aeec91 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfiguration.java @@ -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()); + } + } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorProperties.java index 26a2ecc694..f79de0632e 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/InfoContributorProperties.java @@ -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 { /** diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/BuildInfoContributor.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/BuildInfoContributor.java new file mode 100644 index 0000000000..2142ae870e --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/BuildInfoContributor.java @@ -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 { + + 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 content) { + replaceValue(content, "time", getProperties().getTime()); + } + +} diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java index de71ee2330..17458e76f6 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java @@ -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 { 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 extractContent() { - MutablePropertySources propertySources = new MutablePropertySources(); - propertySources.addFirst(toPropertySources()); - Map 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 content) { - replaceValue(getNestedMap(content, "commit"), "time", getProperties().getDate("commit.time")); + protected void postProcessContent(Map 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 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 getNestedMap(Map map, String key) { - Object o = map.get(key); - if (o == null) { - return Collections.emptyMap(); - } - else { - return (Map) 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 - - } - } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/InfoPropertiesInfoContributor.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/InfoPropertiesInfoContributor.java new file mode 100644 index 0000000000..b9d73313e7 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/InfoPropertiesInfoContributor.java @@ -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 the type of the {@link InfoProperties} to expose + * @author Stephane Nicoll + * @since 1.4.0 + */ +public abstract class InfoPropertiesInfoContributor 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 generateContent() { + Map 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 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 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 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 getNestedMap(Map map, String key) { + Object o = map.get(key); + if (o == null) { + return Collections.emptyMap(); + } + else { + return (Map) 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 + + } + +} diff --git a/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 674c2c69da..c500bb9b1a 100644 --- a/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfigurationTests.java index cb88118d8b..cd580ffe01 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/InfoContributorAutoConfigurationTests.java @@ -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 beans = this.context + .getBeansOfType(InfoContributor.class); + assertThat(beans).containsKeys("buildInfoContributor"); + Map content = + invokeContributor(this.context.getBean("buildInfoContributor", InfoContributor.class)); + Object build = content.get("build"); + assertThat(build).isInstanceOf(Map.class); + Map gitInfo = (Map) build; + assertThat(gitInfo).containsOnlyKeys("group", "artifact"); + } + + @SuppressWarnings("unchecked") + @Test + public void buildPropertiesFullMode() { + load(BuildPropertiesConfiguration.class, "management.info.build.mode=full"); + Map content = + invokeContributor(this.context.getBean("buildInfoContributor", InfoContributor.class)); + Object build = content.get("build"); + assertThat(build).isInstanceOf(Map.class); + Map gitInfo = (Map) 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 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())); + } + + } + } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/info/GitInfoContributorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/info/GitInfoContributorTests.java index fc936dd597..e377a42673 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/info/GitInfoContributorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/info/GitInfoContributorTests.java @@ -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 content = contributor.extractContent(); + Map content = contributor.generateContent(); assertThat(content.get("commit")).isInstanceOf(Map.class); Map commit = (Map) 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 content = contributor.extractContent(); + Map content = contributor.generateContent(); assertThat(content.get("commit")).isInstanceOf(Map.class); Map commit = (Map) content.get("commit"); assertThat(commit.get("id")).isEqualTo("8e29a0b"); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java index 25be81b77d..33b669a3f1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java @@ -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(); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java index f9770e412a..a94a8faeff 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java @@ -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; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java index 938c663f9e..4a4385e00c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java @@ -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 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()); + } + } } diff --git a/spring-boot-autoconfigure/src/test/resources/META-INF/boot/build.properties b/spring-boot-autoconfigure/src/test/resources/META-INF/boot/build.properties new file mode 100644 index 0000000000..c26eab56a9 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/META-INF/boot/build.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 diff --git a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build.properties b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build.properties new file mode 100644 index 0000000000..4550755536 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build.properties @@ -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 diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index a7d08862da..dea904e6bc 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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. diff --git a/spring-boot-samples/spring-boot-sample-actuator/pom.xml b/spring-boot-samples/spring-boot-sample-actuator/pom.xml index 3e9ec6ab5e..2f4c08177a 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/pom.xml +++ b/spring-boot-samples/spring-boot-sample-actuator/pom.xml @@ -71,6 +71,22 @@ true + + + generate build info + + generate-build-info + + + + ${project.build.sourceEncoding} + ${project.reporting.outputEncoding} + ${maven.compiler.source} + ${maven.compiler.target} + + + + diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties index b0d6a08213..8b0f97f092 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties @@ -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,\ diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java index d2229a5407..a65fce50e3 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java @@ -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 diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/pom.xml b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/pom.xml new file mode 100644 index 0000000000..a02e655ec9 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + org.springframework.boot.maven.it + generate-build-info-additional-properties + 0.0.1.BUILD-SNAPSHOT + Generate build info + + UTF-8 + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + generate-build-info + + + + bar + ${project.build.sourceEncoding} + 1.8 + + + + + + + + + + org.springframework + spring-context + @spring.version@ + + + javax.servlet + javax.servlet-api + @servlet-api.version@ + provided + + + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/src/main/java/org/test/SampleApplication.java b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..e8784d4593 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/src/main/java/org/test/SampleApplication.java @@ -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) { + } + +} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/verify.groovy new file mode 100644 index 0000000000..fd0c4b364a --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-additional-properties/verify.groovy @@ -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') \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/pom.xml b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/pom.xml new file mode 100644 index 0000000000..24f7106159 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + org.springframework.boot.maven.it + generate-build-info-custom-file + 0.0.1.BUILD-SNAPSHOT + Generate custom build info + + UTF-8 + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + generate-build-info + + + ${project.build.directory}/build.info + + + + + + + + + org.springframework + spring-context + @spring.version@ + + + javax.servlet + javax.servlet-api + @servlet-api.version@ + provided + + + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/src/main/java/org/test/SampleApplication.java b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..e8784d4593 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/src/main/java/org/test/SampleApplication.java @@ -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) { + } + +} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/verify.groovy new file mode 100644 index 0000000000..97ef5521f8 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info-cutom-file/verify.groovy @@ -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') \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/pom.xml b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/pom.xml new file mode 100644 index 0000000000..cec8f5ca22 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + org.springframework.boot.maven.it + generate-build-info + 0.0.1.BUILD-SNAPSHOT + Generate build info + + UTF-8 + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + generate-build-info + + + + + + + + + org.springframework + spring-context + @spring.version@ + + + javax.servlet + javax.servlet-api + @servlet-api.version@ + provided + + + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/src/main/java/org/test/SampleApplication.java b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..e8784d4593 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/src/main/java/org/test/SampleApplication.java @@ -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) { + } + +} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/verify.groovy new file mode 100644 index 0000000000..e7e477e908 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/generate-build-info/verify.groovy @@ -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') \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/GenerateBuildInfoMojo.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/GenerateBuildInfoMojo.java new file mode 100644 index 0000000000..7c4c5602c3 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/GenerateBuildInfoMojo.java @@ -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 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 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() + "'"); + } + } + +} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java index 390c1085eb..b7e588d1ef 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java @@ -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; diff --git a/spring-boot/src/main/java/org/springframework/boot/info/BuildProperties.java b/spring-boot/src/main/java/org/springframework/boot/info/BuildProperties.java new file mode 100644 index 0000000000..6b37863e73 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/info/BuildProperties.java @@ -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}. + *

+ * 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 + } + } + } + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java b/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java index 2468eaa474..3b8e7deb27 100644 --- a/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java +++ b/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java @@ -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()); } diff --git a/spring-boot/src/test/java/org/springframework/boot/info/BuildPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/info/BuildPropertiesTests.java new file mode 100644 index 0000000000..59ae0438e0 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/info/BuildPropertiesTests.java @@ -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; + } + +}