How to Create a Gradle Based Project using CLI?
Last Updated :
05 Apr, 2023
Gradle is a flexible build automation tool to build software. It is open-source, and also capable of building almost any type of software. A build automation tool automates the build process of applications. Using Gradle, we can create Android, Java, Groovy, Kotlin JVM, Scala, C++, and Swift libraries and applications. We can create Gradle-based projects in many ways, such as using CLI, IDE, etc. Here we will cover how to create a Gradle-based project using CLI (Command Line Interface).
Prerequisites to Create Gradle Project
To create a Gradle project, we need Java 1.8+ and Gradle 5.6.1+ installed on our system. If you are new to Gradle tutorials, please go through the article "How to Install Gradle on Windows?" where we have covered how to Install Gradle. Now it's time to use Gradle for creating and building projects.
Create a Gradle-Based Project using CLI
Gradle provides numerous predefine tasks to work with Gradle. To create Gradle-based projects we will use Gradle "init" task. While creating a Gradle-based project, we can enter a project name when asked to provide it in the option in CLI or select a predefined project directory name as the project name by leaving it blank in CLI. Now we will open a command prompt on windows and navigate to the directory where we want to make a project directory. For example, we created the project directory "Gradle-Java-Demo-App" this will be our project name.
Step-by-Step Implementation
Step 1: Create a folder for the new project and change the directory into it with the help of mkdir and cd commands.
$ mkdir Gradle-Java-Demo-App
$ cd Gradle-Java-Demo-App
Step 2: Execute the "gradle init" task
Execute the "gradle init" task to create a new Gradle project in CLI.
$ gradle init
Step 3: Select the type of project to generate
Gradle provides support for four (basic, application, library, Gradle plugin) types of projects. "gradle init" task prompts for a "select type of project" to be generated, as shown below.
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Here, we have selected option 2 (application). If we do not enter any option (1, 2, 3, or 4), then the project type "basic" will be selected as default.
Step 4: Select the implementation language
Once we hit Enter key, we will be asked to select the implementation language for the project. Gradle supports six different types of programming languages, as listed below.
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 3
Here, we have selected option 3 (java). If we do not enter any option (1...6), Then the default option is java.
Step 5: Select build script DSL
Once we hit Enter key, it will ask to select the type of build script to create. Gradle support Groovy and Kotlin languages for building script as below.
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Here, we have selected option 1 (Groovy). If we do not enter any option (1 or 2), Then the default build script language Groovy is selected.
Step 6: Select the test framework
Once we hit Enter key, Will asked to select a test framework for the project, Gradle support for four different java test frameworks as listed below.
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 4
Here, we have selected option 4 (Jupiter Jupiter). If we do not enter any option (1...4), Then the default test framework for java JUnit 4 is selected.
Step 7: Enter a project name
Next, Will asked to enter the project name as shown below. Either we can enter a new project name or keep a project directory name as the default project name by leaving it blank in CLI and hitting Enter.
Project name (default: Gradle-Java-Demo-App):
Here we have kept it blank to select the project directory name as the default project name.
Step 8: Source package
Similarly, Will also asked to enter the project package shown below. Here either enter a new project package name or can keep a project directory name as the default project package name by leaving it blank in CLI and hitting Enter.
Source package (default: gradle.java.demo.app):
Here also, we have kept it blank to select the project directory name as the default project package.
Step 9: BUILD SUCCESSFUL
Finally, we successfully created the Gradle project. The message "BUILD SUCCESSFUL" is shown in the below screenshot.
Create a Gradle-based project using CLIGradle Project Structure
The "gradle init" task generates the project with the following structure and default files:
├── gradle
│ └── wrapper ---------------------------------------------------- 1
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew -------------------------------------------------------- 2
├── gradlew.bat ---------------------------------------------------- 3
├── settings.gradle ------------------------------------------------ 4
└── app
├── build.gradle
└── src
├── main
│ └── java ---------------- ------------------------------ 5
│ └── gradle
│ └── java
| └── demo
| └── app
| └── App.java
└── test
└── java ---------------- ------------------------------ 6
└── gradle
└── java
└── demo
└── app
└── AppTest.java
- Generated folder for wrapper files:
- Gradle wrapper start script.
- Settings file to define build name and subprojects
- Build script of app project
- Default Java source folder
- Default Java test source folder
You now have the project set up to build a Java application.
Similar Reads
How to Create a Spring Boot Project?
Spring Boot is built on top of the spring and contains all the features of spring. It is one of the most popular frameworks for building Java-based web applications and microservices. It is a favorite among developers due to its rapid, production-ready environment, which allows developers to focus o
6 min read
How to create a Django project?
Dive into the world of web development with Python by exploring the versatile Django framework. Django is a go-to for many developers due to its popularity, open-source license, and robust security features. It enables fast and efficient project development. In this tutorial, we will guide you throu
5 min read
How to Create a Gradle Project in IntelliJ IDEA?
Gradle is an excellent open-source construction tool that is capable of the development of any kind of software. It is an automation tool that is based on Apache Ant and Apache Maven. This tool is capable of developing applications with industry standards and supports a variety of languages includin
2 min read
How to Create a New Branch on Github using Pycharm?
Git is an open-source version control system. It means that whenever a developer develops some project (like an app or website) or something, he/she can constantly update, Git is a version control system that lets you manage and keep track of your source code history. Letâs say you have a project, a
2 min read
How to Export a Git Project?
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific ap
3 min read
Create a New Maven Project from Command Prompt
Maven is a powerful project management tool that is based on POM (project object model). It is used for projects build, dependency and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT.In short, maven is a tool that can be used for building and managing an
2 min read
How to Create a Project In NetBeans GUI?
All Java Development in the IDE takes place within Projects, we first need to create a new project within which to store sources and other project files. An IDE project is a group of JAVA Source files plus its associated metadata (data about data), including project-specific properties files, and ma
2 min read
How to Create a Maven Project in Eclipse IDE?
Maven is a powerful project management tool that is based on POM (project object model). It is used for project build, dependency, and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT. In short terms we can tell maven is a tool that can be used for buildi
2 min read
Adding JGit to the project with Gradle
JGit is the lightweight and pure Java library implementing the Git version control system. It can be widely used for integrating Git functionalities into Java applications without relying on the native Git command line tools. In this article, we will explore the process of adding JGit to the Gradle
3 min read
How to Push Anything to GitHub using Git Bash?
GitHub has become the go-to platform for collaborative software development, offering powerful tools for version control, collaboration, and project management. Git Bash, a command-line interface for Git on Windows, provides a quick way to interact with GitHub repositories. In this guide, we'll lear
3 min read