Auto-configure GitInfo

This commit moves `GitInfo` to a general "project info" area that will be
further improved with others project related information.

Deprecate `spring.git.properties` in favour of `spring.info.git.location`

Closes gh-2484
This commit is contained in:
Stephane Nicoll
2016-03-03 14:31:00 +01:00
parent 03ecdce4fb
commit 0f820afa86
10 changed files with 368 additions and 74 deletions

View File

@@ -0,0 +1,69 @@
/*
* 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.autoconfigure.info;
/**
* Provide git-related information such as commit id and time.
*
* @author Dave Syer
* @author Stephane Nicoll
* @since 1.4.0
*/
public class GitInfo {
private String branch;
private final Commit commit = new Commit();
public String getBranch() {
return this.branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public Commit getCommit() {
return this.commit;
}
public static class Commit {
private String id;
private String time;
public String getId() {
return this.id == null ? ""
: (this.id.length() > 7 ? this.id.substring(0, 7) : this.id);
}
public void setId(String id) {
this.id = id;
}
public String getTime() {
return this.time;
}
public void setTime(String time) {
this.time = time;
}
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.autoconfigure.info;
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.SpringBootCondition;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* {@link EnableAutoConfiguration Auto-configuration} for various project information.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
@Configuration
@EnableConfigurationProperties(ProjectInfoProperties.class)
public class ProjectInfoAutoConfiguration {
@Configuration
@Conditional(GitResourceAvailableCondition.class)
protected static class GitInfoAutoConfiguration {
@ConditionalOnMissingBean
@Bean
public GitInfo gitInfo(ProjectInfoProperties properties) throws Exception {
PropertiesConfigurationFactory<GitInfo> factory = new PropertiesConfigurationFactory<GitInfo>(
new GitInfo());
factory.setTargetName("git");
Properties gitInfoProperties = PropertiesLoaderUtils
.loadProperties(properties.getGit().getLocation());
factory.setProperties(gitInfoProperties);
return factory.getObject();
}
}
static class GitResourceAvailableCondition extends SpringBootCondition {
private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader();
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
ResourceLoader loader = context.getResourceLoader() == null
? this.defaultResourceLoader : context.getResourceLoader();
PropertyResolver propertyResolver = context.getEnvironment();
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(propertyResolver, "spring.info.git.");
String location = resolver.getProperty("location");
if (location == null) {
resolver = new RelaxedPropertyResolver(propertyResolver, "spring.git.");
location = resolver.getProperty("properties");
if (location == null) {
location = "classpath:git.properties";
}
}
boolean match = loader.getResource(location).exists();
return new ConditionOutcome(match, "Git info " + (match ? "found" : "not found") + " at " + location);
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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.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.Resource;
/**
* Configuration properties for project information.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
@ConfigurationProperties("spring.info")
public class ProjectInfoProperties {
private final Git git = new Git();
public Git getGit() {
return this.git;
}
/**
* Make sure that the "spring.git.properties" legacy key is used by default.
* @param defaultGitLocation the default git location to use
*/
@Autowired
void setDefaultGitLocation(@Value("${spring.git.properties:classpath:git.properties}") Resource defaultGitLocation) {
getGit().setLocation(defaultGitLocation);
}
/**
* Git specific info properties.
*/
public static class Git {
/**
* Location of the generated git info properties file.
*/
private Resource location;
public Resource getLocation() {
return this.location;
}
public void setLocation(Resource location) {
this.location = location;
}
}
}

View File

@@ -0,0 +1,117 @@
/*
* 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.autoconfigure.info;
import java.util.Map;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ProjectInfoAutoConfiguration}.
*
* @author Stephane Nicoll
*/
public class ProjectInfoAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void gitInfoUnavailableIfResourceNotAvailable() {
load();
Map<String, GitInfo> beans = this.context
.getBeansOfType(GitInfo.class);
assertThat(beans).hasSize(0);
}
@Test
public void gitLocationTakesPrecedenceOverLegacyKey() {
load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties",
"spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class);
assertGitInfo(gitInfo);
}
@Test
public void gitLegacyKeyIsUsedAsFallback() {
load("spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class);
assertGitInfo(gitInfo);
}
@Test
public void gitInfoWithNoData() {
load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class);
assertThat(gitInfo.getBranch()).isNull();
}
@Test
public void gitInfoFallbackWithGitInfoBean() {
load(CustomGitInfoConfiguration.class,
"spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class);
assertThat(gitInfo).isSameAs(this.context.getBean("customGitInfo"));
}
private void assertGitInfo(GitInfo gitInfo) {
assertThat(gitInfo.getBranch()).isNull();
assertThat(gitInfo.getCommit().getId()).isEqualTo("f95038e");
assertThat(gitInfo.getCommit().getTime()).isEqualTo("2016-03-03T10:02:00+0100");
}
private void load(String... environment) {
load(null, environment);
}
private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
if (config != null) {
context.register(config);
}
context.register(PropertyPlaceholderAutoConfiguration.class,
ProjectInfoAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(context, environment);
context.refresh();
this.context = context;
}
@Configuration
static class CustomGitInfoConfiguration {
@Bean
public GitInfo customGitInfo() {
return new GitInfo();
}
}
}

View File

@@ -0,0 +1,4 @@
git.commit.user.email=john@example.com
git.commit.id=f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a
git.commit.user.name=John Smith
git.commit.time=2016-03-03T10\:02\:00+0100