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?

DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright Andrei Alexandrescu
 
Ts archiving
Ts   archivingTs   archiving
Ts archivingConfiz
 
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 GamesRay Toal
 
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 DispatchMatteo Battaglio
 
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 DAndrei Alexandrescu
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS MultithreadingRicha Jain
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2Chris Farrell
 
20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting Started20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting StartedTeerawat 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

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

Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingSchalk Cronjé
 
10 Cool Facts about Gradle
10 Cool Facts about Gradle10 Cool Facts about Gradle
10 Cool Facts about GradleEvgeny Goldin
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS MeetupLINAGORA
 
Gradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionGradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionSchalk Cronjé
 
Improving your Gradle builds
Improving your Gradle buildsImproving your Gradle builds
Improving your Gradle buildsPeter Ledbrook
 
Idiomatic gradle plugin writing
Idiomatic gradle plugin writingIdiomatic gradle plugin writing
Idiomatic gradle plugin writingSchalk Cronjé
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdfSudhanshiBakre1
 
Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Tino Isnich
 
Basic Gradle Plugin Writing
Basic Gradle Plugin WritingBasic Gradle Plugin Writing
Basic Gradle Plugin WritingSchalk Cronjé
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingSchalk Cronjé
 
Anatomy of a Gradle plugin
Anatomy of a Gradle pluginAnatomy of a Gradle plugin
Anatomy of a Gradle pluginDmytro Zaitsev
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingSchalk Cronjé
 
Gradle in a Polyglot World
Gradle in a Polyglot WorldGradle in a Polyglot World
Gradle in a Polyglot WorldSchalk Cronjé
 
Explore the Rake Gem
Explore the Rake GemExplore the Rake Gem
Explore the Rake GemRudy R. Yazdi
 
Little Did He Know ...
Little Did He Know ...Little Did He Know ...
Little Did He Know ...Burt Beckwith
 

Ä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

[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...itnewsafrica
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sectoritnewsafrica
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 

Kürzlich hochgeladen (20)

[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 

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' }