與 Actuator 整合
生成構建資訊
Spring Boot Actuator 的 info 端點在存在 META-INF/build-info.properties 檔案時,會自動釋出有關構建的資訊。提供了 BuildInfo 任務來生成此檔案。使用此任務最簡單的方法是透過外掛的 DSL
-
Groovy
-
Kotlin
springBoot {
buildInfo()
}
springBoot {
buildInfo()
}
這將配置一個名為 bootBuildInfo 的 BuildInfo 任務,並且,如果存在的話,使 Java 外掛的 classes 任務依賴於它。任務的目標目錄將是主源集資源(通常是 build/resources/main)輸出目錄中的 META-INF。
預設情況下,生成的構建資訊來自專案
| 財產 | 預設值 |
|---|---|
|
|
|
專案的組 |
|
專案的名稱 |
|
專案的版本 |
|
專案構建的時間 |
屬性可以使用 DSL 進行自定義
-
Groovy
-
Kotlin
springBoot {
buildInfo {
properties {
artifact = 'example-app'
version = '1.2.3'
group = 'com.example'
name = 'Example application'
}
}
}
springBoot {
buildInfo {
properties {
artifact.set("example-app")
version.set("1.2.3")
group.set("com.example")
name.set("Example application")
}
}
}
要從生成的構建資訊中排除任何預設屬性,請將其名稱新增到 excludes 中。例如,可以按如下方式排除 time 屬性
-
Groovy
-
Kotlin
springBoot {
buildInfo {
excludes = ['time']
}
}
springBoot {
buildInfo {
excludes.set(setOf("time"))
}
}
build.time 的預設值是專案構建的即時時間。這樣做的副作用是任務永遠不會是最新的。結果,構建將需要更長的時間,因為更多的任務(包括專案的測試)將不得不執行。另一個副作用是任務的輸出將始終更改,因此構建將不會真正可重複。如果您更重視構建效能或可重複性,而不是 build.time 屬性的準確性,請如前一個示例所示排除 time 屬性。
還可以將附加屬性新增到構建資訊中
-
Groovy
-
Kotlin
springBoot {
buildInfo {
properties {
additional = [
'a': 'alpha',
'b': 'bravo'
]
}
}
}
springBoot {
buildInfo {
properties {
additional.set(mapOf(
"a" to "alpha",
"b" to "bravo"
))
}
}
}
可以使用 Provider 延遲計算附加屬性的值。