SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Gradle
Small introduction
Required knowledge
●
●
●
●
●

average java
average groovy (properties, closures, collections: lists, maps)
general knowledge of build scripts (ant, maven etc)
mad search skillz (google)
desire to learn
Groovy Lang - quick intro
●
●
●
●

lists: [1, 2, 3, 4]
maps: ['key1': 'value1', 'key2': 'value2'] --> can be used as parameters to
methods
when calling methods the parens are optional
properties: instead of using getters and setters directly you can just use the
name of the property as if it were a "normal" variable (without the "set" and
"get" prefix)
The Gradle DSL
●
●
●

a Domain Specific Language
a language designed for a specific need (such as building projects)
uses groovy
The Project
●
●
●

the most important object in the Gradle DSL
everything you need is inside the "project" variable
if you don't specify an object all the method calls are delegated to the
"project" variable
Simple example: fileTree
●
●

1.
2.
3.
4.

used for example when you want to load all the files in a directory (the "lib"
directory which contains all the jars)
TMTOWTDI: there's more than one way to do it

fileTree('lib')
fileTree('lib').include('**/*.java').exclude('**/*.cpp')
fileTree(dir: 'lib', include: '**/*.java', exclude: '**/*.cpp')
fileTree('lib') {
include '**/*.java'
exclude '**/*.cpp'
}
Implementation of fileTree
First example: fileTree('lib')
From source code: ConfigurableFileTree fileTree(Object baseDir);
The baseDir is actually a string.
Implementation of fileTree
Second example: fileTree('lib').include('**/*.java').exclude('**/*.cpp')
From source code: ConfigurableFileTree fileTree(Object baseDir);
Gradle uses here something called: fluent interface
(read more here: http://www.martinfowler.com/bliki/FluentInterface.html).
Basically this means you can chain method calls.
Probably you are wondering where are these include and exclude methods
from, hmm? Well, they are from a super interface of ConfigurableFileTree:
org.gradle.api.tasks.util.PatternFilterable.
Implementation of fileTree
Third example: fileTree(dir: 'lib', include: '**/*.java', exclude: '**/*.cpp')
From source code: ConfigurableFileTree fileTree(Map<String, ?> args);
Here, each of "dir", "include" and "exclude" are actually the keys of a map.
Behind the scenes the fileTree method will configure a ConfigurableFileTree
object.
Implementation of fileTree
Fourth example: fileTree('lib') {
include '**/*.java'
exclude '**/*.cpp'
}

From source code: ConfigurableFileTree fileTree(Object baseDir, Closure configureClosure);
There is yet another way to configure the fileTree method: using a closure.
The Simplest Gradle Script
apply plugin: "java"
This assumes that you respect the standard maven project structure:
src
|____ main
|
|____ java // here go all the source files ...
|
|
|____ test
|____ java // ... and here go all the test classes
The Simplest Gradle Script
Why does this simple script work?
Gradle:
● uses a thing called "Convention over Configuration",
● provides a number of tasks to ease the creation of a new project:
○ :compileJava
○ :processResources
○ :classes
○ :jar
○ :assemble
○ :compileTestJava
○ :processTestResources
○ :testClasses
○ :test
○ :check
○ :build
Custom tasks
task --> groovy compiler plugin:
Standard way to define a new task:

task myTaskName() {
// doing some stuff...
}
This is actually a more convenient way for the following:

task("myTaskName")

Source (StackOverflow): How to interpret Gradle DSL
Custom tasks
Gradle provides some predefined tasks which you can configure.
Example: Copy, Zip, Compile, Jar, War
task myCopyTask(type: Copy) {
from 'src/main/webapp'
into 'build/explodedWar'
}
Notice that the task receives a named parameter (a map with one entry
actually) called type.
This is similar to inheriting from a base class in the sense that now, in the
closure (the stuff between curly brackets { // stuff }) you have access to the
from and into methods. These are specific to the copy task.
doLast notation
Sometimes you need to add something to an existing task.
For example to add some stuff to the task defined in the previous slide we
would do:
task myCopyTask << {
// do something
}

This is equivalent to the following:
task myCopyTask.doLast {
// do something
}
The doLast notation is pretty common in build scripts so you'd better remember
it :)
Order must be mantained
Sometimes you need to specify that a task should run only after other tasks are
run first. For this you need to use dependsOn.

task someOtherTask() {
// do some other stuff
}
task myTask(dependsOn: someOtherTask) {
// do stuff
}
Gradle Plugins
These add new capabilities to gradle (obviously).
Here's how to add a plugin:

apply plugin: "java"
The plugins basically:
● Add tasks to the project (example compile, test)
● Preconfigure tasks with defaults
● Add dependency configurations to the project
● Add new properties and methods to existing type via extensions

There are plugins for many things such as static code analysis, new languages
(groovy), various IDE's (eclipse, idea).
The Wrapper
Gradle provides a way to build a project even without having gradle installed,
although you need to have at least the JDK installed.
Here is how to define the wrapper task:

task wrapper(type: Wrapper) {
gradleVersion = '1.3'
}
And here's how you call it from the command line:

gradle wrapper
You need to run this each time you update the gradle version variable.
Gradle will generate some files in the project's root directory: gradlew.bat
gradlew and the gradle directory.
The Wrapper
The recommended way to build the project after you generate the wrapper
files is using the gradlew.bat script which will take the same parameters as
the "normal" gradle:

gradlew build
Just be carefull to call gradlew in the project root directory
(since that's were the script is located).
Adding libraries
First: you must define a repository that tells gradle where to take the libraries
from.
Here's how to do it:

repositories {
mavenCentral()
}
mavenCentral() is the usual repository you should use.
Second: you must specify the libraries you want to use. For example, if you
want junit you need to search on MvnRepository the actual string to include:

dependencies {
testCompile 'junit:junit:+'
}
The complete script
apply plugin: 'java'
apply plugin: 'eclipse'
defaultTasks 'build', 'eclipse'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:+'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.3'
}
Thanks...

Thanks for attention
and

Enjoy!

Weitere ähnliche Inhalte

Was ist angesagt?

Ts archiving
Ts   archivingTs   archiving
Ts archiving
Confiz
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based Games
Ray Toal
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS Multithreading
Richa Jain
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2
Chris Farrell
 
20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting Started20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting Started
Teerawat Issariyakul
 

Was ist angesagt? (20)

DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright
 
Ts archiving
Ts   archivingTs   archiving
Ts archiving
 
Node.js extensions in C++
Node.js extensions in C++Node.js extensions in C++
Node.js extensions in C++
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based Games
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
 
Playing the toStrings
Playing the toStringsPlaying the toStrings
Playing the toStrings
 
DaNode - A home made web server in D
DaNode - A home made web server in DDaNode - A home made web server in D
DaNode - A home made web server in D
 
Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS Multithreading
 
GCD and OperationQueue.
GCD and OperationQueue.GCD and OperationQueue.
GCD and OperationQueue.
 
NS2 Classifiers
NS2 ClassifiersNS2 Classifiers
NS2 Classifiers
 
Collections forceawakens
Collections forceawakensCollections forceawakens
Collections forceawakens
 
packet destruction in NS2
packet destruction in NS2packet destruction in NS2
packet destruction in NS2
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2
 
Testing con spock
Testing con spockTesting con spock
Testing con spock
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
 
NS2 Shadow Object Construction
NS2 Shadow Object ConstructionNS2 Shadow Object Construction
NS2 Shadow Object Construction
 
NS2 Object Construction
NS2 Object ConstructionNS2 Object Construction
NS2 Object Construction
 
20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting Started20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting Started
 

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 build
Igor Khotin
 

Andere mochten auch (8)

Gradle 101
Gradle 101Gradle 101
Gradle 101
 
Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation Tool
 
Gradle : An introduction
Gradle : An introduction Gradle : An introduction
Gradle : An introduction
 
Gradle - time for a new build
Gradle - time for a new buildGradle - time for a new build
Gradle - time for a new build
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
 
Gradle
GradleGradle
Gradle
 
Gradle
GradleGradle
Gradle
 
Gradle by Example
Gradle by ExampleGradle by Example
Gradle by Example
 

Ähnlich wie Gradle - small introduction

10 Cool Facts about Gradle
10 Cool Facts about Gradle10 Cool Facts about Gradle
10 Cool Facts about Gradle
Evgeny Goldin
 
Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01
Tino Isnich
 

Ähnlich wie Gradle - small introduction (20)

Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin Writing
 
10 Cool Facts about Gradle
10 Cool Facts about Gradle10 Cool Facts about Gradle
10 Cool Facts about Gradle
 
Android presentation - Gradle ++
Android presentation - Gradle ++Android presentation - Gradle ++
Android presentation - Gradle ++
 
GradleFX
GradleFXGradleFX
GradleFX
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
Gradle in 45min
Gradle in 45minGradle in 45min
Gradle in 45min
 
Gradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionGradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 version
 
Improving your Gradle builds
Improving your Gradle buildsImproving your Gradle builds
Improving your Gradle builds
 
Idiomatic gradle plugin writing
Idiomatic gradle plugin writingIdiomatic gradle plugin writing
Idiomatic gradle plugin writing
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
 
Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01
 
Basic Gradle Plugin Writing
Basic Gradle Plugin WritingBasic Gradle Plugin Writing
Basic Gradle Plugin Writing
 
OpenCms Days 2012 - Developing OpenCms with Gradle
OpenCms Days 2012 - Developing OpenCms with GradleOpenCms Days 2012 - Developing OpenCms with Gradle
OpenCms Days 2012 - Developing OpenCms with Gradle
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin Writing
 
Anatomy of a Gradle plugin
Anatomy of a Gradle pluginAnatomy of a Gradle plugin
Anatomy of a Gradle plugin
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin Writing
 
Gradle in a Polyglot World
Gradle in a Polyglot WorldGradle in a Polyglot World
Gradle in a Polyglot World
 
Explore the Rake Gem
Explore the Rake GemExplore the Rake Gem
Explore the Rake Gem
 
Little Did He Know ...
Little Did He Know ...Little Did He Know ...
Little Did He Know ...
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Gradle - small introduction

  • 2. Required knowledge ● ● ● ● ● average java average groovy (properties, closures, collections: lists, maps) general knowledge of build scripts (ant, maven etc) mad search skillz (google) desire to learn
  • 3. Groovy Lang - quick intro ● ● ● ● lists: [1, 2, 3, 4] maps: ['key1': 'value1', 'key2': 'value2'] --> can be used as parameters to methods when calling methods the parens are optional properties: instead of using getters and setters directly you can just use the name of the property as if it were a "normal" variable (without the "set" and "get" prefix)
  • 4. The Gradle DSL ● ● ● a Domain Specific Language a language designed for a specific need (such as building projects) uses groovy
  • 5. The Project ● ● ● the most important object in the Gradle DSL everything you need is inside the "project" variable if you don't specify an object all the method calls are delegated to the "project" variable
  • 6. Simple example: fileTree ● ● 1. 2. 3. 4. used for example when you want to load all the files in a directory (the "lib" directory which contains all the jars) TMTOWTDI: there's more than one way to do it fileTree('lib') fileTree('lib').include('**/*.java').exclude('**/*.cpp') fileTree(dir: 'lib', include: '**/*.java', exclude: '**/*.cpp') fileTree('lib') { include '**/*.java' exclude '**/*.cpp' }
  • 7. Implementation of fileTree First example: fileTree('lib') From source code: ConfigurableFileTree fileTree(Object baseDir); The baseDir is actually a string.
  • 8. Implementation of fileTree Second example: fileTree('lib').include('**/*.java').exclude('**/*.cpp') From source code: ConfigurableFileTree fileTree(Object baseDir); Gradle uses here something called: fluent interface (read more here: http://www.martinfowler.com/bliki/FluentInterface.html). Basically this means you can chain method calls. Probably you are wondering where are these include and exclude methods from, hmm? Well, they are from a super interface of ConfigurableFileTree: org.gradle.api.tasks.util.PatternFilterable.
  • 9. Implementation of fileTree Third example: fileTree(dir: 'lib', include: '**/*.java', exclude: '**/*.cpp') From source code: ConfigurableFileTree fileTree(Map<String, ?> args); Here, each of "dir", "include" and "exclude" are actually the keys of a map. Behind the scenes the fileTree method will configure a ConfigurableFileTree object.
  • 10. Implementation of fileTree Fourth example: fileTree('lib') { include '**/*.java' exclude '**/*.cpp' } From source code: ConfigurableFileTree fileTree(Object baseDir, Closure configureClosure); There is yet another way to configure the fileTree method: using a closure.
  • 11. The Simplest Gradle Script apply plugin: "java" This assumes that you respect the standard maven project structure: src |____ main | |____ java // here go all the source files ... | | |____ test |____ java // ... and here go all the test classes
  • 12. The Simplest Gradle Script Why does this simple script work? Gradle: ● uses a thing called "Convention over Configuration", ● provides a number of tasks to ease the creation of a new project: ○ :compileJava ○ :processResources ○ :classes ○ :jar ○ :assemble ○ :compileTestJava ○ :processTestResources ○ :testClasses ○ :test ○ :check ○ :build
  • 13. Custom tasks task --> groovy compiler plugin: Standard way to define a new task: task myTaskName() { // doing some stuff... } This is actually a more convenient way for the following: task("myTaskName") Source (StackOverflow): How to interpret Gradle DSL
  • 14. Custom tasks Gradle provides some predefined tasks which you can configure. Example: Copy, Zip, Compile, Jar, War task myCopyTask(type: Copy) { from 'src/main/webapp' into 'build/explodedWar' } Notice that the task receives a named parameter (a map with one entry actually) called type. This is similar to inheriting from a base class in the sense that now, in the closure (the stuff between curly brackets { // stuff }) you have access to the from and into methods. These are specific to the copy task.
  • 15. doLast notation Sometimes you need to add something to an existing task. For example to add some stuff to the task defined in the previous slide we would do: task myCopyTask << { // do something } This is equivalent to the following: task myCopyTask.doLast { // do something } The doLast notation is pretty common in build scripts so you'd better remember it :)
  • 16. Order must be mantained Sometimes you need to specify that a task should run only after other tasks are run first. For this you need to use dependsOn. task someOtherTask() { // do some other stuff } task myTask(dependsOn: someOtherTask) { // do stuff }
  • 17. Gradle Plugins These add new capabilities to gradle (obviously). Here's how to add a plugin: apply plugin: "java" The plugins basically: ● Add tasks to the project (example compile, test) ● Preconfigure tasks with defaults ● Add dependency configurations to the project ● Add new properties and methods to existing type via extensions There are plugins for many things such as static code analysis, new languages (groovy), various IDE's (eclipse, idea).
  • 18. The Wrapper Gradle provides a way to build a project even without having gradle installed, although you need to have at least the JDK installed. Here is how to define the wrapper task: task wrapper(type: Wrapper) { gradleVersion = '1.3' } And here's how you call it from the command line: gradle wrapper You need to run this each time you update the gradle version variable. Gradle will generate some files in the project's root directory: gradlew.bat gradlew and the gradle directory.
  • 19. The Wrapper The recommended way to build the project after you generate the wrapper files is using the gradlew.bat script which will take the same parameters as the "normal" gradle: gradlew build Just be carefull to call gradlew in the project root directory (since that's were the script is located).
  • 20. Adding libraries First: you must define a repository that tells gradle where to take the libraries from. Here's how to do it: repositories { mavenCentral() } mavenCentral() is the usual repository you should use. Second: you must specify the libraries you want to use. For example, if you want junit you need to search on MvnRepository the actual string to include: dependencies { testCompile 'junit:junit:+' }
  • 21. The complete script apply plugin: 'java' apply plugin: 'eclipse' defaultTasks 'build', 'eclipse' repositories { mavenCentral() } dependencies { testCompile 'junit:junit:+' } task wrapper(type: Wrapper) { gradleVersion = '1.3' }