Map map = [language: 'Groovy']
List list = ['foo', 'bar']
Craig Burke
Groovy/Grails Developer at Carnegie Mellon
Author of the Karma / Bower Installer Gradle Plugins
www.craigburke.com
@craigburke1
@groovypgh
Groovy DSL
Dependency management
Support for many languages/platforms
Plugins
A terrible website
Simple Syntax (Less Noise)
Operator Overloading
Scripting Support
Meta-Programming
Map map = [language: 'Groovy']
List list = ['foo', 'bar']
// equivalent to: say(hello).to('Craig')
say hello to 'Craig'
a + b | a.plus(b) |
a - b | a.minus(b) |
a * b | a.multiply(b) |
a ** b | a.power(b) |
a / b | a.div(b) |
a % b | a.mod(b) |
a | b | a.or(b) |
a & b | a.and(b) |
a ^ b | a.xor(b) |
a or a | a.next() |
a-- or --a | a.previous() |
a << b | a.leftShift(b) |
a >> b | a.rightShift(b) |
a <⇒ b | a.compareTo(b) |
Every class in Groovy has a corresponding metaClass
Class instances can also have custom metaClasses
A first-class function that can be:
Passed as an argument
Assigned to a variable
Returned from a function
instance of the class the closure was defined
usually the same as this unless it was declared inside another closure
same as owner but can be reassigned
class Person {
String name
String nickName
}
Person person = new Person()
person.name = 'Craig'
println person.name
person.nickName = person.name
Number.metaClass.getDollars = { delegate as BigDecimal }
Number.metaClass.getProperty = { String name ->
def rates = [euros: 1.1f, pesos: 0.063f]
delegate * (rates[name] as BigDecimal)
}
def total = 20.dollars + 40.euros + 200.pesos
assert total == 76.60
class Person {
def methodMissing(String name, args) {
if (name.startsWith('say')) {
String message = (name - 'say').trim()
println message
}
}
}
Person you = new Person()
you.sayHello()
you."say Craig is Awesome"()
an atomic piece of work
collection of tasks
one or more projects
Use SDKMan!
Download from gradle.org: http://gradle.org/gradle-download/
Use Gradle wrapper
Create a build.gradle file
Run gradle init
gradle init
gradle init --type groovy-library
task helloWorld {
doLast {
println 'Hello world!'
}
}
run for all tasks everytime gradle is run
run only when a task is executed
task foo {
println 'Configuration phase'
doLast {
println 'Execution phase'
}
}
Not recommended
task foo << {
println "Execution phase"
}
task bar {
println "Configuration phase"
} << {
println "Execution phase"
}
dependsOn
finalizedBy
shouldRunAfter
mustRunAfter
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'java'
compiles and packages project (into a jar)
runs tests and verification tasks
runs both assemble and check
apply plugin: 'groovy'
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
compile 'org.codehaus.groovy:groovy:2.4.5'
testCompile group:'org.spockframework', name:'spock-core', version:'1.0-groovy-2.4'
}
The dependencies required to compile the source of the project.
The dependencies required by the at runtime (includes compile time dependencies).
The additional dependencies required to compile the tests the project (includes compile dependecies).
The dependencies required by the tests at runtime (includes testCompile and runtime dependecies).
apply from file
Add a plugin code in buildSrc
Add a build dependency of published plugin
Build classpath is distinct from application classpath
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
apply plugin: 'groovy'
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
jcenter()
}
dependencies {
compile 'org.codehaus.groovy:groovy:2.4.5'
}
Gradle Shadow (johnrengelman/shadow)
Gradle SSH (int128/gradle-ssh-plugin)
Gradle Node (srs/gradle-node-plugin)
Bower Installer (craigburke/bower-installer-gradle)
bower {
installBase = 'src/assets/bower'
'angular'('1.4.x')
'angular-animate'('1.4.x') {
source 'angular-animate.js' >> '/angular/modules/'
}
'bootstrap'('3.3.x') {
source 'dist/css/*.min.css' >> 'styles/'
source 'dist/fonts/**' >> 'fonts/'
excludes 'jquery'
}
}
Books
Building and Testing with Gradle
Gradle Beyond the Basics!
Gradle In Action
Presentations
Building A Full Application Stack With Gradle
Gradle: From User to Addict
Links
Gradle User Guide