Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Table of Contents
outlinetrue

This page gathers information on plugins that you can use in your Gradle build scripts to enhance it with new capabilities.

If you are looking for plugins that allows other 3rd party tools to work with Gradle (e.g. make your IDE understand Gradle build, or allow your CI server to run Gradle builds) please see the Cookbook.

Android Plugin

This plugin enables the creation of Android applications using Gradle, with all of the power and flexibility you’ve come to expect from Gradle.

Features of the Android plugin include:

  • Compile, package, and install Android applications. (Including handling of Android resource files.)
  • Sign application packages using the default debug key, or with a release key for publication to Android Market.
  • Incorporation of ProGuard to ensure that applications have minimal memory footprint.
  • Create Android applications in Scala (and possibly Groovy or Clojure).

The Android plugin fully integrates into the Gradle build lifecycle by extending the Java plugin. Furthermore, the incorporation of ProGuard into the build not only ensures that Android application packages are small and tight, it also trivially enables the use of Scala for Android application development simply by incorporating the existing Scala plugin into the build. ProGuard will include only those classes from the Scala library that are actually used by your Android application, resulting in an application package that is as small as possible.

Full details are available at http://wiki.github.com/jvoegele/gradle-android-plugin/

AspectJ plugin

This plugin adds aspectj support to the java plugin. Additional configurations (ajc, inpath and aspects) are used to configure the required dependencies. Furthermore it adds ajdt support to the gradle eclipse plugin. The plugin is available at http://github.com/breskeby/gradleplugins/blob/0.9-upgrade/aspectjPlugin/aspectJ.gradle

The plugin can be used by adding the following line to your gradle (v0.9+) build file:

apply url:'http://github.com/breskeby/gradleplugins/raw/0.9-upgrade/aspectjPlugin/aspectJ.gradle'

An example is available at http://github.com/breskeby/gradleplugins/tree/0.9-upgrade/aspectjPlugin/examples/
For further explanations have a look at http://www.breskeby.com/2010/02/using-gradle-with-aspectj/ and http://www.breskeby.com/2010/03/speed-up-your-build-with-gradle/
author: René Gröschke

Cargo Plugin

A Gradle plugin that provides deployment capabilities to local and remote containers via Cargo.

Author: Benjamin Muschko

Home: https://github.com/bmuschko/gradle-cargo-plugin

ClassLoader plugin

tbd. short info on what is the purpose ?

author: Robert Fisher
how-to available at: http://github.com/RobertFischer/gradle-plugins/blob/master/README.md

Clojuresque plugin

Formerly known as clj-gradle plugin.

author: Meikel Brandmeyerhttp://bitbucket.org/kotarak/clojuresque

Cobertura Plugin

Adds Cobertura coverage reporting tasks for Gradle.

https://github.com/valkolovos/gradle_cobertura/wiki

author: Val Kolovos

Cuke plugin

author: Robert Fischer

Runs cucumber.

https://github.com/RobertFischer/gradle-plugins/

DepNames plugin

author: Robert Fisher

Provides the ability to keep external dependency names and versions in one location, and use them throughout a number of projects. By assigning a Java identifier as a key and a configuration spec as a value in a properties file, that identifier becomes available in the dependencies configuration block.

http://enfranchisedmind.com/blog/posts/gradle-depnames/

Emma plugin

This plugin adds Emma code coverage reporting to the java plugin test task. It can be used in single- or multiproject builds. The plugin is available at http://github.com/breskeby/gradleplugins/tree/master/emmaPlugin

The plugin can be used by adding the following line to your gradle (v0.9+) build file:

apply url:'http://github.com/breskeby/gradleplugins/raw/master/emmaPlugin/emma.gradle'

An example is available at http://github.com/breskeby/gradleplugins/tree/master/emmaPlugin/sample/
For further explanations have a look at http://www.breskeby.com/2010/04/add-emma-code-coverage-reporting-to-your-gradle-build/
author: René Gröschke

Exec plugin

Allows to execute shell commands.

author: Robert Fisher
how-to available at: http://github.com/RobertFischer/gradle-plugins/blob/master/README.md

Env plugin

Allows easy access to environment variables.

