Question

I'd like to upload my jar to the Maven Central repository using gradle uploadArchives and use it in other projects as a dependency. I've followed this nice tutorial with the result that my jars are uploaded to Sonatype Nexus (see screenshot below).

The 0.1 version of my jar is available; the line

dependencies {compile 'eu.matthiasbraun:Utils:0.1'}

works just fine in the build.gradle file of my dependent project. I released the 0.1 version by clicking the Close and the Release buttons seen in the screenshot. After that I commented on the Jira ticket I had created here and was told that central sync would run every two hours.

It was my understanding that if I now wanted to release the 0.2 version of my jar, I simply would do gradle uploadArchives and change the line in build.gradle` of my dependent project to

dependencies {compile 'eu.matthiasbraun:Utils:0.2'}.

Yet, when I gradle build my dependent project (after two hours) I got the error that the dependency could not be resolved.

Here's my complete build.gradle which uploads my jar to Sonatype Nexus: link.

How can I automate the release of jars to Maven Central using Gradle?

screenshot

Was it helpful?

Solution

I've used the nexus-workflow gradle plugin to automate the releasing of repositories.

I put this at the top of the build.gradle of my project I want to release:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.adaptc.gradle:nexus-workflow:0.6'
    }
}
apply plugin: 'nexus-workflow'

Additionally I put these three properties in ~/.gradle/gradle.properties

oss-releases.username=mySonatypeUsername
oss-releases.password=mySonatypePassword
oss-releases.url=https://oss.sonatype.org/index.html#stagingRepositories

When I want to push a release to Maven Central, I first upload the jars to Nexus using gradle uploadArchives and then I do gradle nexusStagingRelease. This closes and releases all my open repos on Nexus.


Edit: I also found this plugin by Benjamin Muschko which seems to be an alternative to the plugin described above. I didn't try it yet, though.

OTHER TIPS

Gradle Nexus Staging Plugin can be used to automatize closing the repository and promote/release artifacts to Maven Central.

It has to be added to your project:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.5.1"
    }
}

apply plugin: 'io.codearte.nexus-staging'

and configured:

nexusStaging {
    packageGroup = "org.mycompany.myproject"
    stagingProfileId = "yourStagingProfileId" //optional, but reduce number of calls to Nexus
}

After the artifacts were uploaded (with uploadArchives or any other way) it is enough to call:

./gradlew closeRepository promoteRepository

If a synchronization with Maven Central was enabled the artifacts should automatically appear into Maven Central within several minutes.

More information (including the ways how credentials can be provided) in that blog post or on the project webpage.

Disclaimer. I am the author of this plugin.

The screenshot shows that you have not yet released you library. You have only staged it to OSSRH. What you need to do next is to

  • Close the staging repository by pressing the 'Close' button, this signals to Nexus that nothing else is expected to be added to the staging repository
  • verify the files in the content are ok
  • if you are happy with everything - release the components to Central by pressing the 'Release' button,
  • or if you are unhappy with it press 'Drop' button to delete the staging repo and start from scratch
  • waiting till everything is synced to Central from OSSRH (takes up to an hour or a bit more depending on your timing)

Btw. if you can automate this on the command line with the Nexus Staging Maven Plugin or the Ant Tasks. And you can wrap the Ant tasks and use them in Gradle. More info about all this is in the staging chapter of the Nexus book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top