5. • Flexible build tool
• Based on convention over configuration idea
• Groovy (DSL)
• Open source
• Project Manager: Hans Docter
• GitHub
• First release: April 2008
making the impossible possible,
the possible easy, and the easy elegant
-- Moshé Feldenkrais
2010
8. Intro - DAG
• Read and manipulate graph of tasks, e.g.:
task release(dependsOn: assemble) << {
println 'We release now‘
}
gradle.taskGraph.whenReady {
taskGraph ->
if (taskGraph.hasTask (':release')) {
version = '1.0‘
} else {
version = '1.0-SNAPSHOT‘
}
}
2010
9. Intro - DAG
• Read and manipulate graph of tasks, e.g.:
gradle.taskGraph.beforeTask {
Task task ->
println "executing $task ..."
}
gradle.taskGraph.afterTask {
Task task, TaskState state ->
if (state.failure ) {
println "FAILED"
}
else {
println "done"
}
}
2010
10. Intro - summary
• DSL
• Command line, GUI (+ IDE plugins)
– Reports
– Prunning of tasks tree (-x)
– User friendly (camel case, dry run)
• Rich tasks layer
– Tasks dependencies
– DAG
– Runtime manipulation
2010
11. Frameworkitis is the disease that a framework
wants to do too much for you or it does it in a way that you
don't want but you can't change it. It's fun to get all this
functionality for free, but it hurts when the free
functionality gets in the way. […] To get the desired
behavior you start to fight against the framework. And at this
point you often start to lose, because it's difficult to bend the
framework in a direction it didn't anticipate.
[…] Frameworks try to be in control and tell you when to do
what. A toolkit gives you the building blocks but leaves it
up to you to be in control.
Erich Gamma, www.artima.com
2010
25. repositories {
mavenCentral()
}
More on
repositories
2010
26. repositories {
mavenCentral()
mavenRepo urls:
More on 'http://download.java.net/maven2'
flatDir name: 'localRepository',
repositories dirs: 'lib''
}
2010
27. repositories {
mavenCentral()
mavenRepo urls:
More on 'http://download.java.net/maven2'
flatDir name: 'localRepository',
repositories dirs: 'lib''
}
// Maven2 layout
someroot/[organisation]/[module]/[revision]/[module]-[revision].[ext]
// Typical layout for an ivy repository
someroot/[organisation]/[module]/[revision]/[type]s/[artifact].[ext]
//Simple layout (the organization is not used, no nested folders.)
someroot/[artifact]-[revision].[ext]
2010
32. Core - summary
• Very concise build.gradle file
– DSL
– Convention over configuration
• Configuration is always possible
• Backward compatibility
– Respects standards (layout a'la Maven)
– Ivy & Maven dependencies and repositories
2010
33. Gradle & Maven – dependencies
and repositories
• Full backward compatibility
• Download from and upload to Maven
repos
– Including generation of pom files
• Gradle offers more than Maven
– Uses Apache Ivy
– Mercury (Maven 3) will be supported
2010
34. Gradle – custom logic
• Directly within build script (build.gradle)
– Any Groovy code
– Can import 3rd party classes
2010
35. Gradle – custom logic
• Directly within build script (build.gradle)
– Any Groovy code
– Can import 3rd party classes
• Custom tasks
– build.gradle says ”what”
– ”how” described in task class
2010
36. Gradle – custom logic
• Directly within build script (build.gradle)
– Any Groovy code
– Can import 3rd party classes
• Custom tasks
– build.gradle says ”what”
– ”how” described in task class
• Custom plugins
– More powerful than tasks, but still very easy to create
– Some shipped with Gradle:
• Java, Groovy, Scala, War, Jetty, Maven, Code Quality, OSGi,
Eclipse, Project Report
2010
37. Gradle – custom logic
• Directly within build script (build.gradle)
– Any Groovy code
– Can import 3rd party classes
• Custom tasks
– build.gradle says ”what”
– ”how” described in task class
• Custom plugins
– More powerful than tasks, but still very easy to create
– Some shipped with Gradle:
• Java, Groovy, Scala, War, Jetty, Maven, Code Quality, OSGi,
Eclipse, Project Report
2010
38. Custom logic :
”how” described in task class
public class ReportTask extends DefaultTask {
def FileCollection jars
@TaskAction def createReport() {
def text = new StringBuilder()
text.append("gradle -v".execute().text)
jars.each {
text.append("t- $it.namen")
}
println "GENERATING REPORT"
new File('build/report.txt') << text
}
}
2010
41. Web UI
apply plugin: 'war'
repositories {
...
}
dependencies {
...
}
|-- build.gradle
`-- src Convention
`-- main
|-- java
over
|-- resources configuration
`-- webapp
2010
42. Web UI
apply plugin: 'war' :war - Generates a war archive
with all the compiled classes,
repositories {
the web-app content and the
... libraries.
}
dependencies {
...
}
Convention
over
configuration
2010
43. Web UI
apply plugin: 'war' :war - Generates a war archive
apply plugin: 'jetty' with all the compiled classes,
the web-app content and the
repositories { libraries.
...
} :jettyRun - Uses your files as
dependencies { and where they are and deploys
... them to Jetty.
}
:jettyRunWar - Assembles the
webapp into a war and deploys
it to Jetty.
2010
45. Gradle & Ant – import of build.xml
<project>
<target name="hello">
<echo>Hello, from Ant</echo>
</target>
</project>
2010
46. Gradle & Ant – import of build.xml
<project>
<target name="hello">
<echo>Hello, from Ant</echo>
</target>
</project>
ant.importBuild "build.xml"
task myTask(dependsOn: hello) << {
println "depends on hello ant task"
}
2010
47. Gradle & Ant – import of build.xml
<project>
<target name="hello">
<echo>Hello, from Ant</echo>
</target>
</project>
ant.importBuild "build.xml"
task myTask(dependsOn: hello) << {
println "depends on hello ant task"
}
>gradle myTask
:hello
[ant:echo] Hello, from Ant
:myTask
depends on hello ant task
2010
48. Gradle & Ant – use of Ant tasks
task zip << {
ant.zip(destfile: 'archive.zip') {
fileset(dir: 'src') {
include(name: '**.xml')
exclude(name: '**.java')
}
}
2010
49. Gradle & Ant – use of Ant tasks
task zip << {
ant.zip(destfile: 'archive.zip') {
fileset(dir: 'src') {
include(name: '**.xml')
exclude(name: '**.java')
}
}
task dist(type: Zip) {
from 'src/dist‘
from configurations.runtime
into('libs‘)
}
2010
50. Gradle & Ant - summary
• Ant targets = Gradle tasks
• Import of build.xml
• Use of Ant
– ant object available in every build.gradle
• AntBuilder used beneath
– some Ant tasks rewritten
• for optimization purposes and to be consistent
with other concepts of Gradle
• Gradle = Ant with a boost
2010
54. Web & Desktop UI - summary
• Convention over configuration makes
things easy
• Jetty plugin available out-of-the-box
• Many JVM languages supported
– Java, Groovy, Scala
2010
60. Multi-module build - summary
• Layout – its up to you
• Number of build.gradle files – you decide
• Smart (partial) builds
• Project treated as tasks
– You can depend on them
2010