Skip to content

Commit 7642d41

Browse files
authored
Build: ensure LICENSE/NOTICE is in all jars, always add pom-files to all jars (#3057)
There are a some inconsistencies between the different kinds of jars and the included information: * LICENSE/NOTICE files are present in the "main" jar and in the sources jar, but not in the javadoc jar. * The Maven pom.xml and pom.properties files are only present for release builds or when explicitly requested. * "Additional" jar-manifest attributes that are only present in release builds. This change fixes the three mentioned issues: * Always include pom.xml and pom.properties in the built jar files. * Always include the additional jar-manifest attributes, except the Git information, which would otherwise render the Gradle build cache ineffective. * Include pom.xml + pom.properties + license/notice in literally all jar files. The Gradle logic to include the license+notice+pom files has been simplified as well.
1 parent f07abb7 commit 7642d41

File tree

4 files changed

+59
-61
lines changed

4 files changed

+59
-61
lines changed

build-logic/src/main/kotlin/publishing/MemoizedJarInfo.kt

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import org.gradle.api.java.archives.Attributes
2424
import org.gradle.kotlin.dsl.extra
2525

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

47-
val version = rootProject.version.toString()
4848
val info =
49-
mapOf(
50-
"Implementation-Version" to version,
51-
"Apache-Polaris-Version" to version,
52-
"Apache-Polaris-Is-Release" to isRelease.toString(),
53-
"Apache-Polaris-Build-Git-Head" to gi.gitHead,
54-
"Apache-Polaris-Build-Git-Describe" to gi.gitDescribe,
55-
"Apache-Polaris-Build-Java-Specification-Version" to javaSpecificationVersion,
56-
)
49+
if (includeGitInformation) {
50+
val gi = GitInfo.memoized(rootProject)
51+
mapOf(
52+
"Implementation-Version" to version,
53+
"Apache-Polaris-Version" to version,
54+
"Apache-Polaris-Is-Release" to "true",
55+
"Apache-Polaris-Build-Git-Head" to gi.gitHead,
56+
"Apache-Polaris-Build-Git-Describe" to gi.gitDescribe,
57+
"Apache-Polaris-Build-Java-Specification-Version" to javaSpecificationVersion,
58+
)
59+
} else {
60+
// Not adding Git information here to keep Gradle's up-to-date functionality intact.
61+
// Varying information in the manifest would change the MANIFEST.MF file and the jar.
62+
// If the output changes, the input of dependent tasks is no longer up-to-date and would
63+
// need to be rebuilt.
64+
// This would render the Gradle build-cache ineffective for every Git commit,
65+
// especially in CI, leading to unnecessary long builds.
66+
mapOf(
67+
"Implementation-Version" to version,
68+
"Apache-Polaris-Version" to version,
69+
"Apache-Polaris-Is-Release" to "false",
70+
"Apache-Polaris-Build-Java-Specification-Version" to javaSpecificationVersion,
71+
)
72+
}
73+
5774
rootProject.extra["gitReleaseInfo"] = info
5875
return info
5976
}

build-logic/src/main/kotlin/publishing/PublishingHelperPlugin.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,8 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl
7878
project.run {
7979
extensions.create("publishingHelper", PublishingHelperExtension::class.java)
8080

81-
val isRelease = project.hasProperty("release")
82-
83-
// Adds Git/Build/System related information to the generated jars, if the `release` project
84-
// property is present. Do not add that information in development builds, so that the
85-
// generated jars are still cacheable for Gradle.
86-
if (isRelease || project.hasProperty("jarWithGitInfo")) {
87-
// Runs `git`, considered expensive, so guarded behind project properties.
88-
tasks.withType<Jar>().configureEach {
89-
manifest { MemoizedJarInfo.applyJarManifestAttributes(rootProject, attributes) }
90-
}
91-
92-
addAdditionalJarContent(this)
81+
tasks.withType<Jar>().configureEach {
82+
manifest { MemoizedJarInfo.applyJarManifestAttributes(rootProject, attributes) }
9383
}
9484

9585
apply(plugin = "maven-publish")
@@ -175,5 +165,7 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl
175165
}
176166
}
177167
}
168+
169+
addAdditionalJarContent(this)
178170
}
179171
}

build-logic/src/main/kotlin/publishing/maven-utils.kt

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import org.gradle.api.provider.ListProperty
2727
import org.gradle.api.tasks.CacheableTask
2828
import org.gradle.api.tasks.Input
2929
import org.gradle.api.tasks.OutputDirectory
30-
import org.gradle.api.tasks.SourceSetContainer
31-
import org.gradle.api.tasks.Sync
3230
import org.gradle.api.tasks.TaskAction
33-
import org.gradle.kotlin.dsl.provideDelegate
31+
import org.gradle.jvm.tasks.Jar
32+
import org.gradle.kotlin.dsl.withType
3433

3534
@CacheableTask
3635
abstract class GeneratePomProperties : DefaultTask() {
@@ -76,37 +75,29 @@ fun addAdditionalJarContent(project: Project): Unit =
7675
val generatePomProperties =
7776
tasks.register("generatePomProperties", GeneratePomProperties::class.java) {}
7877

79-
val additionalJarContent =
80-
tasks.register("additionalJarContent", Sync::class.java) {
81-
// Have to manually declare the inputs of this task here on top of the from/include below
82-
inputs.files(
83-
rootProject.layout.files("gradle/jar-licenses/LICENSE", "gradle/jar-licenses/NOTICE")
84-
)
85-
inputs.property("GAV", "${project.group}:${project.name}:${project.version}")
86-
dependsOn("generatePomFileForMavenPublication")
87-
if (!project.file("src/main/resources/META-INF/LICENSE").exists()) {
88-
from(rootProject.rootDir).include("gradle/jar-licenses/LICENSE").eachFile {
89-
this.path = "META-INF/$sourceName"
90-
}
78+
tasks.withType(Jar::class).configureEach {
79+
if (!project.file("src/main/resources/META-INF/LICENSE").exists()) {
80+
from(rootProject.rootDir) {
81+
include("gradle/jar-licenses/LICENSE").eachFile { path = "META-INF/$sourceName" }
9182
}
92-
if (!project.file("src/main/resources/META-INF/NOTICE").exists()) {
93-
from(rootProject.rootDir).include("gradle/jar-licenses/NOTICE").eachFile {
94-
this.path = "META-INF/$sourceName"
95-
}
96-
}
97-
from(tasks.named("generatePomFileForMavenPublication")) {
98-
include("pom-default.xml")
99-
eachFile { this.path = "META-INF/maven/${project.group}/${project.name}/pom.xml" }
83+
} else if (name == "javadocJar") {
84+
from("src/main/resources") { include("META-INF/LICENSE") }
85+
}
86+
if (!project.file("src/main/resources/META-INF/NOTICE").exists()) {
87+
from(rootProject.rootDir) {
88+
include("gradle/jar-licenses/NOTICE").eachFile { path = "META-INF/$sourceName" }
10089
}
101-
into(layout.buildDirectory.dir("license-for-jar"))
90+
} else if (name == "javadocJar") {
91+
from("src/main/resources") { include("META-INF/NOTICE") }
92+
}
93+
from(tasks.named("generatePomFileForMavenPublication")) {
94+
include("pom-default.xml")
95+
eachFile { path = "META-INF/maven/${project.group}/${project.name}/pom.xml" }
96+
}
97+
from(generatePomProperties) {
98+
include("**")
99+
eachFile { path = sourcePath }
102100
}
103-
104-
tasks.named("processResources") { dependsOn("additionalJarContent") }
105-
106-
val sourceSets: SourceSetContainer by project
107-
sourceSets.named("main") {
108-
resources.srcDir(additionalJarContent)
109-
resources.srcDir(generatePomProperties)
110101
}
111102
}
112103
}

tools/version/src/jarTest/java/org/apache/polaris/version/TestPolarisVersion.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ public class TestPolarisVersion {
5555
@Order(1)
5656
public void versionAvailable() {
5757
soft.assertThat(polarisVersionString()).isEqualTo(System.getProperty("polarisVersion"));
58+
soft.assertThat(getBuildReleasedVersion()).isNotEmpty();
59+
soft.assertThat(getBuildJavaSpecificationVersion()).isNotEmpty();
5860
if (isReleaseBuild()) {
59-
soft.assertThat(getBuildReleasedVersion()).isNotEmpty();
6061
soft.assertThat(getBuildGitHead()).isNotEmpty();
6162
soft.assertThat(getBuildGitTag()).isNotEmpty();
62-
soft.assertThat(getBuildJavaSpecificationVersion()).isNotEmpty();
6363
} else {
64-
soft.assertThat(getBuildReleasedVersion()).isEmpty();
6564
soft.assertThat(getBuildGitHead()).isEmpty();
6665
soft.assertThat(getBuildGitTag()).isEmpty();
67-
soft.assertThat(getBuildJavaSpecificationVersion()).isEmpty();
6866
}
6967
}
7068

0 commit comments

Comments
 (0)