author: Robert Fisher
how-to available at: http://github.com/RobertFischer/gradle-plugins/blob/master/README.md

Google App Engine (GAE) Plugin

A Gradle plugin that provides tasks for uploading, running and managing Google App Engine Java projects.

Author: Benjamin Muschko

Home: https://github.com/bmuschko/gradle-gae-plugin

Gaelyk Plugin

A Gradle plugin that provides tasks for managing Gaelyk projects.

Author: Vladimír Oraný, Benjamin Muschko

Home: https://github.com/bmuschko/gradle-gaelyk-plugin

Grails-Gradle Plugin

Build grails projects using gradle.

author: Peter Ledbrook
See https://github.com/grails/grails-gradle-plugin

Groovy-eclipse Plugin

Uses the groovy eclipse compiler for Java and Groovy compilation instead of the default javac/groovy compilers.

author: Adam Murdoch
See https://github.com/adammurdoch/gradle-groovy-eclipse-compiler#readme

GWT Plugin

author: Markus Kobler

http://github.com/markuskobler/gwt-gradle-plugin

IzPack Plugin

A Gradle plugin that provides support for packaging applications for the Java platform via IzPack.

Author: Benjamin Muschko

Home: https://github.com/bmuschko/gradle-izpack-plugin

Javascript Library Plugin

A Gradle plugin for building Javascript Libraries. Allows for source file combination - combining smaller js files into larger ones for user download, and source file filtering to remove unwanted debug code from artifacts.

author: Eric Berry

https://launchpad.net/gradle-jslib

JavaFx Plugin

A Gradle plugin for building JavaFx source. Based on Clojuresque.

Author: Andrew Spina

Home: http://code.google.com/p/gradle-plugin-javafx/

jDocbook Plugin

Author: Steve Ebersole

Home: http://github.com/sebersole/jdocbook-core

Please read this post: http://gradle.1045684.n5.nabble.com/jDocBook-for-Gradle-td1432002.html#a1432002

OneJar plugin

author: Robert Fischer

Provides tasks to generate a single executable jar containing dependencies. Uses one-jar under the hood. The resulting jar file will be next to the standard jar, but with '-fat' attached to the name.

https://github.com/RobertFischer/gradle-plugins/

SvnKit Plugin

A Gradle plugin to connect a project to a Subversion repository using the SvnKit library (http://www.svnkit.com/)

Author: Walter Di Carlo

Home: http://code.google.com/p/gradle-wdc-plugins/

Tomcat Plugin

A Gradle plugin supporting deployment of your web application to an embedded Tomcat web container.

Author: Benjamin Muschko

Home: https://github.com/bmuschko/gradle-tomcat-plugin

Xslt Plugin

A Gradle plug-in that defines a custom task type that can be used to transform one or more documents using XSLT.

Author: Paul Speed

The last version as of this post can be found here: http://filament.svn.sourceforge.net/viewvc/filament/trunk/gradle-plugins/xslt.gradle?revision=445

Some documentation exists in the header of that plug-in file but here is an example usage from the header:

No Format
task checkstyleReport(type: Xslt, dependsOn: check) {
    source project.checkstyleResultsDir
    include '*.xml'
    destDir = project.checkstyleResultsDir
    extension = 'html'

    stylesheetFile = file( 'config/checkstyle/checkstyle-noframes.xsl' )
}

The above definition requires that the specified XSL file be copied in with the other checkstyle configuration files. (The file in the example is part of the checkstyle distribution.)

Workspace Plugin

A Gradle plugin to:
- load project properties from a CSV file
- create sub-project's build.gradle file from sections of a central workspace file
- add conditional project dependencies

Author: Walter Di Carlo

Home: http://code.google.com/p/gradle-wdc-plugins/

PGP Plugin

This plugin won't be supported anymore since Gradle includes a similar but better implementation itself.

See "Signing plugin" in the documentation.

This plugin creates PGP signatures for all Maven artifact files as required for inclusion in the Central Maven Repository.
Description and example are contained in the README and will be enhanced over time.

Current version is 0.0.3.

Add the following to your build script:

Code Block
buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'de.huxhorn.gradle:de.huxhorn.gradle.pgp-plugin:0.0.3'
  }
}

