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
pull/14673/merge
Stephane Nicoll 6 years ago
commit eacea1af46

@ -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;
@ -36,6 +37,7 @@ 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;
@ -60,20 +62,22 @@ 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}")
@ConditionalOnMissingBean
@Bean
public BuildProperties buildProperties() throws Exception {
return new BuildProperties(
loadFrom(this.properties.getBuild().getLocation(), "build"));
return new BuildProperties(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, Charset encoding)
throws IOException {
String p = prefix.endsWith(".") ? prefix : prefix + ".";
Properties source = PropertiesLoaderUtils.loadProperties(location);
Properties source = loadSource(location, encoding);
Properties target = new Properties();
for (String key : source.stringPropertyNames()) {
if (key.startsWith(p)) {
@ -83,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();

@ -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;
@ -52,6 +55,11 @@ public class ProjectInfoProperties {
private Resource location = new ClassPathResource(
"META-INF/build-info.properties");
/**
* File encoding.
*/
private Charset encoding = StandardCharsets.UTF_8;
public Resource getLocation() {
return this.location;
}
@ -60,6 +68,14 @@ public class ProjectInfoProperties {
this.location = location;
}
public Charset getEncoding() {
return this.encoding;
}
public void setEncoding(Charset encoding) {
this.encoding = encoding;
}
}
/**
@ -72,6 +88,11 @@ public class ProjectInfoProperties {
*/
private Resource location = new ClassPathResource("git.properties");
/**
* File encoding.
*/
private Charset encoding = StandardCharsets.UTF_8;
public Resource getLocation() {
return this.location;
}
@ -80,6 +101,14 @@ public class ProjectInfoProperties {
this.location = location;
}
public Charset getEncoding() {
return this.encoding;
}
public void setEncoding(Charset encoding) {
this.encoding = encoding;
}
}
}

@ -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
public void buildPropertiesDefaultLocation() {
this.contextRunner.run((context) -> {
@ -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
static class CustomInfoPropertiesConfiguration {

@ -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™

@ -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.charset=test™

@ -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

Loading…
Cancel
Save