IT&Technology
Gradle Ultimate Guide
Gradle is a modern build automation tool based on the JVM (Java Virtual Machine). It is widely used for building, testing, and deploying projects in Java, Android, Kotlin, and more. It combines the flexibility of Ant with Maven's "Convention over Configuration" strengths.
flyxyr·
#gradle#jar
1. Core Concepts
- Project: The basic unit of a Gradle build. One build consists of one or more projects.
- Task: The smallest executable unit. Examples: compiling code, running tests, generating documentation.
- Build Script: A file that defines projects and tasks. Typically
build.gradle(Groovy) orbuild.gradle.kts(Kotlin). - Gradle Wrapper (
gradlew): The Gradle-recommended way to execute builds. It automatically downloads and installs the appropriate Gradle version, ensuring all team members develop in the same environment.
2. Key Basic Commands
It is always recommended to use the Gradle Wrapper (gradlew) for commands.
Information Retrieval
./gradlew tasks: Displays all executable tasks../gradlew help --task <name>: Displays detailed help for a specific task../gradlew properties: Displays all project properties../gradlew projects: Displays the project structure (useful for multi-module projects).
Build and Compile
./gradlew build: Executes a full project build (compile, test, package)../gradlew clean: Deletes thebuilddirectory and cleans up previous build artifacts../gradlew assemble: Executes compilation and packaging only, without running tests../gradlew compileJava: Executes compilation of Java source code only../gradlew classes: Compiles source code and processes resource files.
Testing
./gradlew test: Runs all unit tests../gradlew test --tests "com.example.MyTest": Runs only a specific test class../gradlew test --debug-jvm: Runs tests in debug mode.
Run and Install
./gradlew run: Runs the application (requires theapplicationplugin)../gradlew install: Installs the generated Jar/War files into the local repository.
3. Advanced Build Options (Common Arguments)
Options to control the Gradle build process and enhance efficiency.
- Parallel Execution:
-parallel- Builds modules in parallel using all available CPU cores.
- Configure-on-demand:
--configure-on-demand- Configures only relevant modules in large multi-project builds.
- Continuous Mode:
-tor--continuous- Monitors file changes and automatically re-executes tasks when code is modified.
- Skip Tests:
-x test- Builds without executing test tasks.
- Offline Mode:
--offline- Uses only dependencies found in the local cache, without connecting to the network.
- Verbose Logging:
-i(info),-d(debug),-s(stacktrace)- Used for debugging build scripts or viewing detailed output.
- Daemon:
--daemon/--no-daemon- Gradle uses a daemon process by default to speed up builds.
4. Dependency Management Techniques
- Display Dependency Tree:
./gradlew dependencies- A powerful command for resolving dependency conflicts (Dependency Hell).
- Display Dependencies for a Specific Module:
./gradlew :app:dependencies - Refresh Dependency Cache:
--refresh-dependencies- Forces a check and download of the latest Snapshot versions or missing dependencies.
5. Multi-Project Builds
When operating from the root directory:
- Build all subprojects:
./gradlew build - Execute a task for a specific subproject:
./gradlew :sub-project:taskName- Example:
./gradlew :services:api:build
- Example:
6. Build Performance Improvement Tips
- Always use Gradle Wrapper: Avoids issues due to version mismatches.
- Enable Build Cache: Add
org.gradle.caching=truetogradle.properties. - Increase Memory Limit: Set
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8ingradle.properties. - Keep Gradle Up-to-Date: Significant performance improvements are often made with each major update.
Comments
No comments yet. Be the first!
