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:

gradle -version

If not installed, follow Exercise 1 for installation steps.

Step 2: Create a New Gradle Project

To create a Java-based Gradle project, run:

mkdir myapp
cd myapp gradle init

Follow the prompts:

  • Select Project Type: Choose application.
  • Select Build Script Language: Choose Groovy or Kotlin.
  • 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:

myapp │── src │ ├── main │ │ └── java │ │ └── com.example │ │ └── App.java │ ├── test │ │ └── java │ │ └── com.example │ │ └── AppTest.java │── build.gradle (or build.gradle.kts) │── settings.gradle │── gradlew │── gradlew.bat │── gradle/
  • src/main/java → Contains main application code.
  • src/test/java → Contains test cases.
  • build.gradle or build.gradle.kts → The Gradle build script (Groovy or Kotlin).
Output:


Understanding Gradle Build Scripts

Gradle supports two script languages:

  1. Groovy DSL (build.gradle) – Default scripting language.
  2. Kotlin DSL (build.gradle.kts) – Modern, type-safe alternative.

Groovy DSL (build.gradle) Example

plugins {
id 'java' } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web:2.7.0' testImplementation 'junit:junit:4.13.2' } tasks.register('hello') { doLast { println 'Hello, DevOps with Gradle!' } }

Kotlin DSL (build.gradle.kts) Example

plugins {
java } repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boot-starter-web:2.7.0") testImplementation("junit:junit:4.13.2") } tasks.register("hello") { doLast { println("Hello, DevOps with Gradle!") } }

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:

dependencies { // Add Guava dependency correctly implementation("com.google.guava:guava:31.1-jre") // Spring Boot Starter Web implementation("org.springframework.boot:spring-boot-starter-web:2.7.0") // JUnit 4 for testing testImplementation("junit:junit:4.13.2") }

After adding dependencies, refresh Gradle using:

gradle build

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

CommandPurpose
gradle buildBuilds the project
gradle testRuns unit tests
gradle runRuns the application (if an application plugin is used)
gradle cleanDeletes previous build files
gradle dependencies

Lists all project dependencies
(Note: 📌 Make sure you are inside
mygapp/ where build.gradle.kts is located.)
gradle helloRuns a custom task (from build.gradle)

Running a Custom Task

add the following to your build.gradle.kts file:

tasks.register("hello"{
doLast { println("Hello, DevOps with Gradle!") } }

Run the command:

gradle hello

Expected Output:

Hello, DevOps with Gradle!

Running and Packaging the Gradle Project

Step 1: Compile the Code

gradle compileJava

This compiles the source files in src/main/java.

Step 2: Run Unit Tests

gradle test

If all tests pass, you’ll see:

BUILD SUCCESSFUL

Step 3: Package the Project into a JAR

gradle jar

This creates a .jar file inside the build/libs/ directory.

Step 4: Run the Application

java -jar build/libs/myapp.jar

Expected Output:

Hello, DevOps with Gradle!

Comments