Publishing + Consuming Artefacts in the Local Maven Repository with Gradle

When you're working on shared libraries, it's quite likely that you'll want to test the new version of the library, before publishing it. This can generally be avoided if we have a snapshots repo, but still helps.

When using Gradle, we need to make sure that we have the maven-publish plugin added, which will then provide us the publishToMavenLocal task, which we can run as following:

./gradlew publishToMavenLocal

This then publishes to our local maven repository:

find ~/.m2/repository/me/jvt/hacking/library
~/.m2/repository/me/jvt/hacking/library
~/.m2/repository/me/jvt/hacking/library/0.1.0
~/.m2/repository/me/jvt/hacking/library/0.1.0/library-0.1.0.jar
~/.m2/repository/me/jvt/hacking/library/0.1.0/library-0.1.0.pom
~/.m2/repository/me/jvt/hacking/library/0.1.0/library-0.1.0.module
~/.m2/repository/me/jvt/hacking/library/maven-metadata-local.xml

Once the library is published, how can we consume it? We add the dependency as with other dependencies but add mavenLocal():

 repositories {
   mavenCentral()
+  mavenLocal()
 }

I never check in these changes, because I've been burned by having this enabled on a project before. Maven pulls from the local Maven repository by default, so when I was running on a shared build platform, it led to inconsistent failing builds between my local machine and remotely as the version that's available may not be the version that's been tested and released. It similarly runs the risk that someone may have overwritten a version of the library that you're expecting to be correct, i.e. Apache commons.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #java #gradle.

This post was filed under articles.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.