Commit eacea1af authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #10771 from hengyunabc

* pr/10771:
  Polish "Add encoding support for git and build properties"
  Add encoding support for git and build properties
parents 73c6cc1b c91d9bfd
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.info; package org.springframework.boot.autoconfigure.info;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties; import java.util.Properties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
...@@ -36,6 +37,7 @@ import org.springframework.core.env.Environment; ...@@ -36,6 +37,7 @@ 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;
...@@ -60,20 +62,22 @@ public class ProjectInfoAutoConfiguration { ...@@ -60,20 +62,22 @@ 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}")
@ConditionalOnMissingBean @ConditionalOnMissingBean
@Bean @Bean
public BuildProperties buildProperties() throws Exception { public BuildProperties buildProperties() throws Exception {
return new BuildProperties( return new BuildProperties(loadFrom(this.properties.getBuild().getLocation(),
loadFrom(this.properties.getBuild().getLocation(), "build")); "build", this.properties.getBuild().getEncoding()));
} }
protected Properties loadFrom(Resource location, String prefix) throws IOException { protected Properties loadFrom(Resource location, String prefix, Charset encoding)
throws IOException {
String p = prefix.endsWith(".") ? prefix : prefix + "."; String p = prefix.endsWith(".") ? prefix : prefix + ".";
Properties source = PropertiesLoaderUtils.loadProperties(location); Properties source = loadSource(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)) {
...@@ -83,6 +87,17 @@ public class ProjectInfoAutoConfiguration { ...@@ -83,6 +87,17 @@ public class ProjectInfoAutoConfiguration {
return target; 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 { static class GitResourceAvailableCondition extends SpringBootCondition {
private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader(); private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader();
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure.info; 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.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
...@@ -52,6 +55,11 @@ public class ProjectInfoProperties { ...@@ -52,6 +55,11 @@ public class ProjectInfoProperties {
private Resource location = new ClassPathResource( private Resource location = new ClassPathResource(
"META-INF/build-info.properties"); "META-INF/build-info.properties");
/**
* File encoding.
*/
private Charset encoding = StandardCharsets.UTF_8;
public Resource getLocation() { public Resource getLocation() {
return this.location; return this.location;
} }
...@@ -60,6 +68,14 @@ public class ProjectInfoProperties { ...@@ -60,6 +68,14 @@ public class ProjectInfoProperties {
this.location = location; this.location = location;
} }
public Charset getEncoding() {
return this.encoding;
}
public void setEncoding(Charset encoding) {
this.encoding = encoding;
}
} }
/** /**
...@@ -72,6 +88,11 @@ public class ProjectInfoProperties { ...@@ -72,6 +88,11 @@ public class ProjectInfoProperties {
*/ */
private Resource location = new ClassPathResource("git.properties"); private Resource location = new ClassPathResource("git.properties");
/**
* File encoding.
*/
private Charset encoding = StandardCharsets.UTF_8;
public Resource getLocation() { public Resource getLocation() {
return this.location; return this.location;
} }
...@@ -80,6 +101,14 @@ public class ProjectInfoProperties { ...@@ -80,6 +101,14 @@ public class ProjectInfoProperties {
this.location = location; this.location = location;
} }
public Charset getEncoding() {
return this.encoding;
}
public void setEncoding(Charset encoding) {
this.encoding = encoding;
}
} }
} }
...@@ -71,6 +71,26 @@ public class ProjectInfoAutoConfigurationTests { ...@@ -71,6 +71,26 @@ public class ProjectInfoAutoConfigurationTests {
}); });
} }
@Test
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 @Test
public void buildPropertiesDefaultLocation() { public void buildPropertiesDefaultLocation() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
...@@ -120,6 +140,28 @@ public class ProjectInfoAutoConfigurationTests { ...@@ -120,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 @Configuration
static class CustomInfoPropertiesConfiguration { static class CustomInfoPropertiesConfiguration {
......
...@@ -3,3 +3,4 @@ build.artifact=acme ...@@ -3,3 +3,4 @@ build.artifact=acme
build.name=acme build.name=acme
build.version=1.0.1-SNAPSHOT build.version=1.0.1-SNAPSHOT
build.time=2016-03-04T10:42:00.000Z build.time=2016-03-04T10:42:00.000Z
build.charset=test™
...@@ -2,3 +2,4 @@ git.commit.user.email=john@example.com ...@@ -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.charset=test™
...@@ -98,7 +98,9 @@ content into your application. Rather, pick only the properties that you need. ...@@ -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. 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]) # 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.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. spring.info.git.location=classpath:git.properties # Location of the generated git.properties file.
# JMX # JMX
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment