Maven and Gradle are the two dominant build tools for Java projects. Maven uses an XML-based declarative configuration (pom.xml), follows strict conventions, and dominates in enterprise environments with heavy use of the Maven Central repository for dependency management. Gradle uses a Groovy or Kotlin DSL, is more flexible and faster than Maven (due to incremental builds and build caching), and is the standard build tool for Android development and modern Java/Kotlin projects. Both tools manage project dependencies, compile source code, run tests, and package applications into JAR, WAR, or executable JAR files. This guide covers installing both Maven 3 and Gradle 8 on RHEL 9, configuring local repositories, and building a sample Java project with each.
Prerequisites
- OpenJDK 21 installed on RHEL 9
Step 1 — Install Maven
# Install Maven from RHEL AppStream
dnf install -y maven
mvn --version
# Configure Maven local repository (default: ~/.m2/repository)
# For shared server installs, set to a shared location:
# Edit /etc/maven/settings.xml or ~/.m2/settings.xml
# /var/maven-repo
Step 2 — Create and Build a Maven Project
# Generate a new Maven project from archetype
mvn archetype:generate
-DgroupId=com.example
-DartifactId=myapp
-DarchetypeArtifactId=maven-archetype-quickstart
-DarchetypeVersion=1.4
-DinteractiveMode=false
cd myapp
# Build the project (compile + test + package)
mvn package
# Run the JAR
java -cp target/myapp-1.0-SNAPSHOT.jar com.example.App
Step 3 — Install Gradle
GRADLE_VERSION="8.8"
cd /opt
curl -LO "https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip"
unzip gradle-${GRADLE_VERSION}-bin.zip
ln -s gradle-${GRADLE_VERSION} gradle
# Add to PATH
cat >> /etc/profile.d/gradle.sh << 'EOF'
export GRADLE_HOME=/opt/gradle
export PATH=$PATH:$GRADLE_HOME/bin
EOF
source /etc/profile.d/gradle.sh
gradle --version
Step 4 — Create and Build a Gradle Project
mkdir /tmp/mygradle && cd /tmp/mygradle
gradle init --type java-application --dsl groovy --project-name mygradle --no-incubating
# Build the project
gradle build
# Run the application
gradle run
Step 5 — Common Maven and Gradle Commands
# Maven
mvn clean # Delete target/ directory
mvn compile # Compile source code
mvn test # Run unit tests
mvn package # Create JAR/WAR
mvn install # Install to local Maven repo (~/.m2)
mvn dependency:tree # Show dependency tree
mvn -DskipTests package # Skip tests when packaging
# Gradle
gradle clean # Delete build/ directory
gradle compileJava # Compile source
gradle test # Run tests
gradle jar # Create JAR
gradle dependencies # Show dependency tree
gradle build -x test # Build skipping tests
gradle wrapper # Generate Gradle wrapper scripts (recommended)
Conclusion
Maven and Gradle on RHEL 9 provide complete Java build and dependency management. For new projects, Gradle is generally preferred due to faster incremental builds and a more expressive DSL. For existing enterprise projects already using Maven, continuing with Maven is practical — Maven’s XML-based POM files are verbose but highly predictable and well-supported by all Java IDEs and CI/CD systems. Both tools integrate with Jenkins, GitHub Actions, and GitLab CI for automated build pipelines.
Next steps: How to Install Java on RHEL 9, How to Deploy a Spring Boot Application on RHEL 9, and How to Install Jenkins on RHEL 9.