Skip to content

Commit f7a4a56

Browse files
hengyunabcsnicoll
authored andcommitted
Add encoding support for git and build properties
See gh-10771
1 parent 73c6cc1 commit f7a4a56

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import org.springframework.core.io.DefaultResourceLoader;
3737
import org.springframework.core.io.Resource;
3838
import org.springframework.core.io.ResourceLoader;
39+
import org.springframework.core.io.support.EncodedResource;
3940
import org.springframework.core.io.support.PropertiesLoaderUtils;
4041
import org.springframework.core.type.AnnotatedTypeMetadata;
42+
import org.springframework.util.StringUtils;
4143

4244
/**
4345
* {@link EnableAutoConfiguration Auto-configuration} for various project information.
@@ -60,20 +62,27 @@ public ProjectInfoAutoConfiguration(ProjectInfoProperties properties) {
6062
@ConditionalOnMissingBean
6163
@Bean
6264
public GitProperties gitProperties() throws Exception {
63-
return new GitProperties(loadFrom(this.properties.getGit().getLocation(), "git"));
65+
return new GitProperties(loadFrom(this.properties.getGit().getLocation(), "git",
66+
this.properties.getGit().getEncoding()));
6467
}
6568

6669
@ConditionalOnResource(resources = "${spring.info.build.location:classpath:META-INF/build-info.properties}")
6770
@ConditionalOnMissingBean
6871
@Bean
6972
public BuildProperties buildProperties() throws Exception {
7073
return new BuildProperties(
71-
loadFrom(this.properties.getBuild().getLocation(), "build"));
74+
loadFrom(this.properties.getBuild().getLocation(), "build",
75+
this.properties.getBuild().getEncoding()));
7276
}
7377

74-
protected Properties loadFrom(Resource location, String prefix) throws IOException {
78+
protected Properties loadFrom(Resource location, String prefix, String encoding) throws IOException {
7579
String p = prefix.endsWith(".") ? prefix : prefix + ".";
76-
Properties source = PropertiesLoaderUtils.loadProperties(location);
80+
Properties source = null;
81+
if (StringUtils.isEmpty(encoding)) {
82+
source = PropertiesLoaderUtils.loadProperties(location);
83+
} else {
84+
source = PropertiesLoaderUtils.loadProperties(new EncodedResource(location, encoding));
85+
}
7786
Properties target = new Properties();
7887
for (String key : source.stringPropertyNames()) {
7988
if (key.startsWith(p)) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public static class Build {
5252
private Resource location = new ClassPathResource(
5353
"META-INF/build-info.properties");
5454

55+
/**
56+
* build-info.properties file encoding.
57+
*/
58+
private String encoding;
59+
5560
public Resource getLocation() {
5661
return this.location;
5762
}
@@ -60,6 +65,14 @@ public void setLocation(Resource location) {
6065
this.location = location;
6166
}
6267

68+
public String getEncoding() {
69+
return this.encoding;
70+
}
71+
72+
public void setEncoding(String encoding) {
73+
this.encoding = encoding;
74+
}
75+
6376
}
6477

6578
/**
@@ -72,6 +85,11 @@ public static class Git {
7285
*/
7386
private Resource location = new ClassPathResource("git.properties");
7487

88+
/**
89+
* git.properties file encoding.
90+
*/
91+
private String encoding;
92+
7593
public Resource getLocation() {
7694
return this.location;
7795
}
@@ -80,6 +98,14 @@ public void setLocation(Resource location) {
8098
this.location = location;
8199
}
82100

101+
public String getEncoding() {
102+
return this.encoding;
103+
}
104+
105+
public void setEncoding(String encoding) {
106+
this.encoding = encoding;
107+
}
108+
83109
}
84110

85111
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public void gitPropertiesFallbackWithGitPropertiesBean() {
7171
});
7272
}
7373

74+
@Test
75+
public void gitPropertiesWithUnicode() {
76+
load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties",
77+
"spring.info.git.encoding=utf-8");
78+
GitProperties gitProperties = this.context.getBean(GitProperties.class);
79+
assertThat(gitProperties.get("commit.unicode")).isEqualTo("中文");
80+
}
81+
7482
@Test
7583
public void buildPropertiesDefaultLocation() {
7684
this.contextRunner.run((context) -> {

spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/git.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ [email protected]
22
git.commit.id=f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a
33
git.commit.user.name=John Smith
44
git.commit.time=2016-03-03T10\:02\:00+0100
5+
git.commit.unicode=中文

0 commit comments

Comments
 (0)