SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
An Introduction to Gradle for Java
Developers
Kostas Saidis
www.niovity.com
Java Hellenic User Group Meetup
April 2, 2016
About Me
Kostas Saidis
saiko@niovity.com
Software & Data
Architect
Twitter:
@saikos
Linkedin:
http://gr.linkedin.com/in/saiko
Academia
BSc @ cs.unipi.gr (2001)
MSc @ di.uoa.gr (2004)
PhD @ di.uoa.gr (2011)
Industry
Freelance consultant, developer &
instructor since 1999
Enterpreneur since 2011 (niovity)
Java
Early Java enthusiast (1997)
Groovyist since 2011
Using Gradle
Building software with Gradle
Since version 0.9
Fifteen real-life projects with team sizes ranging from 2 to 10
members
Custom plugin authoring
Target audience
Java developers
with little or no experience
in Gradle or another build tool.
Build tools
www.ayorkshirehome.com
Software build tools
Tools for managing and automating
the software build process.
Software what?
Building
software
sucks!
(big time)
Software build process
Develop
Test
Assemble
Deploy
Integrate
Repeat
Again and again and again
What is Gradle?
Gradle is one of the finest software
build tools out there
Makes the software build process easier to setup, extend and maintain
In this presentation
We focus on understanding Gradle
Gradle is a superb piece of software
What makes it so great?
No flame wars
No feature comparison with other tools
At a glance
Gradle
Open source: Apache License v2
First release 2009, latest release March 2016 (2.12)
Backed by Gradleware... ooops, I mean Gradle Inc
Used by: Android, Spring IO, Linkedin, Netflix, Twitter and more...
At a glance
Gradle
Combines the best of Ant, Ivy and Maven
Cross-platform file management
Dependency management
Conventions
In a smart and extensible way
Deep API
Groovy DSL
Easy-to-write plugins
Ultimately offering
Clean and elegant builds
Plenty of new features
Better build management
Features
Non exhaustive list
Full-fledged programmability
Both declarative and imperative
Convention over configuration
Transitive dependency management
Multi-project builds
Polyglot (not only Java)
Numerous plugins and integrations
Incremental and parallel execution
Reporting and analytics
Embeddable
Great documentation
The most distinct features
IMHO
1. Its superb design and technical sophistication.
2. The exposure and manipulation of its “internal” Java APIs
through Groovy (aka the Groovy DSL).
A simple example
A simple Java project
1 apply plugin: ”java”
2 group = ”org.foo.something”
3 version = ”1.0−SNAPSHOT”
4 repositories {
5 mavenCentral()
6 }
7 dependencies {
8 compile ”commons−io:commons−io:2.4”
9 testCompile ”junit:junit:4.11”
10 runtime files(”lib/foo.jar”, ”lib/bar.jar”)
11 }
The contents of build.gradle (aka the build script), placed in the root folder of the
project.
Core Concepts
Build script: a build configuration script supporting one or more
projects.
Project: a component that needs to be built. It is made up of
one or more tasks.
Task: a distinct step required to perform the build. Each
task/step is atomic (either succeeds or fails).
Publication: the artifact produced by the build process.
Dependency Resolution
Dependencies: tasks and projects depending on each other (internal)
or on third-party artifacts (external).
Transitive dependencies: the dependencies of a project may
themselves have dependencies.
Repositories: the “places” that hold external dependencies
(Maven/Ivy repos, local folders).
DAG: the directed acyclic graph of dependencies (what
depends on what).
Dependency configurations : named sets (groups) of dependencies
(e.g. per task).
Plugins
A plugin applies a set of extensions to the build process.
Add taks to a project.
Pre-configure these tasks with reasonable defaults.
Add dependency configurations.
Add new properties and methods to existing objects.
Plugins implement the “build-by-convention” principle in a flexible
way.
The Build Lifecycle
1. Initialization: initialization of the project.
2. Configuration: configuration of the project (computes the DAG).
3. Execution: executes the sequence of build tasks.
The simple example
1 apply plugin: ”java”
2 //Introduces a set of Maven−style conventions
3 //(tasks, source locations, dependency configurations, etc)
4 group = ”org.foo.something”
5 version = ”1.0−SNAPSHOT”
6 repositories {
7 //resolve all external dependencies via Maven central
8 mavenCentral()
9 }
10 dependencies {
11 //each name (compile, testCompile, etc) refers to
12 //a configuration introduced by the java plugin
13 compile ”commons−io:commons−io:2.4”
14 testCompile ”junit:junit:4.11”
15 runtime files(”lib/foo.jar”, ”lib/bar.jar”)
16 }
Invoking gradle
Run a build task
> gradle test
Compiles the sources and runs the tests
> gradle tasks
clean, assemble, build, classes, testClasses, test, jar, etc
Never invoke gradle directly but always use the gradle wrapper: a
mechanism to execute gradle without manually installing it
beforehand (not covered here).
A more complex example
Ant integration, custom tasks
1 apply plugin:’java’
2 task generateFiles(type: JavaExec) {
3 main = ’some.class.name’
4 classpath = sourceSets.main.runtimeClasspath
5 args = [ projectDir, ’path/to/gen/files’ ]
6 }
7 test {
8 dependsOn generateFiles
9 doLast {
10 ant.copy(toDir:’build/test−classes’){
11 fileset dir:’path/to/gen/files’
12 }
13 }
14 }
15 import org.apache.commons.io.FileUtils
16 clean.doFirst {
17 FileUtils.deleteQuietly(new File(’path/to/gen/files’))
18 }
Hmmm
What’s going on here?
Let’s have a closer look
1 //apply the java plugin as before
2 apply plugin:’java’
3 //introduce a new task (that invokes a java process)
4 task generateFiles(type: JavaExec) {
5 main = ’some.class.name’
6 classpath = sourceSets.main.runtimeClasspath
7 args = [ projectDir, ’path/to/gen/files’ ]
8 }
9 //customize the test task
10 test {
11 dependsOn generateFiles
12 doLast {
13 ant.copy(toDir:’build/test−classes’){
14 fileset dir:’path/to/gen/files’
15 }
16 }
17 }
18 //import a class required by this build script
19 //some necessary classpath definitions are not shown for brevity
20 import org.apache.commons.io.FileUtils
21 //customize the clean task
22 clean.doFirst {
23 FileUtils.deleteQuietly(new File(’path/to/gen/files’))
24 }
Hmmm
What’s that strange syntax?
It’s Groovy!
groovy-lang.org
The Gradle DSL
Gradle’s power lies in its Groovy-based DSL
What you need to know about Groovy to start using Gradle in its full
potential
For more Groovy magic, have a look at:
An Introduction to Groovy for Java Developers
What is Groovy?
Groovy is a feature-rich, Java-friendly language for the JVM
A super version of Java
Augments Java with additional features
Designed as a companion language for Java
seamless integration with Java → an additional jar at runtime!
syntactically aligned with Java → syntactically correct Java will
work in Groovy (with some gotchas)!
compiles into JVM bytecode and preserves Java semantics → call
Groovy from Java == call Java from Java!
the 2nd language targeting the Java platform (JSR-241) →
Java was the first!
Groovy Scripts
Java
1 public class Booh {
2 public static void main(String[] args) {
3 System.out.println(”Booh booh”);
4 }
5 }
Compile and run it.
Groovy
1 println(”Booh booh”)
Run it directy.
POJOs vs POGOs
Java
1 public class Foo {
2 private String message;
3 public void setMessage(String message) {
4 this.message = message;
5 }
6 public String getMessage() {
7 return message;
8 }
9 }
Groovy
1 class Foo {
2 String message
3 }
Groovy Map Syntax
Java
1 Map<String, String> pairs = new HashMap();
2 pairs.put(”one”, ”1”);
3 pairs.put(”two”, ”2”);
Groovy
1 Map<String, String> pairs = [one:’1’, two:”2”]
Groovy Operator Overloading
1 import java.util.concurrent.*
2 class Tasks {
3 List<Callable> taskList = []
4 void execute() {
5 for(Callable t: taskList) {
6 t.run()
7 }
8 }
9 void leftShift(Tasks tasks) {
10 taskList.addAll(tasks.taskList)
11 }
12 }
13 Tasks t1 = new Tasks()
14 ...
15 Tasks t2 = new Tasks()
16 ...
17 t1 << t2
Groovy Closures
A closure is an anonymous function together with a referencing
environment.
may accept parameters or return a value,
can be assigned to variables,
can be passed as argument,
captures the variables of its surrounding lexical scope.
Example
1 class Test {
2 long x = 3
3 //a block of code assigned to a variable
4 Closure<Long> times = { long l −> l * x }
5 //x is the free variable
6 }
7 Test test = new Test()
8 assert test.times(3) == 9
9 test.x = 4
10 assert test.times(3) == 12
11 class Test2 {
12 long x = 0
13 }
14 test.times.resolveStrategy = Closure.DELEGATE_FIRST
15 test.times.delegate = new Test2()
16 assert test.times(3) == 0
Groovy method invocation syntax
1 //omit parentheses
2 println(”booh booh”)
3 println ”booh booh”
4 method ”one”, 2
5 //omit map’s braces
6 method([one:1, two: 2])
7 method(one:1, two:2)
8 method one:1, two:2
9 //The closure as last argument pattern
10 Closure clos = { int x, int y −> x + y }
11 method(”one”, 2, clos)
12 method ”one”, 2, clos
13 method(”one”, 2) { int x, int y −>
14 x + y
15 }
Back to Gradle
Behind the scenes
Gradle build scripts are Groovy scripts.
Such Groovy scripts operate in the context of Gradle’s domain
objects (DSL objects).
The main DSL object is a org.gradle.api.Project instance.
There is one-to-one relationship between a Projet object and a
build.gradle file.
Each DSL object exposes its own properties and methods.
Have a look at: Gradle DSL Reference
Putting it all together
Epiphany
1 //a groovy script with a Project object as the context
2 apply plugin: ”java”
3 //invocation of Project.apply(Map) method
4 group = ”org.foo.something”
5 version = ”1.0−SNAPSHOT”
6 //update of project.group, project.version properties
7 repositories {
8 mavenCentral()
9 }
10 //invocation of the Project.repositories(Closure) method
11 //From the docs: the closure has a RepositoryHandler object
12 //as its delegate
13 dependencies {
14 compile ”commons−io:commons−io:2.4”
15 testCompile ”junit:junit:4.11”
16 runtime files(”lib/foo.jar”, ”lib/bar.jar”)
17 }
18 //here?
Quiz
And here?
1 apply plugin:’java’
2 task generateFiles(type: JavaExec) {
3 main = ’some.class.name’
4 classpath = sourceSets.main.runtimeClasspath
5 args = [ projectDir, ’path/to/gen/files’ ]
6 }
7 test {
8 dependsOn generateFiles
9 doLast {
10 ant.copy(toDir:’build/test−classes’){
11 fileset dir:’path/to/gen/files’
12 }
13 }
14 }
15 import org.apache.commons.io.FileUtils
16 clean.doFirst {
17 FileUtils.deleteQuietly(new File(’path/to/gen/files’))
18 }
Build lifecycle
Configuration vs Execution Phase
1 test {//closure runs at configuration phase
2 dependsOn generateFiles
3 doLast { //at execution phase
4 ant.copy(toDir:’build/test−classes’){
5 fileset dir:’path/to/gen/files’
6 }
7 }
8 }
9 test.doLast { //at execution phase
10 ant.copy...
11 }
12 task test << { //at execution phase
13 ant.copy...
14 }
15 test { //at configuration phase
16 ant.copy...
17 }
18 task test { //at configuration phase
19 ant.copy...
20 }
Thank you
Questions?
Proudly powered by:
LATEX
Using the Beamer class &
the Wronki theme (slightly modified).

