0% found this document useful (0 votes)
35 views12 pages

Exp 3

The document serves as a DevOps lab manual for using Gradle with IntelliJ IDEA, covering installation verification, project creation, building and running Java applications, hosting static websites on GitHub Pages, testing with Selenium and TestNG, and packaging projects as JAR files. It also provides a comparison between Maven and Gradle, highlighting Gradle's advantages in speed and flexibility for modern projects. Additionally, it includes a Kotlin DSL version for Gradle build scripts, demonstrating how to set up a project using Kotlin.

Uploaded by

Shuchi tha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

Exp 3

The document serves as a DevOps lab manual for using Gradle with IntelliJ IDEA, covering installation verification, project creation, building and running Java applications, hosting static websites on GitHub Pages, testing with Selenium and TestNG, and packaging projects as JAR files. It also provides a comparison between Maven and Gradle, highlighting Gradle's advantages in speed and flexibility for modern projects. Additionally, it includes a Kotlin DSL version for Gradle build scripts, demonstrating how to set up a project using Kotlin.

Uploaded by

Shuchi tha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

Step 2: Verify Installation

1 gradle -v
2 gradle --version

This should display the Gradle version.

3️⃣ Creating a Gradle Project in IntelliJ IDEA


Step 1: Open IntelliJ IDEA and Create a New Project
1. Click on "New Project".
2. Select "Gradle" (under Java/Kotlin).
3. Choose Groovy or Kotlin DSL (Domain Specific Language) for the build script.
4. Set the Group ID (e.g., com.example ).
5. Click Finish.

Step 2: Understanding Project Structure

1 my-gradle-project
2 │── build.gradle (Groovy Build Script)
3 │── settings.gradle
4 │── src
5 │ ├── main
6 │ │ ├── java
7 │ │ └── resources
8 │ ├── test
9 │ │ ├── java
10 │ │ └── resources
11

4️⃣ Build and Run a Simple Java Application


Step 1: Modify build.gradle (Groovy DSL)

1 plugins {
2 id 'application'
3 }
4
5 repositories {
6 mavenCentral()
7 }
8
9 dependencies {
10 testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
11 }
12
13 application {
14 mainClass = 'com.example.Main'
15 }
16

Step 2: Create Main.java in src/main/java/com/example

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


74
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

1 package com.example;
2
3 public class Main {
4 public static void main(String[] args) {
5 System.out.println("Hello from Gradle!");
6 }
7 }
8

Step 3: Build and Run the Project


In IntelliJ IDEA, open the Gradle tool window (View → Tool Windows → Gradle).
Click Tasks > application > run .
Or run from terminal:

1 gradle run
2

5️⃣ Hosting a Static Website on GitHub Pages


Step 1: Create a /docs Directory

Create docs inside the root folder (not in src ).


Add your HTML, CSS, and images inside /docs .

Step 2: Modify build.gradle to Copy Website Files (This is optional)

1 task copyWebsite(type: Copy) {


2 from 'src/main/resources/website'
3 into 'docs'
4 }
5

Step 3: Commit and Push to GitHub

1 git add .
2 git commit -m "Deploy website using Gradle"
3 git push origin main
4

Step 4: Enable GitHub Pages

Go to GitHub Repo → Settings → Pages.


Select the /docs folder as the source.

Your website will be hosted at:

1 https://yourusername.github.io/repository-name/
2

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


75
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

6️⃣ Testing the Website using Selenium & TestNG in IntelliJ IDEA
Step 1: Add Selenium & TestNG Dependencies in build.gradle

1 dependencies {
2 testImplementation 'org.seleniumhq.selenium:selenium-java:4.28.1' // use the latest stable version
3 testImplementation 'org.testng:testng:7.4.0' // use the latest stable version
4 }
5
6 test {
7 useTestNG()
8 }
9

Step 2: Write a Test Script ( src/test/java/org/test/WebpageTest.java )

