For the long time, we have used various build tools to package applications for new software releases or applying patches to existing applications etc. dependency management, version controlling, scalability, flexibility, single-multiple projects sup portability are some of the key areas that drove the selection of a build tool, This session focuses on Gradle as a successful build tool and looks into all the above areas and uses Groovy as a DSL. We will also look into how easy it is to use Gradle as compared to other open source build tools.
Photos: https://plus.google.com/u/0/photos/105295086916869617504/albums/5739617166453582993
Gradle build tool that rocks with DSL By Rajmahendra Hegde at JavaOne Hyderabad, India on 4th May 2012
3. Agenda
• Build tools
• Build tool basics
• Gradle
• Getting Started
• Gradle Tasks
• Gradle with Ant
• Gradle with Maven
• Plugins
• Multi Project Support
• Project Templates
• IDEs Support
4. Build Tool
Jar
Source Files, War
Resource
Files
etc. jpi
Automated Build Tool XYZ...
5. Build Tool
• Initialize • Generic build process
• checkout • Dev, Test, Integ,
• Compile environment
• Check-style • Continuous Integration
• Test
• Code coverage
• Jar
• War
• Ear
• Deploy
• etc...
6. Evolution of build tools
• Javac, Jar.. - Command based
• IDEs – Application based
(a need for building application outside the IDEs!) (this is the age of onsite deployment and
Continuous Integration)
• Ant – Task based - (XML)
• Maven – Goal based (XML)
• …
• Gradle – A mix of good practices/tools(ant, maven,ivy etc.) with a flavor of
DSL
8. Gradle is
• A general purpose Java build system
• Platform independent
• DSL based
• Built for java based projects
• Full support of Grooy, Ant and Maven
• Task based system
• Convention over configuration
• Flexible, scalable, extensible
• Plugins
• Flexible multi-project support
• Free and open source
9. Why
Core Java JVM Language
No No
<XML> <XML>
Use Use
@annotation DSL{}
10. DSL? Domain Specific Language
A domain-specific language (DSL) is a programming language or specification language dedicated
to a particular problem domain, a particular problem representation technique, and/or a particular
solution technique. - Wikipedia
Examples
Chess Notation
1. e4 e5 2. Nf3 Nc6 3. Bb5 a6
1. P-K4 P-K4 2. N-KB3 N-QB3 3. B-B4 B-B4
Music
Western Musical Notation – C, D, E, F, G, A, B, C
Solfège syllables – Do, Re, Mi, Fa, Sol, La, Ti, Do.
Carnatic Music Notation – Sa Re Ga Ma Pa Da Ne Sa
Guitar Tab - 0 1 2 3 4
Harmonica Tab – 1b 1b 2d 2d
Rubik's
Cube Notation - d', d2. f, f', f2, b, b', b2
Very popular in our own field SQL! SELECT * FROM MYTABLE WHERE MYFIELD = 4
And many...
11. Read about DSL
DSLs in Action
By - Debasish Ghosh
Forewords by: Jonas Bonér
December, 2010 | 376 pages
ISBN: 9781935182450
DSLs in Action introduces the concepts you'll need
to build high-quality domain-specific languages. It
explores DSL implementation based on JVM
languages like Java, Scala, Clojure, Ruby, and
Groovy and contains fully explained code snippets
that implement real-world DSL designs. For
experienced developers, the book addresses the
intricacies of DSL design without the pain of writing
parsers by hand.
http://www.manning.com/ghosh/
12. Getting Started
• Download binary zip from gradle.org $ gradle -v
• Unzip in your favorite folder
--------------------------------------
• Set GRADLE_HOME Env. Variable Gradle 1.0-rc-2
--------------------------------------
• Add GRADLE_HOME[/ or ]bin to PATH
Gradle build time: Tuesday, April 24, 2012
• To test 11:52:37 PM UTC
$ gradle -v Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.2 compiled
on December 20 2010
• Build File name: Ivy: 2.2.0
– build.gradle JVM: 1.6.0_31 (Apple Inc. 20.6-b01-415)
– gradle.properties OS: Mac OS X 10.7.3 x86_64
– settings.gradle
13. Build Lifecycle
• Initialization
l
Initializes the scope of the build
l
Identifies projects [multi-project env.] involved
l
Creates Project instance
• Configuration
l
Executes buildscript{} for all its scope
l
Configures the project objects
• Execution
l
Determines the subset of the tasks
l
Runs the build
14. Gradle Tasks
task mytask << { $ gradle mytask
println 'Hello World' :mytask
} The First
Hello World
mytask.doFirst { The Last
println 'The First' Add more
} BUILD SUCCESSFUL
mytask.doLast {
println 'The Last'
}
mytask << {
println 'Add more'
}
18. Gradle DAG
task distribution << { $ gradle distribution
println "We build the zip with :distribution
version=$version" We build the zip with version=1.0-
} SNAPSHOT
BUILD SUCCESSFUL
task release(dependsOn:
'distribution') << { $ gradle release
println 'We release now' :distribution
}
We build the zip with version=1.0
:release
gradle.taskGraph.whenReady
{taskGraph → We release now
BUILD SUCCESSFUL
if (taskGraph.hasTask(release)) {
version = '1.0'
} else {
version = '1.0-SNAPSHOT'
}
}
From Gradle Userguide
19. Configuring Tasks
task copy(type: Zip) { // OR
from 'resources' task myCopy(type: Zip)
into 'target' myCopy {
include('**/*.properties') from 'resources'
} into 'target'
include( '**/*.properties')
// OR }
task myCopy(type: Zip) // OR
myCopy.configure {
from('source') task(myCopy, type: Zip)
into('target') .from('resources')
include('**/*.properties') .into('target')
} .include( '**/*.properties')
20. Gradle with Ant
• Ant is first-class-citizen for Gradle
• ant Builder
• Available in all .gradle file
• Ant .xml
• Directly import existing ant into Gradle build!
• Ant targets can be called directly
21. Gradle with Ant...
task callAnt << { $ gradle callAnt
ant.echo (message: 'Hello Ant 1') :callAnt
ant.echo ('Hello Ant 2') [ant:echo] Hello Ant 1
ant.echo message: 'Hello Ant 3' [ant:echo] Hello Ant 2
ant.echo 'Hello Ant 4' [ant:echo] Hello Ant 3
} [ant:echo] Hello Ant 4
task myCompile << { BUILD SUCCESSFUL
ant.java(classname: 'com.my.classname',
classpath:
${sourceSets.main.runtimeClasspath.asPath}")
}
23. Gradle calls Ant
<!-- build.xml --> $ gradle antHello
<project> :antHello
<target name="antHello"> [ant:echo] Hello, from Ant.
<echo>Hello, from
Ant.</echo> BUILD SUCCESSFUL
</target>
</project>
// build.gradle
ant.importBuild 'build.xml'
24. Gradle adds behaviour to Ant task
<!-- build.xml --> $ gradle callAnt
<project> :callAnt
<target name="callAnt"> [ant:echo] Hello, from Ant.
<echo>Hello, from Gradle adds behaviour to Ant task
Ant.</echo>
</target> BUILD SUCCESSFUL
</project>
// build.gradle
ant.importBuild 'build.xml'
callAnt << {
println 'Gradle adds behaviour
to Ant task.'
}
25. Gradle with Maven
• Ant Ivy
• Gradle build on Ivy for dependency management
• Maven Repository
• Gradle works with any Maven repository
• Maven Project Structure
• By default Gradle uses Maven project structure
38. Multi Project Support
//settings.gradle - defines the subprojects {
project participates in the build repositories {mavenCentral()}
include 'api', 'services', 'web' dependencies {
compile "javax.servlet:servlet-
allprojects { api:2.5"
apply plugin: 'java' }
group = 'org.gradle.sample' task callHoldMyBro (dependsOn:
version = '1.0 ':elderBro:compileJava') {}
}
task omnipotenceTask {
println 'You find me in all the project(':war') {
project'
} apply plugin: 'java'
} dependencies {
compile "javax.servlet:servlet-
api:2.5", project(':api')
}
}
39. Gradle Project Templates
A Gradle plugin which provides templates, and template methods like 'initGroovyProject' to users. This
makes it easier to get up and running using Gradle as a build tool.
apply from: 'http://launchpad.net/gradle-templates/trunk/latest/+download/apply.groovy'
$ gradle createJavaProject
More Info: https://launchpad.net/gradle-templates
45. User Group Events
JUG-India
JUGChennai Java User Groups - India
Java User Groups - Chennai
Find your nearest JUG at
Main Website http://java.net/projects/jug-india
http://jugchennai.in
For JUG updates around india
Tweets: @jug_c
G Group: jug-c discussion@jug-india.java.net
May 5th
JUGChennai - Chennai - Stephen Chin – http://jugchennai.in/javafx
BOJUG – Bangalore - Simon Ritter, Chuk Munn Lee,Roger Brinkley and Terrence Barr
PuneJUG – Pune - Arun Gupta
November 2nd & 3rd
AIOUG Sangam '12 [Java Track] (Main Speaker as of now Arun Gupta)
Call for Paper is open - http://www.aioug.org/sangamspeakers.php