Frequently used Gradle commands list | gradle cheat sheet


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

gradle command cheat sheet

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

Frequently used Maven commands

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 spring boot project, once gradle plugin is configured, Application is build using below command

gradle build

How do you make jar file for spring boot application?

gradle bootJar

Here is the command for creating war file for the same

gradle bootWar

Spring boot application can be started using 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 build file using below command It will display all tasks related the build file in a project gradle tasks command list out all the tasks configured in a project

gradle tasks
gradle tasks --all

This tasks should be run in 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 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 description

Gradle tasks are run with 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 gradle project

the below command list out all dependencies of an project

gradle dependencies

How to run clean gradle build

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

gradle build

How to add gradle to existing project.

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

B:\myproject>gradle init

Select 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 wrapper to 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 following files to project.

└── <project folder>
    └── gradlew
    └── gradlew.bat
    └── build.gradle
    └── 
    └── 
    └── gradle
        └── wrapper
            └── gradle-wrapper.jar
            └── gradle-wrapper.properties
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.