Gradle で git のリビジョン, ビルド日時 を プロパティファイルに入れ込む
Gradle で git のリビジョン, ビルド日時 を プロパティファイルに入れ込む方法です。
間違ってたら、ご指摘くださいmm
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.eclipse.jgit:org.eclipse.jgit:4.5.0.201609210915-r'
}
}
apply plugin: 'java'
processResources {
inputs.dir file("${project.rootDir}/.git")
from('src/main/resources') {
include 'application.yml'
filter(
org.apache.tools.ant.filters.ReplaceTokens,
tokens: [
revision: new org.eclipse.jgit.storage.file.FileRepositoryBuilder()
.setGitDir(new File("${project.rootDir}/.git"))
.build()
.resolve("HEAD")
.getName(),
buildTimestamp: new Date().format("yyyy-MM-dd HH:mm:ss Z")
]
)
}
}
// :
// :
application.yml
revision: '@revision@' buildTimestamp: '@buildTimestamp@'
これで、アプリが git のリビジョンやビルド日時を出力することができるようになります。
まー git コマンド叩いちゃうっていう手もありますが。