On Github AdamRoberts / GradlePresentationForHJUG
Open Source by Gradleware (Apache 2.0 license)
class GreetingTask extends DefaultTask {
@TaskAction
def greet() {
println 'Hello World!'
}
}
task hello(type: GreetingTask)
$ gradle hello :hello Hello World! BUILD SUCCESSFUL
task helloAgain(type: GreetingTask, dependsOn: hello)
$ gradle helloAgain :hello Hello World! :helloAgain Hello World! BUILD SUCCESSFUL
task redundantGreeting(type: GreetingTask) {
doFirst {
println 'Hello Hello Hello'
}
}
$ gradle redundantGreeting :redundantGreeting Hello Hello Hello Hello World! BUILD SUCCESSFUL
task helloAndGoodBye(type: GreetingTask) {
doLast {
println 'Goodbye World'
}
}
$ gradle helloAndGoodbye :helloAndGoodBye Hello World! Goodbye World BUILD SUCCESSFUL
class ConfigurableGreetingTask extends DefaultTask {
def person = 'World'
@TaskAction
def greet() {
println "Hello ${person}!"
}
}
task helloDuke(type: ConfigurableGreetingTask) {
person = 'Duke'
}
$ gradle helloDuke :helloDuke Hello Duke! BUILD SUCCESSFUL
task version1(type: ConfigurableGreetingTask) {
person = 'Duke'
doFirst() {
println 'Hello Everyone!'
}
doLast() {
println "I hope you are having a good day ${person}"
}
}
task version2(type: ConfigurableGreetingTask)
version2.person = 'Duke'
version2.doFirst() {
println 'Hello Everyone!'
}
version2 << {
println "I hope you are having a good day ${person}"
}
task cleanUp {
doFirst() { println 'Cleanup executed' }
}
task fails {
finalizedBy cleanUp
doFirst() { throw new RuntimeException() }
}
$ gradle fails :fails FAILED :cleanUp Cleanup executed FAILURE: Build failed with an exception. ...
configurations {
compile
runtime {
description = "Used at runtime but should not be inherited"
extendsFrom compile
}
}
configurations.compile {
description = 'You can access an already declared configuration'
}
Creating a new configuration is easy
You can extend an existing configuration to inherit the dependencies
You can easily access an already created configuration
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
runtime 'org.slf4j:slf4j-log4j12:1.7.5'
runtime group: 'org.apache', name: 'tomcat', version: '7.0.34', ext: 'zip'
compile files('libs/a.jar', 'libs/b.jar')
compile("com.sun.jersey:jersey-json:1.12") {
exclude group: 'stax', module: 'stax-api'
}
}slf4j will be accessed from a declared repository and made available to any task that uses the compile dependency when the configuration's settings need to be overriden for a single artifact they can be File dependencies are never transitive now that we have used the configurations you can see how each one cooresponds to a seperate classpath
repositories {
mavenCentral()
maven { url "http://repo.mycompany.com/maven2" }
ivy { url "http://repo.mycompany.com/repo" }
flatDir { dirs 'lib1', 'lib2' }
localRepository { dirs 'lib' }
}
apply plugin: 'example-plugin'The plugin then:
configurations {
pluginConf
}
class PluginTask extends DefaultTask {
@TaskAction
def action() {
def deps = project.configurations.pluginConf.allDependencies
... do something ...
}
}
task pluginTask(type: PluginTask)
apply plugin: 'java'
sourceSets {
newCode
}
$ gradle tasks --all
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
...
newCodeClasses - Assembles binary 'newCode'.
...
compileNewCodeJava - Compiles source set 'newCode:java'.
processNewCodeResources - Processes source set 'newCode:resources'.
...
BUILD SUCCESSFUL
jettyRun {
copy {
from 'src/bdd/resources/web.xml'
into "${buildDir}/webapp/WEB-INF"
}
}
jettyRun {
doFirst {
copy {
from 'src/bdd/resources/web.xml'
into "${buildDir}/webapp/WEB-INF"
}
}
}