I would like to sign all jar files before its publication to maven artifactory.
I found that it is very easy to write custom signer, but signing plugin is sadly not designed to modify existing artifacts before publications. I found a possible way how to do it by signing plugin (mentioned in the link above) but it is too hacky. Is there any official way how to transform artifacts just before publication? Or immediately after they are created. But in some general way which will be working with jar, war or with an android AAR.
I tried to use
publishing.publications["maven"].allPublishableArtifacts
It allows me to get access to all artifacts at the moment where they are already created (File.exists()
return true). I know that publish tasks did not start at that moment, but all attempts to modify fail, in the way that nothing happens. Even if I delete file provided by allPublishableArtifacts
, delete()
method return true
, but artifact still exists in build/libs
folder and is published to artifactory.
I found solution. It is important to wait to producer task
publishing.publications["maven"].cast<org.gradle.api.publish.internal.PublicationInternal<MavenArtifact>>()
.allPublishableArtifacts {
buildDependencies.getDependencies(null).firstOrNull()?.doLast {
file.writeText("signed")
}
}
It will override all artifacts by just “signed” text.
I Just don’t understand one thing. It is possible that the collection returned by buildDependencies.getDependencies(null)
can have multiple items. In all scenarios, I tested there is always just one task. What I should do if there will be more of them? I should somehow wait to all of them to be finished? But how?
I finally found a way myself:
the<PublishingExtension>().publications.whenObjectAdded {
the<PublishingExtension>().publications[name].cast<PublicationInternal<MavenArtifact>>()
.allPublishableArtifacts {
if (... some check what we want to sign ...) {
buildDependencies.getDependencies(null).firstOrNull()?.doLast {
api.sign(file, file)
}
}
}
}
I’m just not sure with getDependencies(null).firstOrNull()
But in works in all cases I tested so far.