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

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* 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.
@@ -23,13 +23,11 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import liquibase.integration.spring.SpringLiquibase;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.AutoConfigurationReportEndpoint;
import org.springframework.boot.actuate.endpoint.BeansEndpoint;
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint;
@@ -59,6 +57,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.info.GitInfo;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -67,8 +66,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
/**
@@ -90,6 +87,9 @@ public class EndpointAutoConfiguration {
@Autowired
private InfoPropertiesConfiguration properties;
@Autowired(required = false)
private GitInfo gitInfo;
@Autowired(required = false)
private HealthAggregator healthAggregator = new OrderedHealthAggregator();
@@ -125,9 +125,8 @@ public class EndpointAutoConfiguration {
public InfoEndpoint infoEndpoint() throws Exception {
LinkedHashMap<String, Object> info = new LinkedHashMap<String, Object>();
info.putAll(this.properties.infoMap());
GitInfo gitInfo = this.properties.gitInfo();
if (gitInfo.getBranch() != null) {
info.put("git", gitInfo);
if (this.gitInfo != null && this.gitInfo.getBranch() != null) {
info.put("git", this.gitInfo);
}
return new InfoEndpoint(info);
}
@@ -219,21 +218,6 @@ public class EndpointAutoConfiguration {
@Autowired
private final ConfigurableEnvironment environment = new StandardEnvironment();
@Value("${spring.git.properties:classpath:git.properties}")
private Resource gitProperties;
public GitInfo gitInfo() throws Exception {
PropertiesConfigurationFactory<GitInfo> factory = new PropertiesConfigurationFactory<GitInfo>(
new GitInfo());
factory.setTargetName("git");
Properties properties = new Properties();
if (this.gitProperties.exists()) {
properties = PropertiesLoaderUtils.loadProperties(this.gitProperties);
}
factory.setProperties(properties);
return factory.getObject();
}
public Map<String, Object> infoMap() throws Exception {
PropertiesConfigurationFactory<Map<String, Object>> factory = new PropertiesConfigurationFactory<Map<String, Object>>(
new LinkedHashMap<String, Object>());
@@ -244,49 +228,4 @@ public class EndpointAutoConfiguration {
}
public static 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

@@ -126,7 +126,10 @@
{
"name": "spring.git.properties",
"type": "java.lang.String",
"description": "Resource reference to a generated git info properties file."
"description": "Resource reference to a generated git info properties file.",
"deprecation": {
"replacement": "spring.info.git.location"
}
},
{
"name": "spring.pid.file",

View File

@@ -40,6 +40,7 @@ import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
@@ -140,7 +141,7 @@ public class EndpointAutoConfigurationTests {
public void testInfoEndpointConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "info.foo:bar");
this.context.register(EndpointAutoConfiguration.class);
this.context.register(ProjectInfoAutoConfiguration.class, EndpointAutoConfiguration.class);
this.context.refresh();
InfoEndpoint endpoint = this.context.getBean(InfoEndpoint.class);
assertThat(endpoint).isNotNull();
@@ -152,7 +153,7 @@ public class EndpointAutoConfigurationTests {
public void testNoGitProperties() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.git.properties:classpath:nonexistent");
"spring.info.git.location:classpath:nonexistent");
this.context.register(EndpointAutoConfiguration.class);
this.context.refresh();
InfoEndpoint endpoint = this.context.getBean(InfoEndpoint.class);