Exercise-3
3. Working with Gradle: Setting Up a Gradle Project, Understanding Build Scripts (Groovy and Kotlin DSL), Dependency Management and Task Automation
What is Gradle?
Gradle is a modern, flexible, and high-performance build automation tool used in Java, Kotlin, and Android projects. It is designed to optimize build performance using incremental builds and task caching.
Why Use Gradle?
- Faster than Maven due to incremental builds.
- Uses Groovy/Kotlin DSL instead of XML.
- Highly customizable build automation.
- Supports dependency management like Maven.
- Works with multiple languages (Java, Kotlin, C++, Python, etc.).
Setting Up a Gradle Project
Step 1: Install Gradle (If Not Installed)
Check if Gradle is installed using:
If not installed, follow Exercise 1 for installation steps.
Step 2: Create a New Gradle Project
To create a Java-based Gradle project, run:
Follow the prompts:
- Select Project Type: Choose
application
. - Select Build Script Language: Choose
Groovy
orKotlin
. - Select Java Version: Choose
8, 11, 17, or latest
.
Output:
Once the setup is complete, navigate to the project folder:
Step 3: Verify the Project Structure
Gradle creates a structured project like this:
src/main/java
→ Contains main application code.src/test/java
→ Contains test cases.build.gradle
orbuild.gradle.kts
→ The Gradle build script (Groovy or Kotlin).
Understanding Gradle Build Scripts
Gradle supports two script languages:
- Groovy DSL (
build.gradle
) – Default scripting language. - Kotlin DSL (
build.gradle.kts
) – Modern, type-safe alternative.
Groovy DSL (build.gradle
) Example
Kotlin DSL (build.gradle.kts
) Example
Key Sections in the Build Script
plugins
→ Specifies project type (e.g.,java
).repositories
→ Defines where dependencies are downloaded from.dependencies
→ Lists required libraries.tasks
→ Automates build tasks.
Managing Dependencies in Gradle
Gradle uses dependency management similar to Maven.
Adding Dependencies
Inside build.gradle
or build.gradle.kts
:
After adding dependencies, refresh Gradle using:
This downloads and caches required JAR files.
Automating Tasks in Gradle
Gradle tasks automate common actions like compiling code, running tests, and packaging the project.
Common Gradle Tasks
Command | Purpose |
---|---|
gradle build | Builds the project |
gradle test | Runs unit tests |
gradle run | Runs the application (if an application plugin is used) |
gradle clean | Deletes previous build files |
gradle dependencies | Lists all project dependencies (Note: 📌 Make sure you are inside mygapp/ where build.gradle.kts is located.) |
gradle hello | Runs a custom task (from build.gradle ) |
Running a Custom Task
build.gradle.kts
file:
Run the command:
gradle hello
Expected Output:
Running and Packaging the Gradle Project
Step 1: Compile the Code
This compiles the source files in src/main/java
.
Step 2: Run Unit Tests
If all tests pass, you’ll see:
Step 3: Package the Project into a JAR
This creates a .jar
file inside the build/libs/
directory.
Step 4: Run the Application
Expected Output:
Comments
Post a Comment