Gradle
Gradle is an open-source build automation tool used primarily for Java, Kotlin, Android, and Groovy projects. It uses a Groovy or Kotlin DSL (Domain-Specific Language) to define build scripts (build.gradle or build.gradle.kts), making builds highly flexible and customizable compared to XML-based tools like Maven or Ant.
Core Concepts
| Concept | Description |
|---|---|
| Project | The top-level unit of work in Gradle (represented by build.gradle) |
| Task | A single unit of work (e.g., compile, test, package) |
| Plugin | Extends Gradle’s capabilities (e.g., java, application, android) |
| Wrapper | gradlew script that ensures everyone uses the same Gradle version |
| Build Cache | Stores task outputs to skip re-execution when inputs haven’t changed |
Gradle Commands
1. gradle build
Compiles source code, runs tests, and assembles the project output (e.g., .jar or .war). It is the most commonly used command in any Gradle project.
./gradlew build
2. gradle clean
Deletes the build/ directory, removing all previously compiled classes, resources, and packaged outputs — giving you a fresh slate for the next build.
./gradlew clean
3. gradle test
Executes all unit tests in the project and generates a test report under build/reports/tests/, making it easy to identify failing test cases.
./gradlew test
4. gradle run
Runs the application directly (requires the application plugin), useful for quickly launching your app without manually setting up classpaths.
./gradlew run
5. gradle tasks
Lists all available tasks in the project grouped by category (build, verification, help, etc.), helping you discover what actions are configured.
./gradlew tasks
6. gradle dependencies
Prints the full dependency tree for every configuration (e.g., implementation, testImplementation), which is invaluable for diagnosing version conflicts.
./gradlew dependencies
7. gradle assemble
Compiles and packages the code into a deployable artifact (e.g., .jar, .apk) without running tests, making it faster than a full build.
./gradlew assemble
8. gradle check
Runs all verification tasks — including unit tests, lint checks, and code analysis — without packaging the project, useful in CI pipelines for validation only.
./gradlew check
9. gradle clean build
Combines clean and build in one command — wipes previous outputs and performs a full rebuild from scratch, eliminating stale artifact issues.
./gradlew clean build
10. gradle wrapper
Generates or updates the Gradle Wrapper files (gradlew, gradlew.bat, gradle/wrapper/), so all team members use the exact same Gradle version without manual installation.
gradle wrapper --gradle-version 8.7
11. gradle properties
Displays all project properties, Gradle version, JVM settings, and resolved configuration values, useful for debugging environment-specific build issues.
./gradlew properties
12. gradle :module:taskName
Runs a specific task inside a particular submodule in a multi-project build, avoiding the overhead of running tasks in unrelated modules.
./gradlew :app:assembleDebug
13. gradle --info
Executes any task with verbose INFO-level logging enabled, showing detailed lifecycle events and task decisions to help trace what Gradle is doing step by step.
./gradlew build --info
14. gradle --debug
Enables full DEBUG-level logging for maximum verbosity, capturing every internal Gradle decision — typically used when --info isn’t enough to diagnose a problem.
./gradlew build --debug
15. gradle --scan
Uploads a detailed build scan to scans.gradle.com and returns a shareable URL with deep insights into performance, dependencies, and task timelines.
./gradlew build --scan
16. gradle --offline
Forces Gradle to use only cached dependencies without making any network requests, useful when working without internet access.
./gradlew build --offline
17. gradle --parallel
Enables parallel execution of independent tasks across subprojects, significantly speeding up builds in large multi-module projects.
./gradlew build --parallel
18. gradle --refresh-dependencies
Forces Gradle to re-download all dependencies from remote repositories, bypassing the local cache — helpful when a dependency update isn’t being picked up.
./gradlew build --refresh-dependencies
19. gradle -x <taskName>
Excludes a specific task from execution during the build, for example skipping tests with -x test to get a faster build during development.
./gradlew build -x test
20. gradle help --task <taskName>
Shows detailed documentation for a specific task — including its inputs, outputs, type, and which project it belongs to.
./gradlew help --task build
Install Java & Gradle
Install OpenJDK 17 and Gradle 8.14 via SDKMAN on Ubuntu/Debian:
sudo apt update
sudo apt install openjdk-17-jdk -y
sudo apt install zip -y
curl -s "https://sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install gradle 8.14
Build Spring Boot with Gradle
Clone and build the Spring PetClinic sample application:
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
gradle build
Deploy as a Linux Systemd Service
Create the service unit file:
sudo vi /usr/lib/systemd/system/spring.service
Paste the following service definition:
[Unit]
Description=spring
Documentation=https://github.com/spring-projects/spring-petclinic
java_version=17
[Service]
User=root
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk"
ExecStart=java -jar /home/azureuser/spring-petclinic/build/libs/spring-petclinic-4.0.0-SNAPSHOT.jar
[Install]
WantedBy=multi-user.target
Alias=spring.service
Systemctl Commands
Manage the Spring Boot service using systemctl:
sudo systemctl start spring.service # Start the service
sudo systemctl restart spring.service # Restart the service
sudo systemctl stop spring.service # Stop the service
sudo systemctl enable spring.service # Enable on boot
sudo systemctl daemon-reload # Reload systemd after editing the unit file
