與 Actuator 整合

生成構建資訊

Spring Boot Actuator 的 info 端點會在存在 META-INF/build-info.properties 檔案時自動釋出關於構建的資訊。提供了一個 BuildInfo 任務來生成此檔案。使用此任務的最簡單方法是透過外掛的 DSL

  • Groovy

  • Kotlin

springBoot {
	buildInfo()
}
springBoot {
	buildInfo()
}

這將配置一個名為 bootBuildInfoBuildInfo 任務,如果 Java 外掛的 classes 任務存在,則使其依賴於此任務。此任務的目標目錄將是主原始碼集的資源輸出目錄(通常是 build/resources/main)中的 META-INF

預設情況下,生成的構建資訊派生自專案

屬性 預設值

build.artifact

bootJarbootWar 任務的基礎名稱

build.group

專案的 group

build.name

專案的名稱

build.version

專案的版本

build.time

專案構建時的時間

可以使用 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 的預設值是專案構建時的瞬間。其副作用是該任務永遠不會是 up-to-date 的。因此,構建將花費更長時間,因為包括專案測試在內的更多工必須執行。另一個副作用是該任務的輸出總是會改變,因此構建將不會真正可重複。如果您更看重構建效能或可重複性而非 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 來延遲計算附加屬性的值。