diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java index 8be72a8577..1b08c66c76 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java @@ -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)) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java index beca19685a..0aca49e5de 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java @@ -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; + } + } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java index 9905a36060..a05878d3c8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java @@ -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) -> { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/git.properties b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/git.properties index 2f49e6f821..7d93087432 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/git.properties +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/git.properties @@ -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=中文