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.
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
- 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.
Contact Us