Gradle

From XennisWiki
Jump to: navigation, search

Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven of declaring the project configuration. Gradle uses a directed acyclic graph ("DAG") to determine the order in which tasks can be run. (Wikipedia)

Installation

Arch Linux

Using Pacman

pacman -S gradle

Debian

add-apt-repository ppa:cwchien/gradle
apt-get update
apt-get install gradle

Setup

Create new Java project

cd myProject
gradle init --type java-library

Plugins

Checkstyle

Further information: Checkstyle Plugin

Add to the build.gradle

apply plugin: 'checkstyle'

Run Checkstyle agains the production or test Java source files

./gradlew checkstyleMain
./gradlew checkstyleTest

FindBugs

Further information: Checkstyle Plugin

Add to the build.gradle

apply plugin: 'findbugs'

Run Checkstyle agains the production or test Java source files

./gradlew findbugsMain
./gradlew findbugsTest

Gradle Task Tree

Further information: Gradle Task Tree

./gradlew <task> taskTree --no-repeat

Basics

build.groovy

task first << {
	println "My first task"
}

task second << {
	println "And another task"
}

second.dependsOn << [ first ]

Run

gradle first
gradle second

Description and group

build.groovy

task example {
	description = 'That is my example task.'
	group 'example'
}

Variables

build.groovy

task printColor << {
	println "Color: ${color}"
}

gradle.properties

color = red

Run

gradle -Pcolor=blue printColor

Configuration and do first

build.groovy

task example {
	println "Configurations phase"
}

example.doFirst {
	print "First execute this ... "
}

task example << {
	println "... and then then the taks itself"
}

Create tasks dynamic

build.groovy

[ 'first', 'second' ].each { value ->
    task "${value}Task" << {
    	println "${value} task"
    }
}
[ 'first' : 'My first task', 'second' : 'And another task' ].each { key, value ->
    task( "${key}Task" ) << {
   	    println "${value}"
    }
}

Advanced

Copy files

task copyFiles << {
	copy {
		from 'src/resources'
		into 'build'
	}
}

Using subprojects

In this case two subprojects are in a directory named subproject

 |- build.gradle
 |- settings.gradle
 |- subproject
 |  |- first-subproject
 |  |  |- build.gradle
 |  |- second-subproject
 |  |  |- build.gradle

Include the subprojects in the main settings.gradle file

rootProject.name = 'my-project'

include 'subproject:first-subproject'
include 'subproject:second-subproject'

Dependencies between subprojects

To let the second subproject depend on the other, add this dependency in the subproject/second-subproject/build.gradle file

dependencies {
    compile project(':subproject:first-subproject')
}

Execute task

Dry run a task

./gradlew <task> --dry-run

Settings

Global settings

Global settings can be made in file ~/.gradle/gradle.properties

# Enable daemon
org.gradle.daemon=true
# Enable parallel
org.gradle.parallel=true

File variable

compileJava {
    [...]
    ext.generatedSourcePath=file("build/src/example")
    options.compilerArgs.addAll([
        "-s",
        sourcePath.path
    ])
}

compileJava.doFirst {
    sourcePath.mkdirs()
}

Force rerun taks

Tasks that are "up to date" won't run again. Append a --rerun-tasks to force to rerun all tasks.

See also

External links