From 7f42e6d5970be35407b9a8dbc155985bf1eb67f8 Mon Sep 17 00:00:00 2001 From: Appu Goundan Date: Wed, 10 Jan 2024 15:20:35 -0500 Subject: [PATCH] Custom signing task to ignore sigstore sigs Signed-off-by: Appu Goundan --- ...ld-logic.java-published-library.gradle.kts | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/build-logic/publishing/src/main/kotlin/build-logic.java-published-library.gradle.kts b/build-logic/publishing/src/main/kotlin/build-logic.java-published-library.gradle.kts index 76e50195..0bd7050d 100644 --- a/build-logic/publishing/src/main/kotlin/build-logic.java-published-library.gradle.kts +++ b/build-logic/publishing/src/main/kotlin/build-logic.java-published-library.gradle.kts @@ -1,3 +1,8 @@ +import org.codehaus.groovy.runtime.StringGroovyMethods +import org.gradle.api.publish.internal.PublicationInternal +import org.gradle.api.publish.internal.PublicationInternal.DerivedArtifact +import org.gradle.api.publish.maven.internal.publication.MavenPublicationInternal + plugins { id("build-logic.repositories") id("build-logic.java-library") @@ -19,4 +24,37 @@ publishing { } } -signing.sign(publishing.publications["mavenJava"]) +createSignTask(publishing.publications["mavenJava"]) + +fun createSignTask(publicationToSign: Publication) { + val signTaskName = "sign" + StringGroovyMethods.capitalize(publicationToSign.name) + "Publication" + if (project.tasks.names.contains(signTaskName)) { + throw GradleException("can't create custom sign task (it already exists): $signTaskName") + } else { + project.tasks.create(signTaskName) { // must be create (not register) + val task = this + task.description = "Signs all artifacts in the 'mavenJava' publication." + if (publicationToSign !is MavenPublicationInternal) { + throw GradleException("can't configure signing for non-MavenPublication") + } + publicationToSign.allPublishableArtifacts { + if (task.signatureFiles.contains(this.file) || this.file.name.endsWith(".sigstore")) { // or .sigstore.json eventually + return@allPublishableArtifacts + } + task.dependsOn(this) + val signature = Signature(this::getFile, null, task as SignatureSpec, this) + task.signatures.add(signature) + val derivedArtifact = object : DerivedArtifact { + override fun shouldBePublished(): Boolean { + return task.isEnabled && task.onlyIf.isSatisfiedBy(task) + } + + override fun create(): File { + return signature.file + } + } + publicationToSign.addDerivedArtifact(this, derivedArtifact) + } + } + } +}