Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
c91d9bfd
Commit
c91d9bfd
authored
Oct 10, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Add encoding support for git and build properties"
Closes gh-10771
parent
f7a4a56f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
72 additions
and
26 deletions
+72
-26
ProjectInfoAutoConfiguration.java
...boot/autoconfigure/info/ProjectInfoAutoConfiguration.java
+17
-11
ProjectInfoProperties.java
...mework/boot/autoconfigure/info/ProjectInfoProperties.java
+12
-9
ProjectInfoAutoConfigurationTests.java
...autoconfigure/info/ProjectInfoAutoConfigurationTests.java
+39
-5
build-info.properties
...ngframework/boot/autoconfigure/info/build-info.properties
+1
-0
git.properties
...rg/springframework/boot/autoconfigure/info/git.properties
+1
-1
appendix-application-properties.adoc
...cs/src/main/asciidoc/appendix-application-properties.adoc
+2
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.java
View file @
c91d9bfd
...
...
@@ -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
();
...
...
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/info/ProjectInfoProperties.java
View file @
c91d9bfd
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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 f
ile encoding.
*
F
ile 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 f
ile encoding.
*
F
ile 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
;
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java
View file @
c91d9bfd
...
...
@@ -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
{
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/build-info.properties
View file @
c91d9bfd
...
...
@@ -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™
spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/info/git.properties
View file @
c91d9bfd
...
...
@@ -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™
spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc
View file @
c91d9bfd
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment