Prerequisites: Install Java Development Kit (JDK)
Before using Gradle, ensure you have JDK 8 or higher installed. Run java -version and javac -version to verify. If not installed, use Ubuntu’s package manager:
sudo apt update
sudo apt install openjdk-11-jdk
Step 1: Install Gradle on Ubuntu
You can install Gradle via the package manager (easier) or manually (more control).
sudo apt update
wget -qO - https://packages.gradle.org/gpg | sudo apt-key add -
sudo sh -c 'echo "deb https://packages.gradle.org/distributions/ gradle/$(curl -s https://packages.gradle.org/distributions/latest/release)" > /etc/apt/sources.list.d/gradle.list'
sudo apt update
sudo apt install gradle
~/gradle):unzip gradle-x.y.z-bin.zip -d ~/gradle
PATH:echo "export PATH=$HOME/gradle/gradle-x.y.z/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
Step 2: Verify Gradle Installation
Run gradle -v in the terminal. You should see the installed Gradle version, JVM details, and operating system info.
Step 3: Create or Initialize a Java Project
You can create a project manually or use Gradle’s init task.
Create a root directory and standard source folders:
mkdir my-java-project
cd my-java-project
mkdir -p src/main/java src/test/java
Run gradle init in an empty directory and follow the prompts:
mkdir my-java-project
cd my-java-project
gradle init
Select basic project type, then choose Java application when prompted. This generates a build.gradle file and basic directories.
Step 4: Configure build.gradle
Edit the build.gradle file to define project settings, dependencies, and tasks. A minimal Java project requires:
Example build.gradle:
plugins {
id 'java' // Provides Java compilation, testing, and packaging tasks
id 'application' // Adds tasks to run and package the application
}
group = 'com.example' // Project group (like a package name)
version = '1.0-SNAPSHOT' // Project version
repositories {
mavenCentral() // Uses Maven Central for dependency resolution
}
dependencies {
// Add project dependencies here (e.g., libraries)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
application {
mainClass = 'com.example.Main' // Fully qualified name of the main class
}
test {
useJUnitPlatform() // Configures JUnit 5 for testing
}
Step 5: Compile the Java Project
Navigate to the project root directory and run:
gradle build
This command:
src/main/java.src/test/java.build/libs/<project-name>-<version>.jar.Step 6: Run the Compiled Project
To execute the program, use the run task (if you added the application plugin) or manually run the JAR:
# Option 1: Use Gradle's run task (simpler)
gradle run
# Option 2: Run the JAR file directly
java -jar build/libs/my-java-project-1.0-SNAPSHOT.jar
Optional: Use Gradle Wrapper for Reproducibility
The Gradle wrapper (gradlew) ensures your project uses a specific Gradle version, even if it’s not installed on the system. To add it:
gradle wrapper --gradle-version 8.5 # Replace with desired version
This creates gradlew (Linux/macOS) and gradlew.bat (Windows) scripts. Use them instead of the global gradle command:
./gradlew build # Linux/macOS
./gradlew.bat build # Windows