Introduction
Gradle is a popular build automation tool used primarily for Java, Android, and other JVM-based projects. It offers a powerful and flexible way to manage project builds, dependencies, and other tasks through a Groovy or Kotlin DSL. Understanding the essential Gradle commands can help you streamline your development workflow. This cheat sheet provides a quick reference to the most commonly used Gradle commands.
What is Gradle?
Gradle is a build automation tool that supports multi-project builds, dependency management, and task automation. It is designed to be highly customizable and scalable, making it suitable for projects of all sizes.
Gradle uses a domain-specific language (DSL) based on Groovy or Kotlin to describe builds, providing a powerful alternative to traditional XML-based build scripts.
Gradle Commands Cheat Sheet
Here is a handy cheat sheet of the most commonly used Gradle commands, ordered by their usage and popularity:
Gradle Command | Description |
---|---|
gradle init |
Initializes a new Gradle project. |
gradle build |
Compiles, tests, and assembles the project. |
gradle clean |
Deletes the build directory. |
gradle assemble |
Assembles the outputs of the project without running tests. |
gradle check |
Runs all checks, including tests. |
gradle test |
Runs the unit tests. |
gradle buildDependents |
Assembles and tests this project and all projects that depend on it. |
gradle dependencies |
Displays the dependencies of the project. |
gradle dependencyInsight |
Provides insight into a specific dependency in the project. |
gradle help |
Displays help information about available tasks and options. |
gradle projects |
Displays the sub-projects of the project. |
gradle properties |
Displays the properties of the project. |
gradle tasks |
Displays the tasks available in the project. |
gradle wrapper |
Generates Gradle wrapper files. |
gradle jar |
Assembles a JAR archive containing the main classes. |
gradle war |
Assembles a WAR archive containing the web application. |
gradle bootRun |
Runs a Spring Boot application. |
gradle bootJar |
Assembles an executable JAR archive for a Spring Boot application. |
gradle bootWar |
Assembles an executable WAR archive for a Spring Boot application. |
gradle publish |
Publishes all publications produced by this project to the specified repository. |
gradle uploadArchives |
Uploads the project's artifacts to the specified repository. |
gradle eclipse |
Generates Eclipse project files. |
gradle idea |
Generates IntelliJ IDEA project files. |
gradle --stop |
Stops the Gradle Daemon if it is running. |
gradle --status |
Displays the status of the Gradle Daemon. |
gradle --refresh-dependencies |
Refreshes the dependency cache, forcing a check for updated dependencies. |
Gradle Commands Cheat Sheet Demonstrated with a Sample Project
Let's use a sample Java project to demonstrate the important Gradle commands. This project will have a simple structure with a few Java classes and dependencies.
Sample Project Structure
my-sample-project
├── build.gradle
├── settings.gradle
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── App.java
│ │ └── resources
│ └── test
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── AppTest.java
│ └── resources
└── gradle
└── wrapper
└── gradle-wrapper.properties
build.gradle File
plugins {
id 'java'
id 'application'
}
group = 'com.example'
version = '1.0-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
}
test {
useJUnitPlatform()
}
application {
mainClassName = 'com.example.App'
}
settings.gradle File
rootProject.name = 'my-sample-project'
Important Gradle Commands with Examples
Gradle Init Command
Initializes a new Gradle project.
Syntax:
gradle init
Example: Navigate to the desired directory and run:
cd /path/to/your/project
gradle init
This command initializes a new Gradle project in the current directory, setting up the basic project structure and necessary files.
Gradle Build Command
Compiles, tests, and assembles the project.
Syntax:
gradle build
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle build
This command compiles the source code, runs the tests, and assembles the final artifacts of the project.
Gradle Clean Command
Deletes the build directory.
Syntax:
gradle clean
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle clean
This command removes the build
directory, cleaning up all files generated by previous builds.
Gradle Assemble Command
Assembles the outputs of the project without running tests.
Syntax:
gradle assemble
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle assemble
This command compiles the source code and packages the application, but skips the testing phase.
Gradle Check Command
Runs all checks, including tests.
Syntax:
gradle check
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle check
This command runs all verification tasks in the project, including unit tests and code quality checks.
Gradle Test Command
Runs the unit tests.
Syntax:
gradle test
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle test
This command executes the unit tests for the project, as defined in src/test/java
.
Gradle Dependencies Command
Displays the dependencies of the project.
Syntax:
gradle dependencies
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle dependencies
This command provides a detailed list of all the dependencies used in the project.
Gradle DependencyInsight Command
Provides insight into a specific dependency in the project.
Syntax:
gradle dependencyInsight --dependency <dependency-name>
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle dependencyInsight --dependency junit
This command gives detailed information about the specified dependency (junit
in this case), including where it is used and any conflicts.
Gradle Tasks Command
Displays the tasks available in the project.
Syntax:
gradle tasks
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle tasks
This command lists all the tasks that can be executed in the project, providing a quick overview of available operations.
Gradle Wrapper Command
Generates Gradle wrapper files.
Syntax:
gradle wrapper
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle wrapper
This command creates the Gradle wrapper files (gradlew
and gradlew.bat
), allowing the project to be built with a specific Gradle version without requiring Gradle to be installed globally.
Gradle Jar Command
Assembles a JAR archive containing the main classes.
Syntax:
gradle jar
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle jar
This command creates a JAR file containing the compiled classes of the project, located in the build/libs
directory.
Gradle BootRun Command
Runs a Spring Boot application.
Syntax:
gradle bootRun
Example: Navigate to the project directory and run:
cd /path/to/your/project
gradle bootRun
This command runs the Spring Boot application directly from the source code. (Ensure you have the Spring Boot plugin and dependencies configured in your build.gradle
.)
Example Output
After running these commands, your project directory will have the following additional content:
my-sample-project
├── build
│ ├── classes
│ ├── distributions
│ ├── libs
│ ├── reports
│ ├── scripts
│ └── tmp
├── gradlew
├── gradlew.bat
└── gradle
└── wrapper
└── gradle-wrapper.properties
└── gradle-wrapper.jar
Conclusion
Mastering Gradle commands is essential for managing your builds and dependencies efficiently. This cheat sheet, demonstrated with a sample project, provides a quick reference to the most commonly used Gradle commands, helping you streamline your build process and improve your development workflow.
By understanding and using these commands, you can simplify your project builds, enhance your productivity, and ensure your applications are well-structured and maintainable.
Keep this guide handy to make the most of Gradle. Happy coding!
Comments
Post a Comment
Leave Comment