1 package org.test;
2
3 import org.openqa.selenium.WebDriver;
4 import org.openqa.selenium.chrome.ChromeDriver;
5 import org.testng.Assert;
6 import org.testng.annotations.AfterTest;
7 import org.testng.annotations.BeforeTest;
8 import org.testng.annotations.Test;
9
10 import static org.testng.Assert.assertTrue;
11
12 public class WebpageTest {
13 private static WebDriver driver;
14
15 @BeforeTest
16 public void openBrowser() throws InterruptedException {
17 driver = new ChromeDriver();
18 driver.manage().window().maximize();
19 Thread.sleep(2000);
20 driver.get("https://sauravsarkar-codersarcade.github.io/CA-GRADLE/");
21 }
22
23 @Test
24 public void titleValidationTest(){
25 String actualTitle = driver.getTitle();
26 String expectedTitle = "Tripillar Solutions";
27 Assert.assertEquals(actualTitle, expectedTitle);
28 assertTrue(true, "Title should contain 'Tripillar'");
29
30 }
31
32 @AfterTest
33 public void closeBrowser() throws InterruptedException {
34 Thread.sleep(1000);
35 driver.quit();
36 }
37
38
39 }
40

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


76
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

Step 3: Run the Tests


Open the Gradle tool window in IntelliJ.
Click Tasks > verification > test . “Recommended“
Or run from terminal:

1 gradle test // Fails sometimes due to terminal issues


2

7️⃣ Packaging a Gradle Project as a JAR


Step 1: Modify build.gradle for JAR Packaging

1 plugins {
2 id 'java'
3 id 'application'
4 }
5
6 application {
7 mainClass = 'com.example.Main'
8 }
9 jar {
10 manifest {
11 attributes 'Main-Class': 'com.example.Main' // This tells Java where to start execution
12 }
13 }
14

Step 2: Build and Package the JAR

1 gradle jar
2

The JAR file will be generated in build/libs/ .

Step 3: Run the JAR

1 java -jar build/libs/<my-gradle-project>.jar


2
3 Expected output:
4 Hello from Gradle!
5

8️⃣ Gradle Lifecycle & Common Commands


Task Command Description

Initialize Project gradle init Creates a new Gradle project.

Compile Code gradle build Compiles the project.

Run Application gradle run Runs the application.

Clean Build Files gradle clean Deletes old build files.

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


77
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

Run Tests gradle test Executes unit tests.

Generate JAR gradle jar Packages the project into a


JAR.

Deploy to GitHub Pages git push origin main Pushes website to GitHub.

9️⃣ Conclusion
Gradle provides a fast, flexible, and scalable build automation system.

We covered:

1️⃣ Project Setup in IntelliJ IDEA


2️⃣ Building & Running a Java Application
3️⃣ Hosting a Static Website on GitHub Pages
4️⃣ Testing with Selenium & TestNG
5️⃣ Packaging as a JAR
✅ Now you are ready to use Gradle for build automation! 🚀

🌟 Gradle Build Lifecycle: A Simple & Colorful Guide 🚀


Gradle follows a flexible, task-based lifecycle, unlike Maven's fixed phases. The build lifecycle in Gradle is divided into
three key stages:

🔹 1. Initialization Phase
📌 What happens here?
Determines which projects are part of the build (for multi-project builds).
Creates an instance of each project.

📌 Example Command:
1 gradle help
2

(Shows project details and verifies Gradle setup.)

🔹 2. Configuration Phase
📌 What happens here?
Gradle loads and executes the build.gradle file.
It configures tasks but does not execute them yet.

📌 Example Command:
1 gradle tasks
2

(Lists all available Gradle tasks in the project.)

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


78
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

🔹 3. Execution Phase
📌 What happens here?
Gradle executes the tasks requested by the user.
Dependencies are resolved dynamically.

Supports incremental builds (only modified files are compiled).

📌 Example Command:
1 gradle build
2

(Builds the project by compiling and packaging files.)

🎯 Key Gradle Tasks & Commands


Task 🛠️ Command Description

🧹 Clean gradle clean Deletes previous build files.

🛠 Compile gradle compileJava Compiles the Java source


code.

📦 Package (JAR/WAR) gradle jar Packages the project into a


JAR file.

✅ Test gradle test Runs unit tests.

🚀 Run Application gradle run Executes the main Java


application.

📥 Dependency Resolution gradle dependencies Displays dependency tree.

⏩ Parallel Execution gradle build --parallel Speeds up builds by running


tasks in parallel.

🎨 Gradle Lifecycle Visualized


1 Initialization → Configuration → Execution
2 (Project setup) (Tasks loaded) (Tasks executed)
3

