diff --git a/build-logic/src/main/kotlin/publishing/MemoizedJarInfo.kt b/build-logic/src/main/kotlin/publishing/MemoizedJarInfo.kt index d77a05e75f..f90894efad 100644 --- a/build-logic/src/main/kotlin/publishing/MemoizedJarInfo.kt +++ b/build-logic/src/main/kotlin/publishing/MemoizedJarInfo.kt @@ -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 { @@ -39,21 +40,37 @@ internal class MemoizedJarInfo { @Suppress("UNCHECKED_CAST") rootProject.extra["gitReleaseInfo"] as Map } 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 } diff --git a/build-logic/src/main/kotlin/publishing/PublishingHelperPlugin.kt b/build-logic/src/main/kotlin/publishing/PublishingHelperPlugin.kt index bafc4cc959..359db21e8a 100644 --- a/build-logic/src/main/kotlin/publishing/PublishingHelperPlugin.kt +++ b/build-logic/src/main/kotlin/publishing/PublishingHelperPlugin.kt @@ -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().configureEach { - manifest { MemoizedJarInfo.applyJarManifestAttributes(rootProject, attributes) } - } - - addAdditionalJarContent(this) + tasks.withType().configureEach { + manifest { MemoizedJarInfo.applyJarManifestAttributes(rootProject, attributes) } } apply(plugin = "maven-publish") @@ -175,5 +165,7 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl } } } + + addAdditionalJarContent(this) } } diff --git a/build-logic/src/main/kotlin/publishing/maven-utils.kt b/build-logic/src/main/kotlin/publishing/maven-utils.kt index 4c370f6376..4456b9852f 100644 --- a/build-logic/src/main/kotlin/publishing/maven-utils.kt +++ b/build-logic/src/main/kotlin/publishing/maven-utils.kt @@ -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() { @@ -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) } } } diff --git a/tools/version/src/jarTest/java/org/apache/polaris/version/TestPolarisVersion.java b/tools/version/src/jarTest/java/org/apache/polaris/version/TestPolarisVersion.java index 827d6114dd..c23feef280 100644 --- a/tools/version/src/jarTest/java/org/apache/polaris/version/TestPolarisVersion.java +++ b/tools/version/src/jarTest/java/org/apache/polaris/version/TestPolarisVersion.java @@ -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(); } }