Add encoding support for git and build properties
See gh-10771
This commit is contained in:
parent
73c6cc1b5d
commit
f7a4a56fba
|
|
@ -36,8 +36,10 @@ import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
import org.springframework.core.io.support.EncodedResource;
|
||||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link EnableAutoConfiguration Auto-configuration} for various project information.
|
* {@link EnableAutoConfiguration Auto-configuration} for various project information.
|
||||||
|
|
@ -60,7 +62,8 @@ public class ProjectInfoAutoConfiguration {
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@Bean
|
@Bean
|
||||||
public GitProperties gitProperties() throws Exception {
|
public GitProperties gitProperties() throws Exception {
|
||||||
return new GitProperties(loadFrom(this.properties.getGit().getLocation(), "git"));
|
return new GitProperties(loadFrom(this.properties.getGit().getLocation(), "git",
|
||||||
|
this.properties.getGit().getEncoding()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConditionalOnResource(resources = "${spring.info.build.location:classpath:META-INF/build-info.properties}")
|
@ConditionalOnResource(resources = "${spring.info.build.location:classpath:META-INF/build-info.properties}")
|
||||||
|
|
@ -68,12 +71,18 @@ public class ProjectInfoAutoConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
public BuildProperties buildProperties() throws Exception {
|
public BuildProperties buildProperties() throws Exception {
|
||||||
return new BuildProperties(
|
return new BuildProperties(
|
||||||
loadFrom(this.properties.getBuild().getLocation(), "build"));
|
loadFrom(this.properties.getBuild().getLocation(), "build",
|
||||||
|
this.properties.getBuild().getEncoding()));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Properties loadFrom(Resource location, String prefix) throws IOException {
|
protected Properties loadFrom(Resource location, String prefix, String encoding) throws IOException {
|
||||||
String p = prefix.endsWith(".") ? prefix : prefix + ".";
|
String p = prefix.endsWith(".") ? prefix : prefix + ".";
|
||||||
Properties source = PropertiesLoaderUtils.loadProperties(location);
|
Properties source = null;
|
||||||
|
if (StringUtils.isEmpty(encoding)) {
|
||||||
|
source = PropertiesLoaderUtils.loadProperties(location);
|
||||||
|
} else {
|
||||||
|
source = PropertiesLoaderUtils.loadProperties(new EncodedResource(location, encoding));
|
||||||
|
}
|
||||||
Properties target = new Properties();
|
Properties target = new Properties();
|
||||||
for (String key : source.stringPropertyNames()) {
|
for (String key : source.stringPropertyNames()) {
|
||||||
if (key.startsWith(p)) {
|
if (key.startsWith(p)) {
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,11 @@ public class ProjectInfoProperties {
|
||||||
private Resource location = new ClassPathResource(
|
private Resource location = new ClassPathResource(
|
||||||
"META-INF/build-info.properties");
|
"META-INF/build-info.properties");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build-info.properties file encoding.
|
||||||
|
*/
|
||||||
|
private String encoding;
|
||||||
|
|
||||||
public Resource getLocation() {
|
public Resource getLocation() {
|
||||||
return this.location;
|
return this.location;
|
||||||
}
|
}
|
||||||
|
|
@ -60,6 +65,14 @@ public class ProjectInfoProperties {
|
||||||
this.location = location;
|
this.location = location;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getEncoding() {
|
||||||
|
return this.encoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEncoding(String encoding) {
|
||||||
|
this.encoding = encoding;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -72,6 +85,11 @@ public class ProjectInfoProperties {
|
||||||
*/
|
*/
|
||||||
private Resource location = new ClassPathResource("git.properties");
|
private Resource location = new ClassPathResource("git.properties");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* git.properties file encoding.
|
||||||
|
*/
|
||||||
|
private String encoding;
|
||||||
|
|
||||||
public Resource getLocation() {
|
public Resource getLocation() {
|
||||||
return this.location;
|
return this.location;
|
||||||
}
|
}
|
||||||
|
|
@ -80,6 +98,14 @@ public class ProjectInfoProperties {
|
||||||
this.location = location;
|
this.location = location;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getEncoding() {
|
||||||
|
return this.encoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEncoding(String encoding) {
|
||||||
|
this.encoding = encoding;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,14 @@ public class ProjectInfoAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void gitPropertiesWithUnicode() {
|
||||||
|
load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties",
|
||||||
|
"spring.info.git.encoding=utf-8");
|
||||||
|
GitProperties gitProperties = this.context.getBean(GitProperties.class);
|
||||||
|
assertThat(gitProperties.get("commit.unicode")).isEqualTo("中文");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void buildPropertiesDefaultLocation() {
|
public void buildPropertiesDefaultLocation() {
|
||||||
this.contextRunner.run((context) -> {
|
this.contextRunner.run((context) -> {
|
||||||
|
|
|
||||||
|
|
@ -2,3 +2,4 @@ git.commit.user.email=john@example.com
|
||||||
git.commit.id=f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a
|
git.commit.id=f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a
|
||||||
git.commit.user.name=John Smith
|
git.commit.user.name=John Smith
|
||||||
git.commit.time=2016-03-03T10\:02\:00+0100
|
git.commit.time=2016-03-03T10\:02\:00+0100
|
||||||
|
git.commit.unicode=中文
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue