Skip to content

Commit a6808c2

Browse files
authored
Build: Simplify signing + fix execution in polaris-distribution (#2906)
This change simplifies generation of non-publication artifacts by adding a function taking the task which outputs shall be signed. That function takes care of setting up the correct task dependencies and task execution. Also fixes an issue that signing does not always happen when running `./gradlew :polaris-distribution:assemble`, because the task dependency graph for the archive tasks and the corresponding signing tasks isn't properly set up.
1 parent 74b1254 commit a6808c2

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl
7979
extensions.create("publishingHelper", PublishingHelperExtension::class.java)
8080

8181
val isRelease = project.hasProperty("release")
82-
val isSigning = isRelease || project.hasProperty("signArtifacts")
8382

8483
// Adds Git/Build/System related information to the generated jars, if the `release` project
8584
// property is present. Do not add that information in development builds, so that the
@@ -102,7 +101,7 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl
102101
configureOnRootProject(project)
103102
}
104103

105-
if (isSigning) {
104+
if (isSigningEnabled()) {
106105
plugins.withType<SigningPlugin>().configureEach {
107106
configure<SigningExtension> {
108107
val signingKey: String? by project

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import org.gradle.api.tasks.Exec
2929
import org.gradle.kotlin.dsl.apply
3030
import org.gradle.kotlin.dsl.named
3131
import org.gradle.kotlin.dsl.register
32-
import org.gradle.plugins.signing.Sign
3332

3433
/**
3534
* Configures Apache project specific publishing tasks on the root project, for example the
@@ -40,7 +39,6 @@ internal fun configureOnRootProject(project: Project) =
4039
apply<NexusPublishPlugin>()
4140

4241
val isRelease = project.hasProperty("release")
43-
val isSigning = isRelease || project.hasProperty("signArtifacts")
4442

4543
val sourceTarball = tasks.register<Exec>("sourceTarball")
4644
sourceTarball.configure {
@@ -62,6 +60,8 @@ internal fun configureOnRootProject(project: Project) =
6260
"HEAD",
6361
)
6462
workingDir(project.projectDir)
63+
64+
outputs.file(e.sourceTarball)
6565
}
6666

6767
val digestSourceTarball =
@@ -76,18 +76,7 @@ internal fun configureOnRootProject(project: Project) =
7676

7777
sourceTarball.configure { finalizedBy(digestSourceTarball) }
7878

79-
if (isSigning) {
80-
val signSourceTarball =
81-
tasks.register<Sign>("signSourceTarball") {
82-
description = "Sign the source tarball"
83-
mustRunAfter(sourceTarball)
84-
doFirst {
85-
val e = project.extensions.getByType(PublishingHelperExtension::class.java)
86-
sign(e.sourceTarball.get().asFile)
87-
}
88-
}
89-
sourceTarball.configure { finalizedBy(signSourceTarball) }
90-
}
79+
signTaskOutputs(sourceTarball)
9180

9281
val releaseEmailTemplate = tasks.register("releaseEmailTemplate")
9382
releaseEmailTemplate.configure {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package publishing
21+
22+
import org.gradle.api.Project
23+
import org.gradle.api.tasks.TaskProvider
24+
import org.gradle.internal.extensions.stdlib.capitalized
25+
import org.gradle.plugins.signing.SigningExtension
26+
27+
fun Project.isSigningEnabled(): Boolean = hasProperty("release") || hasProperty("signArtifacts")
28+
29+
/**
30+
* Convenience function to sign all output files of the given task.
31+
*
32+
* Only triggers signing, if the `release` or `signArtifacts` project property is set.
33+
*/
34+
fun Project.signTaskOutputs(task: TaskProvider<*>): Unit {
35+
if (isSigningEnabled()) {
36+
val signingTask = tasks.register("sign" + task.name.capitalized())
37+
signingTask.configure {
38+
dependsOn(task)
39+
actions.addLast {
40+
val files = task.get().outputs.files.files
41+
files.forEach { file -> logger.info("Signing $file for '${task.get().path}'") }
42+
val ext = project.extensions.getByType(SigningExtension::class.java)
43+
ext.sign(*files.toTypedArray())
44+
}
45+
}
46+
task.configure { finalizedBy(signingTask) }
47+
}
48+
}

runtime/distribution/build.gradle.kts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import publishing.GenerateDigest
2121
import publishing.PublishingHelperPlugin
22+
import publishing.signTaskOutputs
2223

2324
plugins {
2425
id("distribution")
@@ -98,9 +99,6 @@ distTar.configure { finalizedBy(digestDistTar) }
9899

99100
distZip.configure { finalizedBy(digestDistZip) }
100101

101-
if (project.hasProperty("release") || project.hasProperty("signArtifacts")) {
102-
signing {
103-
sign(distTar.get())
104-
sign(distZip.get())
105-
}
106-
}
102+
signTaskOutputs(distTar)
103+
104+
signTaskOutputs(distZip)

0 commit comments

Comments
 (0)