Weitere ähnliche Inhalte

Was ist angesagt?

Docker Registry V2
Docker Registry V2Docker Registry V2
Docker Registry V2Docker, Inc.
 
Continuous Integration/Deployment with Gitlab CI
Continuous Integration/Deployment with Gitlab CIContinuous Integration/Deployment with Gitlab CI
Continuous Integration/Deployment with Gitlab CIDavid Hahn
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introductionRasheed Waraich
 
Introducing GitLab (September 2018)
Introducing GitLab (September 2018)Introducing GitLab (September 2018)
Introducing GitLab (September 2018)Noa Harel
 
Accelerated Linux Core Dump Analysis training public slides
Accelerated Linux Core Dump Analysis training public slidesAccelerated Linux Core Dump Analysis training public slides
Accelerated Linux Core Dump Analysis training public slidesDmitry Vostokov
 
Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An IntroductionBehzad Altaf
 
Github - Git Training Slides: Foundations
Github - Git Training Slides: FoundationsGithub - Git Training Slides: Foundations
Github - Git Training Slides: FoundationsLee Hanxue
 
Intro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps WorkshopIntro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps WorkshopWeaveworks
 
Gitlab ci, cncf.sk
Gitlab ci, cncf.skGitlab ci, cncf.sk
Gitlab ci, cncf.skJuraj Hantak
 
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...GITS Indonesia
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean ArchitectureBadoo
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKJosé Paumard
 
