About
the
Talk • Prerequisites
• Basic
understanding
of
build
systems
• Some
exposure
to
Gradle
• Beginner
level
of
exposure
to
Groovy/Java
! • Takeaways
• Learn
a
trick
or
two
to
read
and
write
DSLs
• A
simpler
way
to
look
at
Gradle
Scripts 2
Introduction
(About
me) • Love
writing
code
in
Groovy,
Java,
Ruby,
Scala
and
JavaScript
(CoffeeScript).
• Work
at
ThoughtWorks
• Co-‐organizer
of
Pune
Java
User
Group
• Projects
hosted
on
Github
• (want
to
get
better
at)
running,
playing
drums
and
writing
book 3
Domain
Specific
Language • “A
Domain
Specific
Language
(DSL)
is
a
programming
language
designed
specifically
to
express
solutions
to
problems
in
a
specific
domain”
! ! ! ! ! • http://c2.com/cgi/wiki?DomainSpecificLanguage
• http://en.wikipedia.org/wiki/Domain-‐specific_language 5
Gradle
intro • an
advanced
build
system
• follows
conventions
• yet
very
flexible
• Groovy
build
scripts
• rich
domain
model
for
build
• …
And
much
more 11
Follows
Conventions • Create
a
build.gradle
file
in
project
root
:
!
apply
plugin:
'java'
! • A
valid
minimal
Java
project
build
file
• It
can
build
a
Jar
of
project
• Uses
conventional
src/main/java
for
source
files 12
Reuses
Existing
Infrastructure • Find
dependencies
in
MavenCentral
!
repositories
{
mavenCentral()
}
! • And
use
them
in
your
project
!
dependencies
{
compile
'org.slf4j:slf4j-‐api:1.7.9'
testCompile
'junit:junit:4.10'
}
14
And
the
credit
goes
to… Project void apply(Map options)! Configures this project using plugins or scripts. The following options are available: !•! from: A script to apply to the project. Accepts any path supported by Project.uri(). !•! plugin: The id or implementation class of the plugin to apply to the project. !•! to: The target delegate object or objects. Use this to configure objects other than the project. For more detail, see ObjectConfigurationAction. ! • http://www.gradle.org/docs/current/dsl/ org.gradle.api.Project.html#org.gradle.api.Project:apply(java.util.Map) 27
Project
API • Central
API
to
the
Gradle
build
script
• Available
as
‘project’
reference
in
build
script
• Build
Script
is
evaluated
against
the
instance
of
this
project
object.
• Available
implicitly
in
the
script
top
level*
• methods/properties
are
looked
up
on
this
object
by
default.
• One
project
per
build.gradle 29
Project
Properties • Setting
project
properties
like
:
!
group
=
'org.example'
version
=
'0.1'
description
=
'An
awesome
groovy
library'
! • Find
properties
declared
on
Project
API.
• Make
sure
they
are
not
read-‐only. 31
Project
Properties • Getters
and
Setters
on
project
object.
getBuildDir()/setBuildDir(Object
path)
• Can
be
set
using
groovy
property
notation
buildDir
=
“…”
! • Extra
properties
• can
be
declared
using
ext
namespace.
ext.myProp
=
“abc”
• it
becomes
property
of
project
println
project.myProp 32
Where
do
methods
come
from
? ! • Methods
defined
on
Project
itself
• Find
those
in
Project
API
Documentation
! • Methods
declared
in
the
build
file
• def
myMethod
… 33
Where
do
methods
come
from
? • Extension/Conventions
added
by
plugins
• Plugins
can
add
more
methods
to
project
• like
sourceSets
is
added
by
java
plugin
! • Each
task
becomes
a
method
on
project
• Each
task
name
becomes
a
method
! ! taskA { description = “some task” }! • so
that
task
can
be
easily
referenced
in
a
script
! •
Methods
of
parent
project 34
Script
Blocks • Special
methods
• Takes
in
a
Closure
as
parameter
• Delegates
to
a
special
Handler
whose
methods
are
available
in
the
block’s
context
! ! ! ! ! http://www.gradle.org/docs/current/dsl/org.gradle.api.Project.html#N1486F
37
Script
Blocks
-‐
Another
Example ! dependencies
{
…
}
! • Delegates
to
DependencyHandler
• Executes
the
closure
in
against
the
DependenciesHandler
• methods
like
compile,
testCompile
! ! http://www.gradle.org/docs/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html 39