Publishing artifacts with Gradle

This might not be the most interesting post compared to my normal posts. There are no pretty pictures to look at Sad smile.

By default gradle publishes using the ivy protocol. Thus if you have the following setup…

uploadArchives {
	repositories { mavenLocal() }
}

You will end up publishing an ivy.xml artifact, let us call it artifactX, to your local maven.

The problem with doing this

Inherently this can be done and artifactX will be found in successive builds that reference it. However, artifactX’s dependant libraries will not be identified as they are contained in an ivy.xml file and not an artifactX.pom file. So when you try to build another project, let us call it projectY, which is dependant on artifactX… the build will fail. The reason for this is because to build projectY you need to execute through artifactX and artifactX is dependant on other libraries which projectY is not directly dependant on. These libraries are also called transient libraries.

Using the ivy protocol

The fast workaround I came up with was to just use the ivy protocol, which gradle uses by default.

uploadArchives {
	repositories { ivy { url "file://c:/temp/ivy" } }
}

This worked phenomenally and the transient libraries were pulled in and projectY could be built. Here comes the but… the requirement is to publish artifactX into a maven repository. Thus, I had to get the publishing of artifactX to use the maven protocol and generate a pom instead of the ivy.xml file.

Using the maven protocol

It seems that there are two ways to publish to maven currently.

  1. A lot of the documentation refers to the maven-publish plugin for gradle. However, as it currently stands with gradle-v1.11: it is in an incubating stage and our environment will not allow for something that is not stable yet.
  2. The older way of publishing to maven is to use the maven plugin for gradle. This is the way that will be shown below. Take head though, it must have some restrictions otherwise there would not be any reason for the maven-publish plugin.

NB: do not attempt to use mavenLocal() in uploadArchives as shown above… it will use the ivy protocol.

apply plugin: 'maven'
uploadArchives {
	repositories { mavenDeployer{repository(url: "file://c:/temp/maven") }
}

The above code will publish artifactX into the directory similar to the ivy protocol. However, there are a few differences.

  1. It does not publish the ivy.xml into the maven directory, instead generates a pom, md5, sha1 for each artifact declared
  2. It additionally publishes to the local maven repository that sits in the user home (%user_home%/.m2)

It took a bit of searching, but it seems that it is rather easy to publish between the two protocols.

  • ??????? ???????

    Of course, it’s obvious, that if you wont to upload to maven, you must do it like this:

    mavenDeployer {
    repository (url: mavenLocal().url)
    }