Gradle - Cheat Sheet - header image

Gradle - Cheat Sheet

Last updated on November 23, 2023 -
Star me on GitHub →  

I keep forgetting the Gradle commands I'm using on a daily basis - hence, this cheat sheet. Maybe you find it useful.

(Note: The cheat-sheet generally shows Linux/macOS specific commands. For Windows, replace e.g. ./gradlew build with just gradlew build, as the "." will lead to errors)

Running your build with a specific Java version

In addition to fumbling around with your JAVA_HOME env variable, do this:

  1. gradle.properties in your project directory

    // Note: Even on Windows, forward slashes only!!!
    
    org.gradle.java.home=/path_to_jdk_directory
  2. gradle.properties in the .gradle directory in your HOME_DIRECTORY

    // Note: Even on Windows, forward slashes only!!!
    
    org.gradle.java.home=/path_to_jdk_directory

Build a submodule

Instead of doing this:

cd myproject
cd mysubmodule
../gradlew.bat build

do:

cd myproject
./gradlew build -p mysubmodule

// or

./gradlew :mysubmodule:build

Build + Skipping Tests

Not -DskipTests ;)

./gradlew build -x test

Build Spring Boot Docker Image

./gradlew bootBuildImage --imageName=myorg/myapp

Upgrade Gradle Wrapper

// ./gradlew wrapper --gradle-version {version}

./gradlew wrapper --gradle-version 8.4

If that fails, e.g. the new Gradle version is incompatible with your project or your JDK version you will be stuck in an endless loop, because you won’t be able to run the wrapper anymore.

You can then do a "hard-reset" by changing the Gradle wrapper version in this file:

yourprojectgradle\wrapper\gradle-wrapper.properties

## change the version number in the distributionUrl
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-bin.zip

Gradle Concepts

Gradle Pre-7 <→ Gradle >= 7 mapping

  • compile <→ api (public, i.e. will become a transitive dependency for other modules) and implementation (private, other modules won’t "see" the dependency)

  • testCompile <→ testImplementation

IntelliJ IDEA

Always refresh.

Never forget to click that button, after changing your build.gradle(.kts) file.

New to Gradle? Want a refresher?

You might enjoy this:

Gradle Configuration Snippets

Gradle + Spring Boot: Custom Dependency Versions

See section Version Properties here: https://docs.spring.io/spring-boot/docs/current/reference/html/dependency-versions.html or all possible versions to override.

// in your build.gradle file
ext['jetty.version'] = '11.0.14'

More.

…​soon.

Your help is needed

Please add your own suggestions with a PR and I’ll happily merge them.

There's more where that came from

I'll send you an update when I publish new guides. Absolutely no spam, ever. Unsubscribe anytime.


Share

Comments

let mut author = ?

I'm @MarcoBehler and I share everything I know about making awesome software through my guides, screencasts, talks and courses.

Follow me on Twitter to find out what I'm currently working on.