Skip to content

Commit

Permalink
Enhance released jars (#7651)
Browse files Browse the repository at this point in the history
This change adds a couple of manifest attributes to jars, if the `release` Gradle project property is present.

The attributes are:
* `Nessie-Build-Git-Describe`: result of `git describe` - example: `nessie-0.72.2-4-g0ce4208995`
* `Nessie-Build-Git-Head`: Git HEAD commit ID
* `Nessie-Build-Java-Version`: Java version used for the build - example: `21.0.1`
* `Nessie-Build-System`: result of `uname -a`
* `Nessie-Build-Timestamp`: result of `date +%Y-%m-%d-%H:%M:%S%:z` - example: `2023-10-20-12:01:33+02:00`
* `Nessie-Version`: version as in `version.txt`, example: `0.72.3-SNAPSHOT`

Those attributes are _not_ added for non-release builds, because manifest attribtues count towards Gradle inputs, potentially rendering cached Gradle outputs unusable - i.e. every change could cause a full rebuild.
  • Loading branch information
snazy authored Oct 20, 2023
1 parent 54e4d3b commit 1f09ad9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
60 changes: 60 additions & 0 deletions build-logic/src/main/kotlin/nessie-common-src.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets
import java.util.Properties
import net.ltgt.gradle.errorprone.CheckSeverity
import net.ltgt.gradle.errorprone.errorprone
Expand Down Expand Up @@ -141,3 +143,61 @@ plugins.withType<JavaPlugin>().configureEach {
}
}
}

// Adds Git/Build/System related information to the generated jars, if the `release` project
// property is present. Do not add that information in development builds, so that the
// generated jars are still cachable for Gradle.
if (project.hasProperty("release")) {
tasks.withType<Jar>().configureEach {
manifest { MemoizedGitInfo.gitInfo(rootProject, attributes) }
}
}

class MemoizedGitInfo {
companion object {
private fun execProc(rootProject: Project, cmd: String, vararg args: Any): String {
val buf = ByteArrayOutputStream()
rootProject.exec {
executable = cmd
args(args.toList())
standardOutput = buf
}
return buf.toString(StandardCharsets.UTF_8).trim()
}

fun gitInfo(rootProject: Project, attribs: Attributes) {
val props = gitInfo(rootProject)
attribs.putAll(props)
}

fun gitInfo(rootProject: Project): Map<String, String> {
if (!rootProject.hasProperty("release")) {
return emptyMap()
}

return if (rootProject.extra.has("gitReleaseInfo")) {
@Suppress("UNCHECKED_CAST")
rootProject.extra["gitReleaseInfo"] as Map<String, String>
} else {
val gitHead = execProc(rootProject, "git", "rev-parse", "HEAD")
val gitDescribe = execProc(rootProject, "git", "describe")
val timestamp = execProc(rootProject, "date", "+%Y-%m-%d-%H:%M:%S%:z")
val system = execProc(rootProject, "uname", "-a")
val javaVersion = System.getProperty("java.version")

val info =
mapOf(
"Nessie-Version" to
rootProject.layout.projectDirectory.file("version.txt").asFile.readText(),
"Nessie-Build-Git-Head" to gitHead,
"Nessie-Build-Git-Describe" to gitDescribe,
"Nessie-Build-Timestamp" to timestamp,
"Nessie-Build-System" to system,
"Nessie-Build-Java-Version" to javaVersion
)
rootProject.extra["gitReleaseInfo"] = info
return info
}
}
}
}
17 changes: 16 additions & 1 deletion servers/quarkus-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,22 @@ tasks.withType<ProcessResources>().configureEach {

val packageType = quarkusPackageType()

quarkus { quarkusBuildProperties.put("quarkus.package.type", packageType) }
quarkus {
quarkusBuildProperties.put("quarkus.package.type", packageType)
// Pull manifest attributes from the "main" `jar` task to get the
// release-information into the jars generated by Quarkus.
quarkusBuildProperties.putAll(
provider {
tasks
.named("jar", Jar::class.java)
.get()
.manifest
.attributes
.map { e -> "quarkus.package.manifest.attributes.\"${e.key}\"" to e.value.toString() }
.toMap()
}
)
}

if (quarkusFatJar()) {
afterEvaluate {
Expand Down
13 changes: 13 additions & 0 deletions servers/quarkus-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ quarkus {
)
quarkusBuildProperties.put("quarkus.smallrye-openapi.info-version", project.version.toString())
quarkusBuildProperties.put("quarkus.smallrye-openapi.auto-add-security", "false")
// Pull manifest attributes from the "main" `jar` task to get the
// release-information into the jars generated by Quarkus.
quarkusBuildProperties.putAll(
provider {
tasks
.named("jar", Jar::class.java)
.get()
.manifest
.attributes
.map { e -> "quarkus.package.manifest.attributes.\"${e.key}\"" to e.value.toString() }
.toMap()
}
)
}

val quarkusAppPartsBuild = tasks.named("quarkusAppPartsBuild")
Expand Down

0 comments on commit 1f09ad9

Please sign in to comment.