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 1b08c66c76b..3d7427f3b60 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 @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.info; import java.io.IOException; +import java.nio.charset.Charset; import java.util.Properties; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -39,7 +40,6 @@ 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. @@ -70,19 +70,14 @@ public class ProjectInfoAutoConfiguration { @ConditionalOnMissingBean @Bean public BuildProperties buildProperties() throws Exception { - return new BuildProperties( - loadFrom(this.properties.getBuild().getLocation(), "build", - this.properties.getBuild().getEncoding())); + return new BuildProperties(loadFrom(this.properties.getBuild().getLocation(), + "build", this.properties.getBuild().getEncoding())); } - protected Properties loadFrom(Resource location, String prefix, String encoding) throws IOException { + protected Properties loadFrom(Resource location, String prefix, Charset encoding) + throws IOException { String p = prefix.endsWith(".") ? prefix : prefix + "."; - Properties source = null; - if (StringUtils.isEmpty(encoding)) { - source = PropertiesLoaderUtils.loadProperties(location); - } else { - source = PropertiesLoaderUtils.loadProperties(new EncodedResource(location, encoding)); - } + Properties source = loadSource(location, encoding); Properties target = new Properties(); for (String key : source.stringPropertyNames()) { if (key.startsWith(p)) { @@ -92,6 +87,17 @@ public class ProjectInfoAutoConfiguration { return target; } + private Properties loadSource(Resource location, Charset encoding) + throws IOException { + if (encoding != null) { + return PropertiesLoaderUtils + .loadProperties(new EncodedResource(location, encoding)); + } + else { + return PropertiesLoaderUtils.loadProperties(location); + } + } + static class GitResourceAvailableCondition extends SpringBootCondition { private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader(); 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 0aca49e5de5..763a8be22fd 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure.info; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -53,9 +56,9 @@ public class ProjectInfoProperties { "META-INF/build-info.properties"); /** - * build-info.properties file encoding. + * File encoding. */ - private String encoding; + private Charset encoding = StandardCharsets.UTF_8; public Resource getLocation() { return this.location; @@ -65,11 +68,11 @@ public class ProjectInfoProperties { this.location = location; } - public String getEncoding() { + public Charset getEncoding() { return this.encoding; } - public void setEncoding(String encoding) { + public void setEncoding(Charset encoding) { this.encoding = encoding; } @@ -86,9 +89,9 @@ public class ProjectInfoProperties { private Resource location = new ClassPathResource("git.properties"); /** - * git.properties file encoding. + * File encoding. */ - private String encoding; + private Charset encoding = StandardCharsets.UTF_8; public Resource getLocation() { return this.location; @@ -98,11 +101,11 @@ public class ProjectInfoProperties { this.location = location; } - public String getEncoding() { + public Charset getEncoding() { return this.encoding; } - public void setEncoding(String encoding) { + public void setEncoding(Charset 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 a05878d3c81..af16c2efdba 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 @@ -72,11 +72,23 @@ 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("中文"); + public void gitPropertiesUsesUtf8ByDefault() { + this.contextRunner.withPropertyValues( + "spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties") + .run((context) -> { + GitProperties gitProperties = context.getBean(GitProperties.class); + assertThat(gitProperties.get("commit.charset")).isEqualTo("test™"); + }); + } + + @Test + public void gitPropertiesEncodingCanBeConfigured() { + this.contextRunner.withPropertyValues("spring.info.git.encoding=US-ASCII", + "spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties") + .run((context) -> { + GitProperties gitProperties = context.getBean(GitProperties.class); + assertThat(gitProperties.get("commit.charset")).isNotEqualTo("test™"); + }); } @Test @@ -128,6 +140,28 @@ public class ProjectInfoAutoConfigurationTests { }); } + @Test + public void buildPropertiesUsesUtf8ByDefault() { + this.contextRunner.withPropertyValues( + "spring.info.build.location=classpath:/org/springframework/boot/autoconfigure/info/build-info.properties") + .run((context) -> { + BuildProperties buildProperties = context + .getBean(BuildProperties.class); + assertThat(buildProperties.get("charset")).isEqualTo("test™"); + }); + } + + @Test + public void buildPropertiesEncodingCanBeConfigured() { + this.contextRunner.withPropertyValues("spring.info.build.encoding=US-ASCII", + "spring.info.build.location=classpath:/org/springframework/boot/autoconfigure/info/build-info.properties") + .run((context) -> { + BuildProperties buildProperties = context + .getBean(BuildProperties.class); + assertThat(buildProperties.get("charset")).isNotEqualTo("test™"); + }); + } + @Configuration static class CustomInfoPropertiesConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build-info.properties b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build-info.properties index 877f2f57ead..6c4b010d02f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build-info.properties +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build-info.properties @@ -3,3 +3,4 @@ build.artifact=acme build.name=acme build.version=1.0.1-SNAPSHOT build.time=2016-03-04T10:42:00.000Z +build.charset=test™ 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 7d930874325..5d3f26a1d9e 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,4 +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=中文 +git.commit.charset=test™ diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index ea5b8de2daf..68ecfde0cba 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -98,7 +98,9 @@ content into your application. Rather, pick only the properties that you need. spring.hazelcast.config= # The location of the configuration file to use to initialize Hazelcast. # PROJECT INFORMATION ({sc-spring-boot-autoconfigure}/info/ProjectInfoProperties.{sc-ext}[ProjectInfoProperties]) + spring.info.build.encoding=UTF-8 # File encoding. spring.info.build.location=classpath:META-INF/build-info.properties # Location of the generated build-info.properties file. + spring.info.git.encoding=UTF-8 # File encoding. spring.info.git.location=classpath:git.properties # Location of the generated git.properties file. # JMX