spring_reference/V. Spring Boot Actuator/40.4.1. Automatically expan...

56 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 40.4.1. 在构建时期自动扩展info属性
你可以使用已经存在的构建配置自动扩展info属性而不是对在项目构建配置中存在的属性进行硬编码。这在Maven和Gradle都是可能的。
**使用Maven自动扩展属性**
对于Maven项目你可以使用资源过滤来自动扩展info属性。如果使用spring-boot-starter-parent你可以通过`@..@`占位符引用Maven的'project properties'。
```java
project.artifactId=myproject
project.name=Demo
project.version=X.X.X.X
project.description=Demo project for info endpoint
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@
```
**注**在上面的示例中我们使用project.*来设置一些值以防止由于某些原因Maven的资源过滤没有开启。Maven目标`spring-boot:run`直接将`src/main/resources`添加到classpath下出于热加载的目的。这就绕过了资源过滤和自动扩展属性的特性。你可以使用`exec:java`替换该目标或自定义插件的配置,具体参考[plugin usage page](http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/maven-plugin/usage.html)。
如果你不使用starter parent在你的pom.xml你需要添加处于<build/>元素内):
```xml
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
```
和(处于<plugins/>内):
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
</configuration>
</plugin>
```
**使用Gradle自动扩展属性**
通过配置Java插件的processResources任务你也可以自动使用来自Gradle项目的属性扩展info属性。
```java
processResources {
expand(project.properties)
}
```
然后你可以通过占位符引用Gradle项目的属性
```java
info.build.name=${name}
info.build.description=${description}
info.build.version=${version}
```