✅ Gradle is flexible – tasks can be customized or skipped based on project needs. Unlike Maven, Gradle allows on-
demand task execution rather than following a strict lifecycle.

🌟 Conclusion
🎯 Gradle is powerful because:
✅ It only executes what’s necessary (faster builds).
✅ It supports parallel execution for large projects.
✅ It gives developers more control over task execution.
With Gradle, you can automate builds efficiently and improve performance for Java, Kotlin, and Android projects. 🚀
© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade
79
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

🔄 Maven vs. Gradle: A Detailed Comparison


Feature Maven 🏗 Gradle 🚀
Build Script Language XML ( pom.xml ) Groovy/Kotlin ( build.gradle
or build.gradle.kts )

Performance Slower due to full rebuilds Faster with incremental


builds and parallel execution

Dependency Management Uses Maven Central & local Supports Maven, Ivy, and
repository custom repositories

Configuration Style Declarative (XML-based, Declarative + Imperative


verbose) (more concise, script-based)

Ease of Use More structured, but verbose More flexible, but has a
learning curve

Incremental Builds No support (always rebuilds Supports incremental


everything) builds (only recompiles
changed files)

Build Performance Slower, builds from scratch Faster due to task caching
and incremental execution

Customization & Limited custom scripts, Highly customizable with


Extensibility plugin-based dynamic tasks and scripts

Multi-Project Builds Complex and slower Native support, optimized for


large projects

IDE Support Supported by IntelliJ IDEA, Supported by IntelliJ IDEA,


Eclipse, VS Code Eclipse, VS Code

Dependency Resolution Resolves dependencies Parallel dependency


sequentially resolution (faster)

Popularity Older and widely used in Gaining popularity, especially


enterprise projects in Android & Kotlin projects

Default Lifecycle Phases 3 lifecycle phases (clean, More flexible task execution
build, site) model

Learning Curve Easier for beginners due to Slightly steeper due to script-
structured XML based configuration

Best Suited For Stable Java projects, Modern Java, Android, and
enterprises, and legacy Kotlin projects needing high
codebases performance

🛠️ Which One Should You Choose?


✅ Use Maven if you need stability, structured builds, and an easier learning curve.
✅ Use Gradle if you want faster builds, better performance, and flexibility for complex projects.

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


80
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual
🎯 Final Verdict: If you're working on a simple Java project, Maven is a good choice. However, for modern, large-scale,
or Android projects, Gradle is the better option due to its speed, flexibility, and scalability. 🚀

Here is a KOTLIN DSL version for Gradle “This is Optional“

🎯 Gradle Kotlin Build Script ( build.gradle.kts )


Here's a simple Gradle build script written in Kotlin DSL ( build.gradle.kts ) for a Java application. 🚀

📌 build.gradle.kts (Kotlin DSL for Gradle)

1 // Apply necessary plugins


2 plugins {
3 kotlin("jvm") version "1.9.10" // Kotlin plugin for JVM
4 application // Enables the `run` task
5 }
6
7 // Define project properties
8 group = "com.example"
9 version = "1.0.0"
10 application.mainClass.set("com.example.MainKt") // Define the main class
11
12 // Repositories for dependencies
13 repositories {
14 mavenCentral() // Fetch dependencies from Maven Central
15 }
16
17 // Dependencies
18 dependencies {
19 implementation(kotlin("stdlib")) // Standard Kotlin library
20 testImplementation("org.junit.jupiter:junit-jupiter:5.9.1") // JUnit 5 for testing
21 }
22
23 // Enable JUnit 5 for tests
24 tasks.test {
25 useJUnitPlatform()
26 }
27
28 // JAR packaging
29 tasks.jar {
30 manifest {
31 attributes["Main-Class"] = "com.example.MainKt"
32 }
33 }
34

📌 Main.kt (Inside src/main/kotlin/com/example/ )

1 package com.example
2
3 fun main() {
4 println("Hello, Gradle with Kotlin DSL! 🚀")
© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade
81
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual
5 }
6

📌 Running the Project


💻 Run the application
1 gradle run
2

📦 Build the project


1 gradle build
2

🧹 Clean the build files


1 gradle clean
2

📂 Generate a JAR file


1 gradle jar
2

