Program 3
Program 3
3. Working with Gradle: Setting Up a Gradle Project, Understanding Build Scripts (Groovy and Kotlin
DSL), Dependency Management and Task Automation.
This command creates a new Java application project with a sample build.gradle file.
Gradle uses a DSL (Domain-Specific Language) to define the build scripts. Gradle supports two DSLs:
Groovy DSL: This is the default language used for Gradle build scripts (build.gradle). Example of a
simple build.gradle file (Groovy DSL):
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4'
}
task customTask {
doLast {
println 'This is a custom task'
}
}
Kotlin DSL: Gradle also supports Kotlin for its build scripts (build.gradle.kts). Example of a
simple build.gradle.kts file (Kotlin DSL):
plugins {
kotlin("jvm") version "1.5.21"
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:2.5.4")
}
tasks.register("customTask") {
doLast {
println("This is a custom task")
}
}
Syntax: Groovy uses a more concise, dynamic syntax, while Kotlin offers a more structured, statically-
typed approach.
Error handling: Kotlin provides better error detection at compile time due to its static nature.
Task Block: Tasks define operations in Gradle, and they can be executed from the command line
using gradle <task-name>.
In Groovy DSL:
task hello {
doLast {
println 'Hello, Gradle!'
}
}
In Kotlin DSL:
tasks.register("hello") {
doLast {
println("Hello, Gradle!")
}
}
3: Dependency Management
Gradle provides a powerful dependency management system. You define your project’s dependencies in
the dependencies block.
1. Adding dependencies:
Gradle supports various dependency scopes such
as implementation, compileOnly, testImplementation, and others.
dependencies {
implementation 'com.google.guava:guava:30.1-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
}
dependencies {
implementation("com.google.guava:guava:30.1-jre")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
}
2. Declaring repositories: To resolve dependencies, you need to specify repositories where Gradle
should look for them. Typically, you’ll use Maven Central or JCenter, but you can also configure
private repositories.
Example (Groovy):
repositories {
mavenCentral()
}
Example (Kotlin):
repositories {
mavenCentral()
}
4: Task Automation
Gradle tasks automate various tasks in your project lifecycle, like compiling code, running tests, and
creating builds.
1. Using predefined tasks: Gradle provides many predefined tasks for common activities, such as:
build – compiles the project, runs tests, and creates the build output.
test – runs tests.
clean – deletes the build output.
2. Example of running the build task:
gradle build
3. Creating custom tasks: You can define your own tasks to automate specific actions. For example,
creating a custom task to print a message.
task printMessage {
doLast {
println 'This is a custom task automation'
}
}
tasks.register("printMessage") {
doLast {
println("This is a custom task automation")
}
}
gradle <task-name>
For example:
6: Advanced Automation
You can define task dependencies and configure tasks to run in a specific order. Example of task
dependency:
task firstTask {
doLast {
println 'Running the first task'
}
}
task secondTask {
dependsOn firstTask
doLast {
println 'Running the second task'
}
}
In this case, secondTask will depend on the completion of firstTask before it runs.
plugins {
id 'application'
}
application {
mainClass = 'com.example.AdditionOperation'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.13.2'
}
test {
outputs.upToDateWhen { false }
testLogging {
events "passed", "failed", "skipped"
exceptionFormat "full"
showStandardStreams = true
}
}
package com.example;
Step 4: AdditionOperationTest.java (JUnit Test) (Change file name and update below code)
package com.example;
import org.junit.Test;
import static org.junit.Assert.*;
@Test
public void testAddition() {
double num1 = 5;
double num2 = 10;
double expectedSum = num1 + num2;
gradle build
gradle run
To test the project:
gradle test
plugins {
kotlin("jvm") version "1.8.21"
application
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
testImplementation("junit:junit:4.13.2")
}
application {
mainClass.set("com.example.MainKt")
}
tasks.test {
useJUnit()
testLogging {
events("passed", "failed", "skipped")
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
showStandardStreams = true
}
outputs.upToDateWhen { false }
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
package com.example
fun main() {
val num1 = 10.0
val num2 = 5.0
val result = addNumbers(num1, num2)
println("The sum of $num1 and $num2 is: $result")
}
Step 4: MainTest.kt (JUnit Test) (Change file name and update below code)
package com.example
import org.junit.Assert.*
import org.junit.Test
class MainTest {
@Test
fun testAddNumbers() {
val num1 = 10.0
val num2 = 5.0
assertEquals("The sum of $num1 and $num2 should be 15.0", 15.0, result, 0.001)
}
}
gradle build
To run the project:
gradle run
gradle test