Frequently used Gradle commands list | Gradle cheat sheet

Gradle is a build automation tool for java and android projects. A developer working on java projects knows about Gradle command use. This tutorial covers list of commands used by the developer for daily usage in projects

Please have a look at my previous article maven installation. For the maven installation, the JDK is required.

You can use either gradle wrapper command gradlew or [installs gradle](/gradle-installation> locally for gradle command execution

How to Compile Gradle project?

gradlew compile

It will compile the project java files

How to run spring boot project with Gradle**

In the spring boot project, once the Gradle plugin is configured, Application is built using the below command

gradle build

How do you make a jar file for the spring boot application?

gradle bootJar

Here is the command for creating a war file for the same

gradle bootWar

Spring boot application can be started using the bootRun command.

gradle bootRun

This will compile, copy and run the spring boot server, It is not required to build and compile the application.

How to list tasks in a project

all tasks can be listed for the build file using the below command It will display all tasks related to the build file in a project Gradle tasks command lists out all the tasks configured in a project

gradle tasks
gradle tasks --all

These tasks should be run in the project where build.gradle exists.

B:\javaproject>gradle tasks
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'javaproject'.
components - Displays the components produced by root project 'javaproject'. [incubating]
dependencies - Displays all dependencies declared in root project 'javaproject'.
dependencyInsight - Displays the insight into a specific dependency in the root project 'javaproject'.
dependentComponents - Displays the dependent components of components in root project 'javaproject'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'javaproject'. [incubating]
outgoingVariants - Displays the outgoing variants of root project 'javaproject'.
projects - Displays the sub-projects of root project 'javaproject'.
properties - Displays the properties of root project 'javaproject'.
tasks - Displays the tasks runnable from root project 'javaproject'.

To see all tasks and more detail, run gradle tasks --all

All the tasks are listed with a description.

Gradle tasks are run with the below command.

gradle taskname

for example, to build the project

gradle build

How to Convert maven project to Gradle project

gradle setupBuild command converts the maven project to gradle build

gradle setupBuild

This read pom.xml configuration and outputs build.gradle file

How to run test case execution in Gradle

test tasks used to compile and run test cases in Gradle projects

gradle test

How to list out dependencies of the Gradle project

the below command list out all dependencies of a project

gradle dependencies

How to run clean gradle build

gradle clean command deletes previous build output. This will help to run a freshly build project.

gradle build

How to add Gradle to the existing project

grid init command required configuration and files to existing java project.

B:\myproject>gradle init

Select the type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4]


Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2]

Project name (default: myproject):

> Task :init
Get more help with your project: https://guides.gradle.org/creating-new-gradle-builds

BUILD SUCCESSFUL in 1m 4s
2 actionable tasks: 2 executed

How to add a wrapper to the Gradle project?

Gradle and Gradle wrapper are used to execute the tasks

gradle wrapper command adds the wrapper and required jars to the project

gradle wrapper --gradle-version 7.1.1

This adds the following files to the project.

└── <project folder>
    └── gradlew
    └── gradlew.bat
    └── build.gradle
    └──
    └──
    └── gradle
        └── wrapper
            └── gradle-wrapper.jar
            └── gradle-wrapper.properties