Gitops: the kubernetes way
Gitops: the kubernetes wayGitops: the kubernetes way
Gitops: the kubernetes waysparkfabrik
 

Was ist angesagt? (20)

Docker Registry V2
Docker Registry V2Docker Registry V2
Docker Registry V2
 
Building with Gradle
Building with GradleBuilding with Gradle
Building with Gradle
 
Continuous Integration/Deployment with Gitlab CI
Continuous Integration/Deployment with Gitlab CIContinuous Integration/Deployment with Gitlab CI
Continuous Integration/Deployment with Gitlab CI
 
Jenkins tutorial
Jenkins tutorialJenkins tutorial
Jenkins tutorial
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Introducing GitLab (September 2018)
Introducing GitLab (September 2018)Introducing GitLab (September 2018)
Introducing GitLab (September 2018)
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
CI/CD with Github Actions
CI/CD with Github ActionsCI/CD with Github Actions
CI/CD with Github Actions
 
Gitlab ci-cd
Gitlab ci-cdGitlab ci-cd
Gitlab ci-cd
 
Gitops Hands On
Gitops Hands OnGitops Hands On
Gitops Hands On
 
Accelerated Linux Core Dump Analysis training public slides
Accelerated Linux Core Dump Analysis training public slidesAccelerated Linux Core Dump Analysis training public slides
Accelerated Linux Core Dump Analysis training public slides
 
Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An Introduction
 