🌟 Why Use Kotlin DSL for Gradle?


✅ Type-Safety - Catch errors at compile-time.
✅ Better IDE Support - IntelliJ IDEA provides autocomplete and suggestions.
✅ Interoperability - Works seamlessly with Java and Kotlin projects.
This script sets up a basic Kotlin/Java project with JUnit 5 support, dependency management, and a runnable
JAR file! 🚀

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


82
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

CA - Experiment 2 Part 2 : GRADLE KOTLIN DSL WORKFLOW || IntelliJ


Idea
Here's a detailed and well-formatted documentation Gradle Kotlin DSL setup, including explanations, code snippets,
and color-coded syntax (for reference when viewing in an IDE).

Important Note : This experiment is only required if students or VTU asks you to show GRADLE with KOTLIN
DSL, or else you can skip this.

Gradle Kotlin DSL: Setting Up & Building a Kotlin Project in IntelliJ IDEA

1️⃣ Setting Up the Gradle Project


Step 1: Create a New Project
1. Open IntelliJ IDEA.
2. Click on File > New > Project.
3. Select Gradle as the build system.
4. Choose Kotlin as the language.
5. Select Gradle Kotlin DSL (it will generate build.gradle.kts ).
6. Name your project (e.g., MVNGRDKOTLINDEMO ).
7. Set the JDK (use JDK 17.0.4, since that's your version).
8. Click Finish.

2️⃣ Understanding build.gradle.kts


After creating the project, the default build.gradle.kts file looks like this:

1 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2
3 plugins {
4 kotlin("jvm") version "1.8.10" // Use latest stable Kotlin version
5 application
6 }
7
8 group = "org.example"
9 version = "1.0-SNAPSHOT"
10

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


83
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual
11 repositories {
12 mavenCentral()
13 }
14
15 dependencies {
16 implementation(kotlin("stdlib")) // Kotlin Standard Library
17 testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
18 testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")
19 }
20
21 tasks.test {
22 useJUnitPlatform()
23 }
24
25 tasks.withType<KotlinCompile> {
26 kotlinOptions.jvmTarget = "17" // Match with your JDK version
27 }
28
29 application {
30 mainClass.set("MainKt") // Update this if using a package
31 }
32

3️⃣ Creating the Main Kotlin File


Now, create your Main.kt file inside src/main/kotlin/ .
If you're using a package (e.g., org.example ), it should look like:

1 package org.example
2
3 fun main() {
4 println("Hello, Gradle with Kotlin DSL!")
5 }
6

If you're not using a package, remove the package line and ensure mainClass.set("MainKt") in build.gradle.kts .

4️⃣ Building and Running the Project


Build the Project

1 ./gradlew build
2

Run the Project

1 ./gradlew run
2

5️⃣ Packaging as a JAR


To run the project without IntelliJ, we need a JAR file.

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


84
👤 Saurav Sarkar
Coders Arcade ━━━━━━━━━━━━━━━━━━━ DevOps Lab Manual

Step 1: Create a Fat (Uber) JAR

Modify build.gradle.kts :

1 tasks.register<Jar>("fatJar") {
2 archiveClassifier.set("all")
3 duplicatesStrategy = DuplicatesStrategy.EXCLUDE
4 manifest {
5 attributes["Main-Class"] = "MainKt"
6 }
7 from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
8 with(tasks.jar.get() as CopySpec)
9 }
10

Step 2: Build the Fat JAR

1 ./gradlew fatJar
2

Step 3: Run the Fat JAR

1 java -jar build/libs/MVNGRDKOTLINDEMO-1.0-SNAPSHOT-all.jar


2

6️⃣ Common Gradle Commands


Command Description

./gradlew build Builds the project

./gradlew run Runs the application

./gradlew test Runs the tests

./gradlew clean Cleans the build directory

./gradlew fatJar Creates a runnable JAR

7️⃣ Additional Features


Adding Custom Gradle Tasks

Example: A simple task that prints "Hello, Gradle!"

1 tasks.register("hello") {
2 doLast {
3 println("Hello, Gradle!")
4 }
5 }
6

Run it with:

1 ./gradlew hello

© 2025 Coders Arcade 📺 www.youtube.com/c/codersarcade


85
👤 Saurav Sarkar

You might also like