1. 簡介
通常,我們的項目構建配置包含大量關於應用程序的信息。其中一些信息可能需要在應用程序本身中使用。因此,與其硬編碼這些信息,不如從現有的構建配置中獲取它們。
在本教程中,我們將學習如何在 Spring Boot 應用程序中使用項目構建配置中的信息。
2. 構建信息
假設我們希望在網站主頁上顯示應用程序描述和版本信息。
通常,這些信息存在於 <em pom.xml</em> 中:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot</artifactId>
<name>spring-boot</name>
<packaging>war</packaging>
<description>This is simple boot application for Spring boot actuator test</description>
<version>0.0.1-SNAPSHOT</version>
...
</project>3. 引用應用程序屬性文件中的信息
現在,為了在我們的應用程序中使用上述信息,我們需要首先在我們的應用程序屬性文件中引用它:
[email protected]@
[email protected]@這裏,我們使用構建屬性 project.description 來設置應用程序屬性 application-description。 類似地,application-version 使用 project.version 進行設置。
此處最重要的部分是圍繞屬性名稱使用 > 字符。這告訴 Spring 從 Maven 項目中展開名稱屬性。
現在,當我們構建項目時,這些屬性將被替換為來自 pom.xml 的值。
這種展開也稱為資源過濾。 值得注意的是,這種過濾僅應用於生產配置。 因此,我們不能在 src/test/resources 下的文件中使用構建屬性。
另一個需要注意的事實是,如果我們使用 addResources 標誌,則 spring-boot:run 目標會將 src/main/resources 直接添加到類路徑中。 儘管這對於熱部署很有用,但它會繞過資源過濾,因此此功能也無效。
現在,上述屬性展開在僅使用 spring-boot-starter-parent 時,會原生生效。
3.1. 不使用 spring-boot-starter-parent 擴展屬性
讓我們看看如何啓用此功能,而無需使用 spring-boot-starter-parent 依賴項。
首先,我們需要在項目的 pom.xml 的 <build/> 元素中啓用資源過濾:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>在這裏,我們僅在 src/main/resources 下啓用了資源過濾。
然後,我們可以為 maven-resources-plugin 添加分隔符配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>請注意,我們已將 useDefaultDelimiters 屬性指定為 false。 這確保了標準 Spring 佔位符,如 ${placeholder},不會在構建過程中被展開。
4. 使用 YAML 文件中的構建信息
如果我們使用 YAML 來存儲應用程序屬性,我們可能無法使用 @ 來指定構建屬性。
但是,我們可以通過以下方式克服這個問題:配置 maven-resources-plugin 中的不同分隔符:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>^</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>或者,只需通過在我們的 pom.xml 的 properties 塊中覆蓋 resource.delimiter 屬性 即可:
<properties>
<resource.delimiter>^</resource.delimiter>
</properties>然後,我們可以在 YAML 文件中使用 ^:
application-description: ^project.description^
application-version: ^project.version^5. 使用 <em>BuildProperties</em> Bean
在這一部分,我們將學習如何使用 <em>BuildProperties</em> Bean 在 Spring Boot 項目中檢索構建信息。
5.1. Maven 設置
在 Spring Boot 項目中,我們將在項目的 <em>pom.xml</em> 文件中添加build-info 目標到 <em>spring-boot-maven-plugin</em> 中:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<java.version>${java.version}</java.version>
<description>${project.description}</description>
<custom.value>123</custom.value>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<build>需要注意的是,該插件允許我們在 additionalProperties 標籤中定義自定義屬性。 此外,自定義屬性的值可以是常量或從其他變量派生而來。
現在,我們準備好構建我們的項目並檢查 target/classes/META-INFO 目錄下的 build-info.properties 文件。
$ cat target/classes/META-INF/build-info.properties
build.artifact=spring-boot-properties
build.custom.value=123
build.description=Spring Boot Properties Module
build.group=com.baeldung.spring-boot-modules
build.java.version=11
build.name=spring-boot-properties
build.time=2023-04-08T04\:29\:28.905Z
build.version=0.0.1-SNAPSHOT
正如預期的那樣,所有構建信息現在都已在應用程序中提供。
5.2. 訪問構建屬性
要訪問構建信息,我們將在應用程序代碼中使用 BuildProperties Bean:
@Autowired
private BuildProperties buildProperties;接下來,我們來訪問默認構建屬性,例如 artifact(製品)、group(組)和 version(版本):
@Test
void givenBuildPropertiesBean_WhenFetchDefaultBuildProperties_ThenGetValidValues() {
Assertions.assertEquals("spring-boot-properties", buildProperties.getArtifact());
Assertions.assertEquals("com.baeldung.spring-boot-modules", buildProperties.getGroup());
Assertions.assertEquals("0.0.1-SNAPSHOT", buildProperties.getVersion());
}最後,我們還將驗證插件中配置的額外構建屬性:
@Test
void givenBuildPropertiesBean_WhenFetchCustomBuildProprties_ThenGetValidValues() {
Assertions.assertEquals("123", buildProperties.get("custom.value"));
Assertions.assertNotNull(buildProperties.get("java.version"));
Assertions.assertEquals("Spring Boot Properties Module", buildProperties.get("description"));
}太棒了!看起來我們已經搞定了。
5.3. Gradle 設置
如果使用 Gradle 構建我們的項目,則可以使用 build.gradle 文件來配置 buildInfo 插件:
springBoot {
buildInfo {
properties {
additional = [
'description': project.getDescription(),
'java.version': JavaVersion.current(),
'custom.value': 123,
]
}
}
}進一步,我們將查找我們的Gradle項目的 build-info.properties 文件,該文件位於 build/resources/main/META-INFO 目錄下。
$ cat build/resources/main/META-INF/build-info.properties
build.artifact=spring-boot-properties
build.custom.value=123
build.description=Spring Boot Properties Module
build.group=com.baeldung.spring-boot-modules
build.java.version=11
build.name=spring-boot-properties
build.time=2023-04-08T05\:14\:12.283459Z
build.version=0.0.1-SNAPSHOT
這就是全部!不需要對應用程序代碼進行任何修改。