Github - Git Training Slides: Foundations
Github - Git Training Slides: FoundationsGithub - Git Training Slides: Foundations
Github - Git Training Slides: Foundations
 
Intro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps WorkshopIntro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps Workshop
 
Gitlab ci, cncf.sk
Gitlab ci, cncf.skGitlab ci, cncf.sk
Gitlab ci, cncf.sk
 
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
 
Apache Maven
Apache MavenApache Maven
Apache Maven
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UK
 
Gitops: the kubernetes way
Gitops: the kubernetes wayGitops: the kubernetes way
Gitops: the kubernetes way
 

Andere mochten auch

Gradle - time for a new build
Gradle - time for a new buildGradle - time for a new build
Gradle - time for a new buildIgor Khotin
 
Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!Corneil du Plessis
 
Javaone - Gradle: Harder, Better, Stronger, Faster
Javaone - Gradle: Harder, Better, Stronger, Faster Javaone - Gradle: Harder, Better, Stronger, Faster
Javaone - Gradle: Harder, Better, Stronger, Faster Andres Almiray
 
Gradle enabled android project
Gradle enabled android projectGradle enabled android project
Gradle enabled android projectShaka Huang
 
Gradle 2.Breaking stereotypes
Gradle 2.Breaking stereotypesGradle 2.Breaking stereotypes
Gradle 2.Breaking stereotypesStrannik_2013
 
Gradle 2.Write once, builde everywhere
Gradle 2.Write once, builde everywhereGradle 2.Write once, builde everywhere
Gradle 2.Write once, builde everywhereStrannik_2013
 
Top 10 reasons to migrate to Gradle
Top 10 reasons to migrate to GradleTop 10 reasons to migrate to Gradle
Top 10 reasons to migrate to GradleStrannik_2013
 
Git.From thorns to the stars
Git.From thorns to the starsGit.From thorns to the stars
Git.From thorns to the starsStrannik_2013
 
Android Gradle about using flavor
Android Gradle about using flavorAndroid Gradle about using flavor
Android Gradle about using flavorTed Liang
 
From Ant to Maven to Gradle a tale of CI tools for JVM
From Ant to Maven to Gradle a tale of CI tools for JVMFrom Ant to Maven to Gradle a tale of CI tools for JVM
From Ant to Maven to Gradle a tale of CI tools for JVMBucharest Java User Group
 
4. arteria carotida externa
4. arteria carotida externa4. arteria carotida externa
4. arteria carotida externaanatogral
 
