Loading...

October 26, 2010

Gradle Goodness: Base Plugin Usage

Gradle has plugins to provide functionality in a modularized way. One of the basic plugins is the base plugin. This plugin is not part of the public API of Gradle, so the functionality can change. In this post we look at what functionality the base plugin provides for Gradle version 0.9-rc1.

When we apply the base plugin to our project we get a couple of tasks we can use:

assemble
task that builds all archives of the project.
clean
task that deletes the build directory.

We get two configurations:

archives
configuration for archives.
default
extends archives configuration.

For each configuration we get a build and upload task:

buildArchives and buildDefault
builds all artifacts for the configuration.
uploadArchives and uploadDefault
upload artifacts for the configuration.

And we get a task rule clean<taskname> that is capable of cleaning the ouput files of any task we create in our project.

Also the following properties are added to the project:

distDirName
directory to store archives created by the project.
libsDirName
directory to store JAR files created by the project.
archivesBaseName
base name for archives.

In the following sample we define a project with the base plugin and use the tasks, configurations and properties that are added by the plugin:

apply plugin: 'base'

version = '1.0'

archivesBaseName = 'sample'
distsDirName = 'dist'
libsDirName = 'projectlibs'

task simpleJar(type: Jar)

task simpleZip(type: Zip)

artifacts {
    archives simpleJar
}

repositories {
    flatDir name: 'localRepo', dirs: "$buildDir/repository"
}

uploadArchives {
    repositories {
        add project.repositories.localRepo
    }
}

Let's run gradle with a couple of the tasks:

$ gradle clean assemble buildDefault uploadArchives cleanSimpleJar
:clean
:simpleJar
:simpleZip
:assemble
:buildDefault
:uploadArchives
:cleanSimpleJar

BUILD SUCCESSFUL

Total time: 3.416 secs

If we look in the build directory we see what is created:

build
  |
  +- dist
  |    |
  |    +- sample-1.0.zip
  |
  +- projectlibs
  |
  +- repository
       |
       +- sample-1.0.jar