2
GRADLE INSTALLATION
sudo apt update
sudo apt install openjdk-17-jdk -y
java --version
sudo update-alternatives --config java
Remove Existing Gradle (Only if Present)
gradle -v
sudo apt remove gradle -y //via APT
ls ~/.sdkman //via sdkman
sdk uninstall gradle <version> --force
#Replace <version> with the installed version for example, 8.13
sdk uninstall gradle 8.13 --force
Download and Extract Gradle
wget https://services.gradle.org/distributions/gradle-8.14-bin.zip -P /tmp
sudo unzip -d /opt/gradle /tmp/gradle-8.14-bin.zip
Modify bash command file by running the following command:
gedit ~/.bashrc
At the end of the file, add the following lines:
export GRADLE_HOME=/opt/gradle/gradle-8.14
export PATH=${GRADLE_HOME}/bin:${PATH}
Save the file. Then to apply the changes run the following in terminal:
source ~/.bashrc
Verify Gradle Installation
gradle -v
Create and Initialize the Project
cd ~
mkdir HelloGradleGroovy
cd HelloGradleGroovy
gradle init --type java-application --dsl groovy --overwrite
Choose defaults by pressing Enter for each prompt.
Verify Project Structure
tree
Open the file:
gedit app/build.gradle
Replace its contents with:
plugins {
// Apply the Java plugin for compiling Java code
id 'java'
// Apply the application plugin to add support for building an application
id 'application'
}
group = 'org.example'
version = '1.0'
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Define your dependencies. Use JUnit Jupiter (JUnit 5) for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
// If using parameterized tests
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.0'
}
application {
// Define the main class for the application.
mainClass = 'org.example.App'
}
// A custom task example: printing a greeting
task hello {
doLast {
println 'Hello, Gradle!'
}
}
Running and Verifying Your Gradle Project
gradle build
gradle run
gradle hello
Set Up Gradle Project with Kotlin DSL
cd ~
mkdir HelloGradleKotlin
cd HelloGradleKotlin
gradle init --type java-application --dsl kotlin --overwrite
Choose defaults by pressing Enter for each prompt.
Verify Project Structure
tree
Open the file:
gedit app/build.gradle.kts
Replace its contents with:
plugins {
// Apply the Java plugin for compiling Java code
java
// Apply the application plugin to add support for building an application
application
}
group = "org.example"
version = "1.0"
repositories {
mavenCentral() // Configures Maven Central for dependencies
}
dependencies {
// Define dependencies using Kotlin DSL syntax. Use JUnit Jupiter (JUnit 5) for testing.
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.0")
// If using parameterized tests
testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.0")
}
application {
mainClass.set("org.example.App") // Specifies the main class
}
// A custom task example using Kotlin DSL
tasks.register("hello") {
doLast {
println("Hello, Gradle with Kotlin DSL!")
}
}
Running and Verifying Your Gradle Project
gradle build
gradle run
gradle hello
Comments
Post a Comment