You can then apply the plugin:

Code Block
allprojects {
  // below doesn't work yet...
  // apply plugin: 'sign'
  apply plugin: de.huxhorn.gradle.pgp.PgpPlugin


  pgp {
    secretKeyRingFile = new File("${System.properties['user.home']}/.gnupg/secring.gpg")
    keyId = '740A1840'
  }
}

Configuration is either done by the pgp convention like this:

Code Block
pgp {
  secretKeyRingFile = new File("${System.properties['user.home']}/.gnupg/secring.gpg")
  keyId = '740A1840'
  password = 'youShouldNotDoThis'
}

or using gradle properties:

Code Block
-PpgpKeyId=740A1840
-PpgpSecretKeyRingFile=/path/to/file
-PpgpPassword=youShoudlntDoThisEither

Information in the pgp convention overrides gradle properties! Because of this, it is possible to sign sub-modules using different keys in case of a multi-module project.

Instead of specifying the password using either convention or gradle property you should simply omit it.

In that case, the user will be asked for the password at build time - which is the most secure option.

Author: Jörn Huxhorn

Home: http://github.com/huxi/huxi-gradle-plugins

Protocol Buffers Plugin

Gradle build plugin to handle Protocol Buffers automated code generation and compilation

Author: Alex Antonov
Home: http://code.google.com/p/gradle-plugin-protobuf

org.linkedin plugins

* Author: Yan Pujante
* Available at https://github.com/linkedin/gradle-plugins in source form
* Latest version (1.5.0) available at maven central: org.linkedin:org.linkedin:org.linkedin.gradle-plugins:1.5.0
* Check out the file README.md for details

a. org.linkedin.userConfig

Plugin which attempts to load user configuration from a set of various files and make it available to all gradle build files as a 'userConfig' object (instance of groovy.util.ConfigObject).

Code Block
titleExample of usage
apply plugin: 'org.linkedin.userConfig'
topBuildDir = userConfig.top.build.dir ?: "${rootDir}/out/build"

b. org.linkedin.spec

This plugin reads a file called project-spec.groovy and makes it available in all build files as a 'spec' object (instance of java.util.Map)

Code Block
titleExample of usage
//project-spec.groovy
spec = [
    name: 'gradle-plugins',
    group: 'org.linkedin',
    version: '1.1.0',
    versions: [
      groovy: '1.7.5'
    ]
]
spec.external = [
    json: 'org.json:json:20090211',
    groovy: "org.codehaus.groovy:groovy:${spec.versions.groovy}"
]

// build.gradle:
apply plugin: 'org.linkedin.spec'
allprojects {
  group = spec.group
  version = spec.version
  dependencies {
   compile spec.external.json
   groovy spec.external.groovy
  }
}

c. org.linkedin.repository

This plugin allows you to externalize repository configuration and override it with your own defaults. It tries to read a set of files and make it available to your build script as a 'allRepositories' object.

Code Block
titleExample of usage
// repositories.gradle
allRepositories.buildscript = {
  mavenCentral()
}

// build.gradle:
apply plugin: 'org.linkedin.repository'
subprojects {
  buildscript {
    allRepositories.buildscript.configure(repositories)
  }
}

d. org.linkedin.release

This plugin adds 'release' and 'publish' tasks to easily release (in a local repo) or publish (in a remote repo). It automatically handles sources / javadoc / groovydoc. The repositories are configured using the 'org.linkedin.repository' plugin. The plugin is 'snapshot aware' and can be used with the org.linkedin.spec plugin to easily switch between snapshot and non snapshot releases.

Code Block
titleExample of usage
gradle release

// if using the org.linkedin.spec plugin
gradle publish -Psnapshot=true

e. org.linkedin.cmdline

This plugin adds the following tasks to create a packaged command line (tgz by default).
* package-assemble: Assembles the package (exploded)
* package: Create the package
* package-install: Install the package (locally)
* package-clean-install: Cleans the (previously) installed package

This plugin automatically copies src/cmdline/resources into the root of the tar ball and deploy all dependencies (jars) in the lib folder (of the tar ball). This plugin can be used with the org.linkedin.release plugin to release/publish the package.

JSLint Plugin

A plugin to enable static analysis of JavaScript code using JSLint.

