Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Power Up your Gradle

Power Up your Gradle

Does Gradle make you sad? Are slow & flaky builds stealing your time? Are you a Maven maven, longing for the days you could write incomprehensible build files in XML rather than Groovy? If so, this session is for you! Together, we will delve into some techniques to keep your Gradle builds simple and maintainable.

Nayan Hajratwala

August 10, 2021
Tweet

More Decks by Nayan Hajratwala

Other Decks in Technology

Transcript

  1. A Quick Smackdown • In the Red Corner, a Maven

    POM f ile: • https://raw.githubusercontent.com/metasfresh/ metasfresh-parent-legacy/2018-19_grave_noyce/ pom.xml • In the Blue Corner, a Gradle Build f ile: • https://raw.githubusercontent.com/asciidoctor/ asciidoctorj/main/build.gradle
  2. Dependency Declarations dependencies { implementation group: ”org.springframework", name: ”spring -

    core", version: ”2.5" implementation(“org.springframework:spring - core:2.5”) implementation(‘org.springframework:spring - core:2.5’) implementation “org.springframework:spring - core:2.5” implementation ‘org.springframework:spring - core:2.5’ }
  3. Dependency Declarations dependencies { implementation(group = org.springframework", name = ”spring

    - core", version = ”2.5”) implementation(“org.springframework:spring - core:2.5”) } • Typesafe / Better refactoring & content assist support • build.gradle -> build.gradle.kts
  4. Plugins Out with the old… apply plugin: "com.github.ben - manes.versions"

    buildscript { repositories { gradlePluginPortal() } dependencies { classpath “com.github.ben - manes:gradle - versions - plugin:0.39.0” } } In with the new… plugins { id "com.github.ben - manes.versions" version “0.39.0” }
  5. Always use a wrapper • Install the gradle wrapper in

    your project gradle wrapper - - gradle - version=7.7.1 • Ensures everyone using your project uses the same version
  6. Reduce the friction • https://github.com/gdubw/gng brew tap gdubw/gng & &

    brew install gng • Anywhere in your project hierarchy, you can just do: gw clean build
  7. JVM - living in the past… • Be Honest, which

    JVM are you using? • Java 8 was released in 2014 • Current LTS version is 11 • Latest version is 16 • Next LTS version is 17 - available Sept 14, 2021 • Java Toolchains - https://docs.gradle.org/current/userguide/toolchains.html val jvmVersion: JavaLanguageVersion = JavaLanguageVersion.of(16) java { toolchain { languageVersion.set(jvmVersion) } }
  8. Gradle • Each Gradle release comes with usability & performance

    improvements, so upgrade & check release notes - https://gradle.org/releases/ • Increment one version at a time, f ixing warnings & deprecations as you go.
  9. Versions Plugin • https://github.com/ben-manes/gradle-versions-plugin gw dependencyUpdates # depUp anyone? •

    Consider Dependabot style auto updates • Protip: If you’re way behind, incrementally upgrade to slowly eliminate warnings & keep it working. 1.3.1 - > 1.3.9 - > 1.4.0 - > 1.4.5 - > 2.0.0 - > 2.0.1
  10. Build Cache • Saves time by reusing outputs produced by

    other build • https://docs.gradle.org/current/userguide/build_cache.html • In gradle.properties org.gradle.caching=true
  11. Test Performance • Identify & f ix slow tests •

    Ensure tests are independent • Parallelize tasks.withType<Test>().conf i gureEach { useJUnitPlatform() reports.html.required.set(false) maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1 } • https://docs.gradle.org/current/userguide/performance.html#parallel_test_execution
  12. Build Scan • Get an overview of your build, performance

    metrics, etc. • https://scans.gradle.com/ gw build - - scan # or whatever tasks you want • Lots more tips here: • https://docs.gradle.org/current/userguide/performance.html