Monday, November 18, 2024
Google search engine
HomeLanguagesJavaHow to Create a Gradle Based Project using CLI?

How to Create a Gradle Based Project using CLI?

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 CLI

Create a Gradle-based project using CLI

Gradle 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
  1. Generated folder for wrapper files:  
  2. Gradle wrapper start script.
  3. Settings file to define build name and subprojects
  4. Build script of app project
  5. Default Java source folder
  6. Default Java test source folder

You now have the project set up to build a Java application.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments