Publish an artifact to a Maven repository using Gradle
I have been using Gradle as a build tool for my Java/Groovy/Scala projects in the last 12 months and I'm very happy with it.
I'm not going into a full description of Gradle, there are plenty of resources available on the Internet praising this awesome build tool. If you don't know it already, I really recommend considering it for your next project.
One of the very attractive features of Gradle, is the seamless integration with Maven repositories. You can easily declare your dependencies as belonging to a Maven repository or to a file based repository.
Similarly, you can upload artifact to a Maven repository with very little code. The following is an example of a Java project build file producing a jar file that gets uploaded to an Apache Archivia Maven repository. For some reasons, I couldn't find a complete example in the (otherwise exceptional) documentation, so I thought to post it here as a reference.
apply plugin: 'java'
apply plugin: 'maven'
// Project configuration is used for Maven deploy:
version = '1.0-SNAPSHOT'
group = 'io.fiandes.sandbox'
configurations {
deployerJars
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:r07'
deployerJars "org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-6"
}
uploadArchives {
repositories {
deployer = mavenDeployer {
uniqueVersion = false
configureAuth = {
authentication(userName: 'user', password: 'password')
}
configuration = configurations.deployerJars
snapshotRepository(url: "dav:http://192.168.1:1:8080/archiva/repository/snapshots/", configureAuth)
repository(url: "dav:http://192.168.1.1:8080/archiva/repository/internal/", configureAuth)
}
}
}
The dependency on
"org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-6"
is needed because Gradle doesn't quite know how to handle the WebDav protocol.
The Google Guava dependency is there as an example, it is not needed for the Maven publishing purpose.
Finally, the "uniqueVersion = false" flag is needed to avoid Gradle to create weirdly named artifacts. The most interesting thing here, is that Gradle will generate the Maven POM for you and upload it to the Maven repository.
Another very cool feature is the automatic uploading to the correct repository depending on the version name. If the version name ends with "SNAPSHOT" (capital letters), Gradle will automatically upload the artifact to the repository "snapshotRepository". If the version is not marked as SNAPSHOT, it will be uploaded to the regular releases repository.