Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated release to Maven Central with Gradle

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

like image 204
Matthias Braun Avatar asked Dec 06 '13 20:12

Matthias Braun


2 Answers

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.

like image 94
Matthias Braun Avatar answered Nov 04 '22 22:11

Matthias Braun


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.

like image 24
Marcin Zajączkowski Avatar answered Nov 04 '22 23:11

Marcin Zajączkowski