Predicting the Future as a Service with Azure ML and R
Predicting the Future as a Service with Azure ML and R Predicting the Future as a Service with Azure ML and R
Predicting the Future as a Service with Azure ML and R Barbara Fusinska
 
남동구 라선거구 최승원
남동구 라선거구 최승원남동구 라선거구 최승원
남동구 라선거구 최승원승원 최
 
5. arteria carotida int.
5. arteria carotida int.5. arteria carotida int.
5. arteria carotida int.anatogral
 

Andere mochten auch (20)

Gradle by Example
Gradle by ExampleGradle by Example
Gradle by Example
 
Gradle
GradleGradle
Gradle
 
Gradle - time for a new build
Gradle - time for a new buildGradle - time for a new build
Gradle - time for a new build
 
Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!
 
Javaone - Gradle: Harder, Better, Stronger, Faster
Javaone - Gradle: Harder, Better, Stronger, Faster Javaone - Gradle: Harder, Better, Stronger, Faster
Javaone - Gradle: Harder, Better, Stronger, Faster
 
Gradle enabled android project
Gradle enabled android projectGradle enabled android project
Gradle enabled android project
 
Gradle presentation
Gradle presentationGradle presentation
Gradle presentation
 
Building with Gradle
Building with GradleBuilding with Gradle
Building with Gradle
 
Gradle 2.Breaking stereotypes
Gradle 2.Breaking stereotypesGradle 2.Breaking stereotypes
Gradle 2.Breaking stereotypes
 
Gradle 2.Write once, builde everywhere
Gradle 2.Write once, builde everywhereGradle 2.Write once, builde everywhere
Gradle 2.Write once, builde everywhere
 
Top 10 reasons to migrate to Gradle
Top 10 reasons to migrate to GradleTop 10 reasons to migrate to Gradle
Top 10 reasons to migrate to Gradle
 
Git.From thorns to the stars
Git.From thorns to the starsGit.From thorns to the stars
Git.From thorns to the stars
 
Gradle
GradleGradle
Gradle
 
Android Gradle about using flavor
Android Gradle about using flavorAndroid Gradle about using flavor
Android Gradle about using flavor
 
From Ant to Maven to Gradle a tale of CI tools for JVM
From Ant to Maven to Gradle a tale of CI tools for JVMFrom Ant to Maven to Gradle a tale of CI tools for JVM
From Ant to Maven to Gradle a tale of CI tools for JVM
 
4. arteria carotida externa
4. arteria carotida externa4. arteria carotida externa
4. arteria carotida externa
 
Predicting the Future as a Service with Azure ML and R
Predicting the Future as a Service with Azure ML and R Predicting the Future as a Service with Azure ML and R
Predicting the Future as a Service with Azure ML and R
 
남동구 라선거구 최승원
남동구 라선거구 최승원남동구 라선거구 최승원
남동구 라선거구 최승원
 
5. arteria carotida int.
5. arteria carotida int.5. arteria carotida int.
5. arteria carotida int.
 
When the connection fails
When the connection failsWhen the connection fails
When the connection fails
 

Ähnlich wie An Introduction to Gradle for Java Developers

In the Brain of Hans Dockter: Gradle
In the Brain of Hans Dockter: GradleIn the Brain of Hans Dockter: Gradle
In the Brain of Hans Dockter: GradleSkills Matter
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneAndres Almiray
 
Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemKostas Saidis
 
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume LaforgeGR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume LaforgeGR8Conf
 
Using the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM DevelopmentUsing the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM DevelopmentSchalk Cronjé
 
Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Tino Isnich
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyJames Williams
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingAndres Almiray
 
10 Cool Facts about Gradle
10 Cool Facts about Gradle10 Cool Facts about Gradle
10 Cool Facts about GradleEvgeny Goldin
 
Gradle - Build system evolved
Gradle - Build system evolvedGradle - Build system evolved
Gradle - Build system evolvedBhagwat Kumar
 
淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合Kyle Lin
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 featuresshrinath97
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyAndres Almiray
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGuillaume Laforge
 
Gradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionGradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionSchalk Cronjé
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for JavaCharles Anderson
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebC4Media
 
Gradle in a Polyglot World
Gradle in a Polyglot WorldGradle in a Polyglot World
Gradle in a Polyglot WorldSchalk Cronjé
 

