Skip to content

Commit

Permalink
Gradle: Save current Git hash in JAR manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
skalarproduktraum committed Apr 15, 2024
1 parent 40f4b90 commit 0594ac3
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ tasks {
}
jar {
archiveVersion.set(rootProject.version.toString())

manifest.attributes["Implementation-Build"] = run { // retrieve the git commit hash
val gitFolder = "$projectDir/.git/"
val digit = 7
/* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash
* otherwise: a reference to a file containing the current commit hash */
val head = file(gitFolder + "HEAD").readText().split(":") // .git/HEAD
val isCommit = head.size == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd
// def isRef = head.length > 1 // ref: refs/heads/main
when {
isCommit -> head[0] // e5a7c79edabb
else -> file(gitFolder + head[1].trim()) // .git/refs/heads/main
.readText()
}.trim().take(digit)
}
manifest.attributes["Implementation-Version"] = project.version
}

Expand Down

0 comments on commit 0594ac3

Please sign in to comment.