Add encoding support for git and build properties

See gh-10771
pull/14673/merge
hengyunabc 7 years ago committed by Stephane Nicoll
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.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;
/**
* {@link EnableAutoConfiguration Auto-configuration} for various project information.
@ -60,7 +62,8 @@ public class ProjectInfoAutoConfiguration {
@ConditionalOnMissingBean
@Bean
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}")
@ -68,12 +71,18 @@ public class ProjectInfoAutoConfiguration {
@Bean
public BuildProperties buildProperties() throws Exception {
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 + ".";
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();
for (String key : source.stringPropertyNames()) {
if (key.startsWith(p)) {

@ -52,6 +52,11 @@ public class ProjectInfoProperties {
private Resource location = new ClassPathResource(
"META-INF/build-info.properties");
/**
* build-info.properties file encoding.
*/
private String encoding;
public Resource getLocation() {
return this.location;
}
@ -60,6 +65,14 @@ public class ProjectInfoProperties {
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");
/**
* git.properties file encoding.
*/
private String encoding;
public Resource getLocation() {
return this.location;
}
@ -80,6 +98,14 @@ public class ProjectInfoProperties {
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
public void buildPropertiesDefaultLocation() {
this.contextRunner.run((context) -> {

@ -2,3 +2,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
git.commit.unicode=中文

Loading…
Cancel
Save