Ähnlich wie An Introduction to Gradle for Java Developers (20)

In the Brain of Hans Dockter: Gradle
In the Brain of Hans Dockter: GradleIn the Brain of Hans Dockter: Gradle
In the Brain of Hans Dockter: Gradle
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
 
Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystem
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume LaforgeGR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
 
Using the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM DevelopmentUsing the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM Development
 
Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 Groovytesting
 
10 Cool Facts about Gradle
10 Cool Facts about Gradle10 Cool Facts about Gradle
10 Cool Facts about Gradle
 
Gradle - Build system evolved
Gradle - Build system evolvedGradle - Build system evolved
Gradle - Build system evolved
 
淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 features
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with Groovy
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
 
Gradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionGradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 version
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 
Griffon Presentation
Griffon PresentationGriffon Presentation
Griffon Presentation
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and Geb
 
Gradle in a Polyglot World
Gradle in a Polyglot WorldGradle in a Polyglot World
Gradle in a Polyglot World
 

Kürzlich hochgeladen

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

An Introduction to Gradle for Java Developers

  • 1. An Introduction to Gradle for Java Developers Kostas Saidis www.niovity.com Java Hellenic User Group Meetup April 2, 2016
  • 2. About Me Kostas Saidis saiko@niovity.com Software & Data Architect Twitter: @saikos Linkedin: http://gr.linkedin.com/in/saiko Academia BSc @ cs.unipi.gr (2001) MSc @ di.uoa.gr (2004) PhD @ di.uoa.gr (2011) Industry Freelance consultant, developer & instructor since 1999 Enterpreneur since 2011 (niovity) Java Early Java enthusiast (1997) Groovyist since 2011
  • 3. Using Gradle Building software with Gradle Since version 0.9 Fifteen real-life projects with team sizes ranging from 2 to 10 members Custom plugin authoring
  • 4. Target audience Java developers with little or no experience in Gradle or another build tool.
  • 6. Software build tools Tools for managing and automating the software build process.
  • 7. Software what? Building software sucks! (big time) Software build process Develop Test Assemble Deploy Integrate Repeat Again and again and again
  • 8. What is Gradle? Gradle is one of the finest software build tools out there Makes the software build process easier to setup, extend and maintain
  • 9. In this presentation We focus on understanding Gradle Gradle is a superb piece of software What makes it so great? No flame wars No feature comparison with other tools
  • 10. At a glance Gradle Open source: Apache License v2 First release 2009, latest release March 2016 (2.12) Backed by Gradleware... ooops, I mean Gradle Inc Used by: Android, Spring IO, Linkedin, Netflix, Twitter and more...
  • 11. At a glance Gradle Combines the best of Ant, Ivy and Maven Cross-platform file management Dependency management Conventions In a smart and extensible way Deep API Groovy DSL Easy-to-write plugins Ultimately offering Clean and elegant builds Plenty of new features Better build management
  • 12. Features Non exhaustive list Full-fledged programmability Both declarative and imperative Convention over configuration Transitive dependency management Multi-project builds Polyglot (not only Java) Numerous plugins and integrations Incremental and parallel execution Reporting and analytics Embeddable Great documentation
  • 13. The most distinct features IMHO 1. Its superb design and technical sophistication. 2. The exposure and manipulation of its “internal” Java APIs through Groovy (aka the Groovy DSL).
  • 14. A simple example A simple Java project 1 apply plugin: ”java” 2 group = ”org.foo.something” 3 version = ”1.0−SNAPSHOT” 4 repositories { 5 mavenCentral() 6 } 7 dependencies { 8 compile ”commons−io:commons−io:2.4” 9 testCompile ”junit:junit:4.11” 10 runtime files(”lib/foo.jar”, ”lib/bar.jar”) 11 } The contents of build.gradle (aka the build script), placed in the root folder of the project.
  • 15. Core Concepts Build script: a build configuration script supporting one or more projects. Project: a component that needs to be built. It is made up of one or more tasks. Task: a distinct step required to perform the build. Each task/step is atomic (either succeeds or fails). Publication: the artifact produced by the build process.
  • 16. Dependency Resolution Dependencies: tasks and projects depending on each other (internal) or on third-party artifacts (external). Transitive dependencies: the dependencies of a project may themselves have dependencies. Repositories: the “places” that hold external dependencies (Maven/Ivy repos, local folders). DAG: the directed acyclic graph of dependencies (what depends on what). Dependency configurations : named sets (groups) of dependencies (e.g. per task).
  • 17. Plugins A plugin applies a set of extensions to the build process. Add taks to a project. Pre-configure these tasks with reasonable defaults. Add dependency configurations. Add new properties and methods to existing objects. Plugins implement the “build-by-convention” principle in a flexible way.
  • 18. The Build Lifecycle 1. Initialization: initialization of the project. 2. Configuration: configuration of the project (computes the DAG). 3. Execution: executes the sequence of build tasks.
  • 19. The simple example 1 apply plugin: ”java” 2 //Introduces a set of Maven−style conventions 3 //(tasks, source locations, dependency configurations, etc) 4 group = ”org.foo.something” 5 version = ”1.0−SNAPSHOT” 6 repositories { 7 //resolve all external dependencies via Maven central 8 mavenCentral() 9 } 10 dependencies { 11 //each name (compile, testCompile, etc) refers to 12 //a configuration introduced by the java plugin 13 compile ”commons−io:commons−io:2.4” 14 testCompile ”junit:junit:4.11” 15 runtime files(”lib/foo.jar”, ”lib/bar.jar”) 16 }
  • 20. Invoking gradle Run a build task > gradle test Compiles the sources and runs the tests > gradle tasks clean, assemble, build, classes, testClasses, test, jar, etc Never invoke gradle directly but always use the gradle wrapper: a mechanism to execute gradle without manually installing it beforehand (not covered here).
  • 21. A more complex example Ant integration, custom tasks 1 apply plugin:’java’ 2 task generateFiles(type: JavaExec) { 3 main = ’some.class.name’ 4 classpath = sourceSets.main.runtimeClasspath 5 args = [ projectDir, ’path/to/gen/files’ ] 6 } 7 test { 8 dependsOn generateFiles 9 doLast { 10 ant.copy(toDir:’build/test−classes’){ 11 fileset dir:’path/to/gen/files’ 12 } 13 } 14 } 15 import org.apache.commons.io.FileUtils 16 clean.doFirst { 17 FileUtils.deleteQuietly(new File(’path/to/gen/files’)) 18 }
  • 23. Let’s have a closer look 1 //apply the java plugin as before 2 apply plugin:’java’ 3 //introduce a new task (that invokes a java process) 4 task generateFiles(type: JavaExec) { 5 main = ’some.class.name’ 6 classpath = sourceSets.main.runtimeClasspath 7 args = [ projectDir, ’path/to/gen/files’ ] 8 } 9 //customize the test task 10 test { 11 dependsOn generateFiles 12 doLast { 13 ant.copy(toDir:’build/test−classes’){ 14 fileset dir:’path/to/gen/files’ 15 } 16 } 17 } 18 //import a class required by this build script 19 //some necessary classpath definitions are not shown for brevity 20 import org.apache.commons.io.FileUtils 21 //customize the clean task 22 clean.doFirst { 23 FileUtils.deleteQuietly(new File(’path/to/gen/files’)) 24 }
  • 26. The Gradle DSL Gradle’s power lies in its Groovy-based DSL What you need to know about Groovy to start using Gradle in its full potential For more Groovy magic, have a look at: An Introduction to Groovy for Java Developers
  • 27. What is Groovy? Groovy is a feature-rich, Java-friendly language for the JVM A super version of Java Augments Java with additional features Designed as a companion language for Java seamless integration with Java → an additional jar at runtime! syntactically aligned with Java → syntactically correct Java will work in Groovy (with some gotchas)! compiles into JVM bytecode and preserves Java semantics → call Groovy from Java == call Java from Java! the 2nd language targeting the Java platform (JSR-241) → Java was the first!
  • 28. Groovy Scripts Java 1 public class Booh { 2 public static void main(String[] args) { 3 System.out.println(”Booh booh”); 4 } 5 } Compile and run it. Groovy 1 println(”Booh booh”) Run it directy.
  • 29. POJOs vs POGOs Java 1 public class Foo { 2 private String message; 3 public void setMessage(String message) { 4 this.message = message; 5 } 6 public String getMessage() { 7 return message; 8 } 9 } Groovy 1 class Foo { 2 String message 3 }
  • 30. Groovy Map Syntax Java 1 Map<String, String> pairs = new HashMap(); 2 pairs.put(”one”, ”1”); 3 pairs.put(”two”, ”2”); Groovy 1 Map<String, String> pairs = [one:’1’, two:”2”]
  • 31. Groovy Operator Overloading 1 import java.util.concurrent.* 2 class Tasks { 3 List<Callable> taskList = [] 4 void execute() { 5 for(Callable t: taskList) { 6 t.run() 7 } 8 } 9 void leftShift(Tasks tasks) { 10 taskList.addAll(tasks.taskList) 11 } 12 } 13 Tasks t1 = new Tasks() 14 ... 15 Tasks t2 = new Tasks() 16 ... 17 t1 << t2
  • 32. Groovy Closures A closure is an anonymous function together with a referencing environment. may accept parameters or return a value, can be assigned to variables, can be passed as argument, captures the variables of its surrounding lexical scope.
  • 33. Example 1 class Test { 2 long x = 3 3 //a block of code assigned to a variable 4 Closure<Long> times = { long l −> l * x } 5 //x is the free variable 6 } 7 Test test = new Test() 8 assert test.times(3) == 9 9 test.x = 4 10 assert test.times(3) == 12 11 class Test2 { 12 long x = 0 13 } 14 test.times.resolveStrategy = Closure.DELEGATE_FIRST 15 test.times.delegate = new Test2() 16 assert test.times(3) == 0
  • 34. Groovy method invocation syntax 1 //omit parentheses 2 println(”booh booh”) 3 println ”booh booh” 4 method ”one”, 2 5 //omit map’s braces 6 method([one:1, two: 2]) 7 method(one:1, two:2) 8 method one:1, two:2 9 //The closure as last argument pattern 10 Closure clos = { int x, int y −> x + y } 11 method(”one”, 2, clos) 12 method ”one”, 2, clos 13 method(”one”, 2) { int x, int y −> 14 x + y 15 }
  • 35. Back to Gradle Behind the scenes Gradle build scripts are Groovy scripts. Such Groovy scripts operate in the context of Gradle’s domain objects (DSL objects). The main DSL object is a org.gradle.api.Project instance. There is one-to-one relationship between a Projet object and a build.gradle file. Each DSL object exposes its own properties and methods. Have a look at: Gradle DSL Reference
  • 36. Putting it all together Epiphany 1 //a groovy script with a Project object as the context 2 apply plugin: ”java” 3 //invocation of Project.apply(Map) method 4 group = ”org.foo.something” 5 version = ”1.0−SNAPSHOT” 6 //update of project.group, project.version properties 7 repositories { 8 mavenCentral() 9 } 10 //invocation of the Project.repositories(Closure) method 11 //From the docs: the closure has a RepositoryHandler object 12 //as its delegate 13 dependencies { 14 compile ”commons−io:commons−io:2.4” 15 testCompile ”junit:junit:4.11” 16 runtime files(”lib/foo.jar”, ”lib/bar.jar”) 17 } 18 //here?
  • 37. Quiz And here? 1 apply plugin:’java’ 2 task generateFiles(type: JavaExec) { 3 main = ’some.class.name’ 4 classpath = sourceSets.main.runtimeClasspath 5 args = [ projectDir, ’path/to/gen/files’ ] 6 } 7 test { 8 dependsOn generateFiles 9 doLast { 10 ant.copy(toDir:’build/test−classes’){ 11 fileset dir:’path/to/gen/files’ 12 } 13 } 14 } 15 import org.apache.commons.io.FileUtils 16 clean.doFirst { 17 FileUtils.deleteQuietly(new File(’path/to/gen/files’)) 18 }
  • 38. Build lifecycle Configuration vs Execution Phase 1 test {//closure runs at configuration phase 2 dependsOn generateFiles 3 doLast { //at execution phase 4 ant.copy(toDir:’build/test−classes’){ 5 fileset dir:’path/to/gen/files’ 6 } 7 } 8 } 9 test.doLast { //at execution phase 10 ant.copy... 11 } 12 task test << { //at execution phase 13 ant.copy... 14 } 15 test { //at configuration phase 16 ant.copy... 17 } 18 task test { //at configuration phase 19 ant.copy... 20 }
  • 39. Thank you Questions? Proudly powered by: LATEX Using the Beamer class & the Wronki theme (slightly modified).