author: Kelly Robinson

source: https://github.com/kellyrob99/gradle-jslint-plugin

Writing Custom Plugins

See: Writing Custom Plugins chapter in Gradle User Guide

Example of a simple custom task within buildSrc

In your build.gradle, add

Code Block
task copyToLib(type: CopyToLib)

and put the class in buildSrc/src/main/groovy

Code Block
titleCopyToLib.groovy
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

class CopyToLib extends DefaultTask {

    CopyToLib() {
        dependsOn("jar")
    }

    @TaskAction
    def run() {
        libDir = project.file('build/output/lib')
        libDir.mkdirs()
        project.configurations.default.each { File file ->
            ant.copy(file: file.path, toDir: libDir)
        }
        project.configurations.default.allArtifacts.each { artifact ->
            ant.copy(file: artifact.file, todir: libDir)
        }
    }

}

and then you can do

No Format
gradle copyToLib

Example of a simple custom plugin within buildSrc

This plugin creates a java file with a getter for the version, group and name derived from the build.gradle file. Such a class can be useful for printing the current version of a application, and you don't want the actual version in two places as you will forget to change one of them at release time.

In your build.gradle, add

Code Block
apply plugin: VersionClass

and put the class below in buildSrc/src/main/groovy

Code Block
titleVersionClass.groovy
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.plugins.JavaPlugin
import java.util.Date;

class VersionClass implements Plugin<Project>  {
    
    VersionClass() {
    }
    
    def void apply(Project project) {
        project.getPlugins().apply( JavaPlugin.class )
        def genSrc = 'generated-src/version'
        def generatedSrcDir = new File(project.buildDir, genSrc)
        
        def makeVersionClassTask = project.task('makeVersionClass') << {
            def now = new Date()
            def outFilename = "java/"+project.group.replace('.','/')+"/"+project.name+"/BuildVersion.java"
            def outFile = new File(generatedSrcDir, outFilename)
            outFile.getParentFile().mkdirs()
            def f = new FileWriter(outFile)
            f.write('package  '+project.group+"."+project.name+';\n')
            f.write("""
/**
 * Simple class for storing the version derived from the gradle build.gradle file.
 *
 */
public class BuildVersion {

    private static final String version = \""""+project.version+"""\";
    private static final String name = \""""+project.name+"""\";
    private static final String group = \""""+project.group+"""\";
    private static final String date = \""""+now+"""\";

    /** returns the version of the project from the gradle build.gradle file. */
    public static String getVersion() {
        return version;
    }
    /** returns the name of the project from the gradle build.gradle file. */
    public static String getName() {
        return name;
    }
    /** returns the group of the project from the gradle build.gradle file. */
    public static String getGroup() {
        return group;
    }
    /** returns the date this file was generated, usually the last date that the project was modified. */
    public static String getDate() {
        return date;
    }
    public static String getDetailedVersion() {
        return getGroup()+":"+getName()+":"+getVersion()+" "+getDate();
    }
""")
            
            f.write("}\n")
            f.close()
        }
        project.sourceSets {
            version {
                java {
                    srcDir project.buildDir.name+'/'+genSrc+'/java'
                }
            }
        }
        makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource() )
        makeVersionClassTask.getOutputs().files(generatedSrcDir)
        if (project.getBuildFile() != null && project.getBuildFile().exists()) {
            makeVersionClassTask.getInputs().files(project.getBuildFile())
        }
        project.getTasks().getByName('compileJava').dependsOn('compileVersionJava')
        project.getTasks().getByName('compileVersionJava').dependsOn('makeVersionClass')
        project.getTasks().getByName('jar') {
            from project.sourceSets.version.classes
        }
    }
}

and then you can do

No Format
gradle makeVersionClass

to generate a java class with the version from the build.gradle.

Also, gradle will automatically compile this class and add it to the jar default jar along with the normal main java code.

Obviously, this whole function might be better accomplished by simply printing the project.version into a text file
in the resources directory, but this serves as a basic, but non-trivial, plugin example as well as an example of how to deal with generated java code.

Building an external plugin (outside buildSrc)

See: Gradle Build Script (build.gradle) for Writing Gradle Plugins