gradle bu ilds cu stom izing and refac toring€¦ · anatomy of a gradle projec t $ gradle init...
TRANSCRIPT
![Page 1: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/1.jpg)
Customizing and Refactoring Gradle Builds
Marc Philipp, Gradle Inc.
![Page 2: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/2.jpg)
Marc PhilippSo�ware Engineer at Gradle, Inc.
JUnit 5 team lead
Twi�er: Web:
@marcphilippmarcphilipp.de
![Page 3: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/3.jpg)
What is Gradle?
![Page 4: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/4.jpg)
What is Gradle?
⬢
⬢
⬢
⬢
based on the Java Virtual Machine (JVM)implemented in Javafocused on flexibility and performance100% open‑source (Apache 2.0) and free
Gradle is an open‑source buildautoma�on tool
![Page 5: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/5.jpg)
Versa�le⬢
⬢
⬢
⬢
Java ecosystem: Java, Groovy, Kotlin, Scala, …Official build tool for AndroidNa�ve projects: C, C++, Swi�, …And more: Go, Asciidoctor, …
![Page 6: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/6.jpg)
Gradle Inc.⬢
⬢
⬢
⬡
⬡
⬢
Vision: Build HappinessMission: Accelerate Developer Produc�vityProducts:Gradle Build ToolGradle Enterprise
more than 60 employees including over 40 engineers
![Page 7: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/7.jpg)
Agenda⬢
⬢
⬡
⬡
⬡
Basic conceptsFrom Quick & Dirty to Safe & Sounddependency managementcustom taskscustom configura�on
![Page 8: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/8.jpg)
✋Show of Hands
![Page 9: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/9.jpg)
Basic Concepts
![Page 10: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/10.jpg)
Tasks⬢
⬢
⬢
a Gradle build executes taskstasks can depend on other taskstasks have inputs and outputs
![Page 11: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/11.jpg)
Hello Worldtasks.register("helloWorld") { // in build.gradle doLast { println("Hello World!") } }
$ gradle helloWorld > Task :helloWorld Hello World! BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed
![Page 12: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/12.jpg)
Build ScriptsA Gradle project is configured in build scripts:
⬢
⬢
settings.gradle[.kts]: configures the subprojectsthat comprise the buildbuild.gradle[.kts]: configures the used pluginsand tasks
![Page 13: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/13.jpg)
se�ngs.gradle[.kts]rootProject.name = "new-project" include("subproject-a") include("subproject-b")
![Page 14: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/14.jpg)
build.gradle[.kts]plugins { java // to compile Java sources application // to generate startup scripts } repositories { jcenter() // to resolve dependencies } dependencies { implementation("com.google.guava:guava:28.0-jre") testImplementation("org.junit.jupiter:junit-jupiter:5.5.2") } application { // extension of the 'application' plugin mainClassName = "com.example.App" }
![Page 15: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/15.jpg)
Groovy vs. Kotlin DSL⬢
⬢
⬡
⬡
⬢
build scripts use a Domain Specific Language (DSL)ini�ally Gradle only supported Groovydynamically typedlimited IDE support
Kotlin DSL is stable since Gradle 5.0
Build scripts should be declara�ve –complex logic does not belong here.
![Page 16: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/16.jpg)
Gradle Wrapper./gradlew <tasks> instead of gradle <tasks>
⬢
⬢
⬢
⬢
execute builds with prior installa�on of Gradledownloads required versioncaches already downloaded versions locallyeveryone uses the same version
![Page 17: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/17.jpg)
Anatomy of a Gradle project$ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example \ --project-name=new-project BUILD SUCCESSFUL in 0s 2 actionable tasks: 2 executed
├── build.gradle.kts // build script ├── gradle/wrapper // wrapper jar and configuration ├── gradlew // wrapper script for Linux/macOS ├── gradlew.bat // wrapper script for Windows ├── settings.gradle.kts // settings script └── src // Java source tree ├── main │ ├── java │ └── resources └── test ├── java └── resources
![Page 18: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/18.jpg)
Incremental Builds⬢
⬡
⬡
⬡
⬢
only execute tasks that are affected by changes inbetween two subsequent buildsinputs have changedoutputs are present and unchangedtask implementa�on has changed (e.g. differentplugin version)
keep outputs of all tasks that are up‑to‑date
![Page 19: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/19.jpg)
First Build$ ./gradlew --console=plain build > Task :compileJava > Task :processResources NO-SOURCE > Task :classes > Task :jar [...] > Task :compileTestJava > Task :testClasses > Task :test > Task :check > Task :build BUILD SUCCESSFUL in 5s 7 actionable tasks: 7 executed
![Page 20: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/20.jpg)
Subsequent Build$ ./gradlew --console=plain build > Task :compileJava UP-TO-DATE > Task :processResources NO-SOURCE > Task :classes UP-TO-DATE > Task :jar UP-TO-DATE [...] > Task :compileTestJava UP-TO-DATE > Task :testClasses UP-TO-DATE > Task :test UP-TO-DATE > Task :check UP-TO-DATE > Task :build UP-TO-DATE BUILD SUCCESSFUL in 0s 7 actionable tasks: 7 up-to-date
![Page 21: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/21.jpg)
Build Scans⬢
⬢
⬢
Accelerate debugging of build problemsPrivate but shareable linkFree to use on
›
scans.gradle.com$ ./gradlew build --scan BUILD SUCCESSFUL in 1s 7 actionable tasks: 5 executed, 2 up-to-date Publishing build scan... https://gradle.com/s/lu7dxy7quyoju
h�ps://gradle.com/s/lu7dxy7quyoju
![Page 22: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/22.jpg)
Build Cache⬢
⬢
allows reusing task outputs of any previous buildlocal and remote cache
$ git pull [...] 185 files changed, 4320 insertions(+), 1755 deletions(-)
$ ./gradlew --build-cache sanityCheck BUILD SUCCESSFUL in 1m 11s 1338 actionable tasks: 238 executed, 1100 from cache
![Page 23: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/23.jpg)
Dependency Management
![Page 24: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/24.jpg)
Demo
![Page 25: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/25.jpg)
Recap⬢
⬢
⬢
⬢
Don’t duplicate dependency versionPrefer api or implementation over compileUse buildSrc to collect dependency versionsUse a java-platform plugin to streamlinedependency management
![Page 26: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/26.jpg)
More on Dependency ManagementFree webinars:
⬢
⬢
h�ps://gradle.com/blog/dependency‑management‑fundamentals/h�ps://gradle.com/blog/dependency‑management‑part‑2‑handling‑conflicts/
![Page 27: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/27.jpg)
Custom Tasks
![Page 28: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/28.jpg)
Demo
![Page 29: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/29.jpg)
Recap⬢
⬢
⬢
Don’t define complex tasks directly in the build scriptDefine them in the buildSrc projectAllows for tes�ng and reuse in subprojects
![Page 30: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/30.jpg)
Custom Configura�on
![Page 31: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/31.jpg)
Demo
![Page 32: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/32.jpg)
Recap⬢
⬢
⬢
Extract custom logic into separate build scriptsEven be�er: Extract your custom logic into a pre‑compiled script plugin in buildSrcNext step: Move it to a separate plugin to use it inindependent projects
![Page 33: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/33.jpg)
Summary
![Page 34: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/34.jpg)
Summary⬢
⬢
Keep your build scripts declara�veUse buildSrc to share logic
![Page 35: Gradle Bu ilds Cu stom izing and Refac toring€¦ · Anatomy of a Gradle projec t $ gradle init --dsl=kotlin --type=java-application \ --test-framework=junit --package=com.example](https://reader034.vdocuments.net/reader034/viewer/2022051808/6009e1fe61123455064c93a4/html5/thumbnails/35.jpg)
Links⬢
⬢
Demo code:
My talks on Gradle and JUnit: h�ps://github.com/marcphilipp/gradle‑refactorings
h�ps://www.marcphilipp.de/en/talks/