Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions build-logic/src/main/kotlin/publishing/MemoizedJarInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import org.gradle.api.java.archives.Attributes
import org.gradle.kotlin.dsl.extra

/**
* Helper class to generate Jar manifest attributes including Git commit SHA, Git describe, project
* version and Java specification version.
* Helper class to generate Jar manifest attributes including project version and Java specification
* version. Git information like the commit SHA and Git describe output are only included for
* release builds, or if explicitly requested.
*/
internal class MemoizedJarInfo {
companion object {
Expand All @@ -39,21 +40,37 @@ internal class MemoizedJarInfo {
@Suppress("UNCHECKED_CAST")
rootProject.extra["gitReleaseInfo"] as Map<String, String>
} else {
val isRelease =
rootProject.hasProperty("release") || rootProject.hasProperty("jarWithGitInfo")
val gi = GitInfo.memoized(rootProject)
val version = rootProject.version.toString()
val javaSpecificationVersion = System.getProperty("java.specification.version")
val includeGitInformation =
rootProject.hasProperty("release") || rootProject.hasProperty("jarWithGitInfo")

val version = rootProject.version.toString()
val info =
mapOf(
"Implementation-Version" to version,
"Apache-Polaris-Version" to version,
"Apache-Polaris-Is-Release" to isRelease.toString(),
"Apache-Polaris-Build-Git-Head" to gi.gitHead,
"Apache-Polaris-Build-Git-Describe" to gi.gitDescribe,
"Apache-Polaris-Build-Java-Specification-Version" to javaSpecificationVersion,
)
if (includeGitInformation) {
val gi = GitInfo.memoized(rootProject)
mapOf(
"Implementation-Version" to version,
"Apache-Polaris-Version" to version,
"Apache-Polaris-Is-Release" to "true",
"Apache-Polaris-Build-Git-Head" to gi.gitHead,
"Apache-Polaris-Build-Git-Describe" to gi.gitDescribe,
"Apache-Polaris-Build-Java-Specification-Version" to javaSpecificationVersion,
)
} else {
// Not adding Git information here to keep Gradle's up-to-date functionality intact.
// Varying information in the manifest would change the MANIFEST.MF file and the jar.
// If the output changes, the input of dependent tasks is no longer up-to-date and would
// need to be rebuilt.
// This would render the Gradle build-cache ineffective for every Git commit,
// especially in CI, leading to unnecessary long builds.
mapOf(
"Implementation-Version" to version,
"Apache-Polaris-Version" to version,
"Apache-Polaris-Is-Release" to "false",
"Apache-Polaris-Build-Java-Specification-Version" to javaSpecificationVersion,
)
}

rootProject.extra["gitReleaseInfo"] = info
return info
}
Expand Down
16 changes: 4 additions & 12 deletions build-logic/src/main/kotlin/publishing/PublishingHelperPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,8 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl
project.run {
extensions.create("publishingHelper", PublishingHelperExtension::class.java)

val isRelease = project.hasProperty("release")

// 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 cacheable for Gradle.
if (isRelease || project.hasProperty("jarWithGitInfo")) {
// Runs `git`, considered expensive, so guarded behind project properties.
tasks.withType<Jar>().configureEach {
manifest { MemoizedJarInfo.applyJarManifestAttributes(rootProject, attributes) }
}

addAdditionalJarContent(this)
tasks.withType<Jar>().configureEach {
manifest { MemoizedJarInfo.applyJarManifestAttributes(rootProject, attributes) }
}

apply(plugin = "maven-publish")
Expand Down Expand Up @@ -175,5 +165,7 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl
}
}
}

addAdditionalJarContent(this)
}
}
53 changes: 22 additions & 31 deletions build-logic/src/main/kotlin/publishing/maven-utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.withType

@CacheableTask
abstract class GeneratePomProperties : DefaultTask() {
Expand Down Expand Up @@ -76,37 +75,29 @@ fun addAdditionalJarContent(project: Project): Unit =
val generatePomProperties =
tasks.register("generatePomProperties", GeneratePomProperties::class.java) {}

val additionalJarContent =
tasks.register("additionalJarContent", Sync::class.java) {
// Have to manually declare the inputs of this task here on top of the from/include below
inputs.files(
rootProject.layout.files("gradle/jar-licenses/LICENSE", "gradle/jar-licenses/NOTICE")
)
inputs.property("GAV", "${project.group}:${project.name}:${project.version}")
dependsOn("generatePomFileForMavenPublication")
if (!project.file("src/main/resources/META-INF/LICENSE").exists()) {
from(rootProject.rootDir).include("gradle/jar-licenses/LICENSE").eachFile {
this.path = "META-INF/$sourceName"
}
tasks.withType(Jar::class).configureEach {
if (!project.file("src/main/resources/META-INF/LICENSE").exists()) {
from(rootProject.rootDir) {
include("gradle/jar-licenses/LICENSE").eachFile { path = "META-INF/$sourceName" }
}
if (!project.file("src/main/resources/META-INF/NOTICE").exists()) {
from(rootProject.rootDir).include("gradle/jar-licenses/NOTICE").eachFile {
this.path = "META-INF/$sourceName"
}
}
from(tasks.named("generatePomFileForMavenPublication")) {
include("pom-default.xml")
eachFile { this.path = "META-INF/maven/${project.group}/${project.name}/pom.xml" }
} else if (name == "javadocJar") {
from("src/main/resources") { include("META-INF/LICENSE") }
}
if (!project.file("src/main/resources/META-INF/NOTICE").exists()) {
from(rootProject.rootDir) {
include("gradle/jar-licenses/NOTICE").eachFile { path = "META-INF/$sourceName" }
}
into(layout.buildDirectory.dir("license-for-jar"))
} else if (name == "javadocJar") {
from("src/main/resources") { include("META-INF/NOTICE") }
}
from(tasks.named("generatePomFileForMavenPublication")) {
include("pom-default.xml")
eachFile { path = "META-INF/maven/${project.group}/${project.name}/pom.xml" }
}
from(generatePomProperties) {
include("**")
eachFile { path = sourcePath }
}

tasks.named("processResources") { dependsOn("additionalJarContent") }

val sourceSets: SourceSetContainer by project
sourceSets.named("main") {
resources.srcDir(additionalJarContent)
resources.srcDir(generatePomProperties)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ public class TestPolarisVersion {
@Order(1)
public void versionAvailable() {
soft.assertThat(polarisVersionString()).isEqualTo(System.getProperty("polarisVersion"));
soft.assertThat(getBuildReleasedVersion()).isNotEmpty();
soft.assertThat(getBuildJavaSpecificationVersion()).isNotEmpty();
if (isReleaseBuild()) {
soft.assertThat(getBuildReleasedVersion()).isNotEmpty();
soft.assertThat(getBuildGitHead()).isNotEmpty();
soft.assertThat(getBuildGitTag()).isNotEmpty();
soft.assertThat(getBuildJavaSpecificationVersion()).isNotEmpty();
} else {
soft.assertThat(getBuildReleasedVersion()).isEmpty();
soft.assertThat(getBuildGitHead()).isEmpty();
soft.assertThat(getBuildGitTag()).isEmpty();
soft.assertThat(getBuildJavaSpecificationVersion()).isEmpty();
}
}

Expand Down