Demystifying Gradle Build Scripts
Kunal Dabir
slido.com #GRADLE
About me
-
VP Eng.
-
Author
-
Speaker
-
Open Source
-
github.com/kdabir
What do we Build?
-
Libraries
-
Web Apps
-
Micro Services
-
Mobile Apps
-
Native (Desktop) Apps
-
CLI Apps
What do we Build?
Artifacts
What do we write?
-
Main Source Code
-
Resources / Assets
-
Configuration
-
Tests
What do we have?
Source sets
What do we depend on?
Other Artifacts, like libraries, often which are not built by us
What do we depend on?
Dependencies
Where do we get dependencies from?
Repositories
(Package Registries)
Repositories are typically remote servers, access controlled for publishing
How Are Libraries Stored in Repositories?
Repository Format
Different Repositories have their own formats like Ivy, Maven, NPM etc.
Which Are Well Known Repositories
-
Maven Central
-
NPM
-
RubyGems
-
Github Package Registry
-
Corporate Nexus /Artifactory
Who downloads dependencies for us?
Apps - Native/CLI :
brew, apt-get, yum
Libraries:
NPM/Yarn, Bundler/Gems, PIP, Maven/Gradle
Who downloads dependencies for us?
Package Managers
What do we do with built Artifact?
-
Deploy it (to servers)
-
Publish it (as library)
-
Publish it (to app stores)
Where do we publish?
Repositories
Internal or Public
What does a Build Tool do?
Compiles source code
Runs automated tests
Manages dependencies
Runs application locally
What does a Build Tool do?
-
Runs static analysis of code
-
Checks code coverage
-
Produces reports
-
Runs database migrations
-
Web assets optimizations
What does a Build Tool do?
-
Packages distributable binaries
-
Publishes artifacts
-
Generates (API) Documentation
-
Tags releases in VCS
-
Deploys application to servers
What does a Build Tool do?
-
And many more tasks...
Everyone Reinvented the wheel...
... in their favorite language
Evolution
Bash Scripts
Make
Ant
Maven
Gradle
Why not write build scripts in just any scripting language?
because they don't speak the language of the domain (terms used in build)
Why not use IDE as build tool?
-
How will it work of CI?
-
Honor other teammate's preferences
-
Running from CLI
Why not just a task runner? (ant, grunt, make, rake)
because you have to repeat yourself in every project you build
Why not Maven?
-
Maven did many good things in its time
-
Introduced Dependency Management
-
Brough Conventions
-
-
But
-
XML is verbose
-
Customization is cumbersome
-
-
...
Why not Maven?
Gradle
What makes Gradle better
-
Rich DSL
-
Excellent dependency resolution
-
Out of the box plugins (sensible defaults)
-
Fully configurable / extensible
-
Flexible syntax
-
Performant
-
Flexible Multi Project support
What makes Gradle better
-
Vibrant community & ecosystem
-
Plethora of Plugins
-
Open source
-
backed by a company
-
Good adoption
Imperative v/s Declarative
-
Imperative Mixes 'how' with 'what'
-
provide steps for execution and ordering.
-
Looks easy at start, hard to maintain and update
What makes Gradle better
Gradle embraces domain modelling as a core tenet. Focusing on the domain model as opposed to the execution model (like prior generation build tools such as Apache Ant) has many advantages. A strong domain model communicates the intent (i.e. the what) over the mechanics (i.e. the how). This allows humans to understand builds at a level that is meaningful to them.
Domain Model of a Build
Project
Task
Domain Model of a Build
Project object contains collections of Task Objects and many other methods
Domain Model of a Build
Each Task is an Object on which we can call methods to configure it's behaviour
Project API
-
Central API to the Gradle build script
-
Available as ‘project’ reference in the build script
-
BuildScript is evaluated against the instance of this project object.
-
-
Available implicitly in the script
-
methods/properties are looked up on this object by default.
-
-
One project object per build.gradle
Script blocks
Create a task
task hello
hello.doLast {
println "I am a task"
}
task hello {
doLast {
println "I am a task"
}
}
Or
Task ordering
task bye {
doLast {
println "I am another task"
}
}
bye.dependsOn(hello)
Plugins
plugins {
id 'java'
}
plugins {
id "org.jetbrains.kotlin.jvm" version "1.6.21"
}
Repositories
repositories {
mavenCentral()
}
Dependencies
dependencies {
compile 'org.springframework:spring-core:4.0.6.RELEASE'
testCompile 'junit:junit:4.12'
}
Running
gradle help
Gradle wrapper
gradle wrapper
./gradlew myTask
Hands On Time
Questions?
slido.com #GRADLE
Demystifying Gradle Build Scripts
By Kunal Dabir
Demystifying Gradle Build Scripts
Gradle is an incredibly flexible build tool but a lot of times developers are taken aback by syntax of Gradle Build Scripts. Lot of Kotlin and Java Developers use Gradle without understanding the underpinning basics of gradle builds. This talk will focus on how to understand Gradle Build files and adhere to its best practises. This talk will provide the lenses to identify the syntactic elements of the Gradle DSL.
- 84