How to Publish a Gradle Project on Maven Central
By Mikael Ståldal
You can publish a Java or Kotlin library built with Gradle on Maven Central like this.
First setup your Gradle build to produce a valid bundle for Maven Central.
-
Include the Maven Publish Plugin in your build.
-
Setup building of Javadoc and Source archives using Dokka.
-
Set the required POM metadata in Gradle like this.
See here for a complete working example.
Now you can run gradle publish to create the Maven bundle in the mavenPublish directory (which you probably want to include in .gitignore).
Maven Central also requires that you sign the artifacts with OpenPGP. You can do that with Gradle’s signing plugin, but it’s better to do signing and the actual publishing outside of the build tool, to avoid the kind of supply-chain attacks which currently plagues the NPM eco-system. If you run Gradle in a sandbox and don’t give it access to keys/credentials used for signing and publishing, then a compromized build will not be able to spread like a worm. You can sign the artifacts with this command:
find mavenPublish/ -type f \( -name '*.jar' -o -name '*.pom' -o -name '*.module' \) -execdir gpg --armor --detach-sign '{}' \;
Then create the bundle to be published:
(cd mavenPublish/ && zip -r ../central-bundle.zip .)
Finally, you can upload the bundle to Maven Central.