Dark theme | Light theme

November 19, 2009

Gradle Goodness: Deploying Our Gradle Plugin to a Maven Repository

We have created a Gradle plugin, but now we want to share it with others. Suppose we have a central Maven repository within our company than we can use this repository to distribute our plugin among other developers. We don't need to change the code of our plugin, but we do need to move the code into a new project. We created the plugin in the buildSrc directory and that means it is only available for that project. We create a new directory and move the src directory from the buildSrc directory to the new project directory. Next we create a build.gradle file.

$ mkdir makedirs-plugin
$ cd makedirs-plugin
$ cp -R <old-dir>/buildSrc/src .
$ touch build.gradle

We open build.gradle in a text editor. Our plugin code is now nothing more or less than Groovy code, so we need the Groovy plugin for our project. The classes must be compiled into class files, but we have dependencies on the Gradle API for our plugin. When the files where in the buildSrc directory all was well, because Gradle would make sure the classpath was correct. But now we are a normal Groovy project, so we must define the location of the Gradle API class files ourselves. We add the lib directory of our Gradle installation directory to the groovy configuration to resolve the dependencies.

Next we define the location of our company Maven repository with the uploadArchives() method. A special repositories.mavenDeployer() method gives us the possibility to define the location. The uploadArchives task uses this location to deploy the JAR with all classes to the repository.

// File: build.gradle
usePlugin 'groovy'  // Need it to compile plugin classes.
usePlugin 'maven' // Need it to deploy to Maven repository.

// Project configuration is used for Maven deploy:
version = '1.0'
group = 'com.mrhaki.gradle.plugins'

uploadArchives {
    repositories.mavenDeployer {
        repository url: 'file:///shared/repos/m2/repository'

dependencies {
    // Add all JAR files in the lib directory of theGradle installation directory
    // to the groovy configuration (is also used by compile configuration)
    groovy fileTree(dir: new File(gradle.gradleHomeDir, 'lib'), includes: ['*.jar'])

// Make sure all code is compiled, tested and checked before uploadArchives.
uploadArchives.dependsOn ':build'

We run the build to deploy the JAR file to the repository: $ gradle uploadArchives.

Okay we have deployed the plugin to the repository, it is time to use it in another project. We create a new directory and create the build.gradle file and open it in a text editor. We must use the usePlugin() method to enable the plugin, but the class is not yet on the classpath. We need to define the build script dependency on the plugin with the buildscript() method. This will make sure any dependency needed by the build process itself can be resolved.

// File: build.gradle
buildscript {
    repositories {
        mavenRepo urls: 'file:///shared/repos/m2/repository'
    dependencies {
        classpath 'com.mrhaki.gradle.plugins:makedirs-plugin:1.0'


Written with Gradle 0.8.