Gradle is a general-purpose build automation tool. It combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build. Its powered by Groovy DSL. Presentation discusses what and why Gradle with demo for java, groovy, web, multi-project and grails projects.
2. Agenda
• What
and
Why?
• Key
features
of
Gradle
• Project
and
tasks
• Using
java/groovy/war/jetty
plugins
• Manage
dependency
• Working
with
multi-‐module
project
• Create
grails
application
using
Gradle
2
3. What
is
Gradle?
• A
general-‐purpose
build
automation
tool.
It
can
automate
•
building
• testing
• deployment
• publishing
• generate
static
websites
• generate
documentation
• indeed
anything
else
• Designed
to
take
advantage
of
convention
over
configuration.
• Combines
the
power
and
flexibility
of
Ant
with
the
dependency
management
and
conventions
of
Maven
into
a
more
effective
way
to
build.
3
4. Why
Gradle?
• Powerful
and
expressive
Groovy
DSL
instead
of
XML.
• Adaptable
to
nonconventional
project
structures.
• Works
well
for
java
as
well
as
non
java
projects.
• Extensible
(plugins!).
• Multi
project
builds
are
hassle
free.
• Powerful
dependency
management.
• Reduced
start
up
cost
using
Gradle
daemon.
• Caching
of
task
input
output.
• Introduced
to
Android
with
Android
Studio.
• Grails
3.0
build
system
will
be
updated
to
use
Gradle.
5. Combines
best
features
from
other
build
tools.
Image
source
:
http://www.drdobbs.com/jvm/why-‐build-‐your-‐java-‐projects-‐with-‐gradle/240168608
6. Features
Image
source
:
http://www.drdobbs.com/jvm/why-‐build-‐your-‐java-‐projects-‐with-‐gradle/240168608
8. Installation
Using
gvm
$
gvm
install
gradle
$
gradle
-‐v
$
gradle
init
$
gradle
tasks
Access
gradle
documentation
from
local
file
system
{USER_HOME}/.gvm/gradle/current/docs/userguide/userguide.pdf
9. Gradle
Build
Script
• build.gradle
file
in
project
root
directory.
• Its
actually
a
groovy
script.
• This
is
where
you
define
gradle
tasks.
• Every
Gradle
build
is
made
up
of
one
or
more
projects.
9
10. Gradle
project
• It
does
not
necessarily
represent
a
thing
to
be
built.
• It
might
represent
a
• library
jar
• web-‐app
• distribution
zip
assembled
from
the
JARs
produced
by
other
projects
• thing
to
be
done
:
deploy
to
staging
server
10
11. Gradle
Tasks
• An
atomic
piece
of
work
that
a
build
performs.
• compile
classes
• create
jar
• generate
java
doc
• publish
some
archive
to
a
repository
• Each
project
is
made
up
of
one
or
more
tasks
11
12. build.gradle
task
hello
<<
{
Defines
task
'hello'
with
groovy
closure.
println
"Hello
!!!!"
}
task
greet
<<{
println
"Welocome
Mr.
Kumar"
}
task
intro(dependsOn:
hello)
<<
{
println
"I'm
Gradle"
Defines
task
'intro'
which
depends
on
'hello'
task
appends
additional
groovy
closure
to
be
executed
to
existing
'hello'
task
}
hello
<<
{
println
"Hello
extended!!!!"
}
greet.dependsOn
hello,
intro
//
gradle
tasks
:list
all
the
available
tasks
//
gradle
intro
:executes
intro
task
//
gradle
-‐q
greet
:bare
build
output
//
gradle
-‐-‐daemon
hello
:subsequent
execution
will
be
fast
gradle
tasks
are
groovy
objects.
You
can
change
dependency
of
existing
tasks
13. Using
plugins
• Gradle
core
intentionally
provides
very
little
for
automation.
• All
of
the
useful
features
are
added
by
plugins.
e.g.
java
compile.
• Gradle
plugins
• add
new
tasks
(e.g.
JavaCompile)
• add
domain
objects
(e.g.
SourceSet)
• add
conventions
(e.g.
Java
source
is
located
at
src/main/java)
• extends
core
objects
and
objects
from
other
plugins
• Plugin
portal
:
http://plugins.gradle.org/
13
14. Applying
plugins
14
Script
plugins
//from
a
script
on
the
local
filesystem
or
at
a
remote
location.
apply
from:
'other.gradle'
Binary
plugins
//
applying
a
binary
plugin
by
type
apply
plugin:
'java'
Using
plugins
DSL
//version
is
optional
plugins
{
id
"com.jfrog.bintray"
version
"0.4.1"
}
20. Working
with
groovy
plugin
• Groovy
plugin
extends
the
Java
plugin
to
add
support
for
Groovy
projects.
• It
can
deal
with
Groovy
code,
mixed
Groovy
and
Java
code,
and
even
pure
Java
code.
• New/modified
tasks
• compileGroovy,
groovyDoc,
classes
etc
• Project
layout
• src/main/groovy
• src/test/groovy
• You
must
add
dependency
for
groovy
• compile
localGroovy()
• compile
'org.codehaus.groovy:groovy-‐all:2.3.6'
22. Using
war
and
jetty
plugin
apply
plugin:
"war"
//gradle
assemble
//apply
plugin:
"jetty"
//gradle
jettyRun
version
=
"1.0.0"
archivesBaseName
=
"multi-‐web"
repositories
{
mavenCentral()
}
dependencies
{
compile
project(":utils")
compile
"javax.servlet:servlet-‐api:2.5"
}
|-‐-‐
build.gradle
`-‐-‐
src
`-‐-‐
main
|-‐-‐
java
|
`-‐-‐
com
|
`-‐-‐
intelligrape
|
`-‐-‐
web
|
`-‐-‐
HomeServlet.java
`-‐-‐
webapp
|-‐-‐
WEB-‐INF
|
`-‐-‐
web.xml
`-‐-‐
index.jsp
23. Working
with
multi-‐project
build.gradle 'hello'
task
is
added
to
root
as
well
as
all
the
sub
projects
allprojects
{
task
hello
<<
{
task
-‐>
println
"I'm
$task.project.name"
}
}
subprojects
{
Applied
to
only
sub
projects
task
onlySubProjectTask
<<
{
task
-‐>
println
"I'm
$task.project.name"
}
apply
plugin:
"java"
repositories
{
mavenCentral()
}
java
plugin
applied
to
all
subproj
Applied
to
only
root
project
}
task
onlyMainProjectTask
<<
{
task
-‐>
println
"Only
Main
Project
Task
:
${task.project.name}
"
}
multi-‐project/
|-‐-‐
build.gradle
|-‐-‐
settings.gradle
|-‐-‐
top
|
`-‐-‐
build.gradle
`-‐-‐
util
`-‐-‐
build.gradle
settings.gradle
rootProject.name
=
'multi-‐project'
include
'util',
'top'
24. Working
with
multi-‐project
cont...
Executing
'hello'
task
:
executed
for
all
projects
$
gradle
hello
:hello
I'm
multi-‐project
:top:hello
I'm
top
:util:hello
I'm
util
Executing
'onlySubProjectTask'
task
:
executed
for
only
sub-‐projects
$
gradle
onlySubProjectTask
:top:onlySubProjectTask
I'm
top
:util:onlySubProjectTask
I'm
util
Execute
hello
task
of
'top'
sub-‐project
only
$
gradle
:top:hello
:top:hello
I'm
top
25. Using
grails-‐gradle
plugin
buildscript
{
repositories
{
jcenter()
}
dependencies
{classpath
"org.grails:grails-‐gradle-‐plugin:2.0.0"}
}
version
"0.1"
group
"example"
apply
plugin:
"grails"
grails
{
grailsVersion
=
'2.3.6'
springLoadedVersion
'1.1.3'
groovyVersion
=
'2.3.6'
}
repositories
{
//maven
repo
for
the
Grails
Central
repository
(Core
libraries
and
plugins)
grails.central()
}
dependencies
{
//
No
container
is
deployed
by
default,
so
add
this
bootstrap
"org.grails.plugins:tomcat:7.0.50"
//
Just
an
example
of
adding
a
Grails
plugin
compile
'org.grails.plugins:resources:1.2'
}
26. Using
grails-‐gradle
plugin
continued
• Create
build.gradle
file
and
copy
content
from
previous
slide.
• Execute
'gradle
init'
which
will
create
file
structure
• To
run
app
execute:
• gradle
gradle-‐run-‐app
• The
plugin
configures
a
Gradle
Task
Rule
to
allow
execution
of
any
available
Grails
scripts
as
Gradle
tasks.
Simply
prefix
the
Grails
script
name
with
grails-‐
and
execute
from
Gradle.
• e.g.
'gradle
grails-‐refresh-‐dependencies'
• Passing
arguments
to
Grails
Tasks
:
• gradle
-‐PgrailsArgs="FooController"
grails-‐create-‐controller
• Setting
the
Grails
Environment
:
• gradle
-‐PgrailsEnv=prod
grails-‐run-‐app
26