How to Install Maven and Gradle on RHEL 7

Apache Maven and Gradle are the two dominant build automation tools in the Java ecosystem. Maven, with its opinionated convention-over-configuration approach and XML-based pom.xml descriptor, has been an enterprise standard for over a decade. Gradle, a newer entrant, uses a Groovy or Kotlin DSL and provides faster incremental builds through an advanced caching system. Many teams support both tools in parallel, particularly when migrating from Maven to Gradle or when working with mixed codebases. This guide covers installing both tools on RHEL 7, configuring them system-wide, and verifying they are ready for use.

Prerequisites

  • RHEL 7 with root or sudo access
  • Java 8 or later already installed and JAVA_HOME configured (see the Java installation guide)
  • wget or curl available for downloading archives
  • Sufficient disk space in /opt (at least 500 MB recommended)

Step 1: Install wget and Verify Java

Ensure the download tool and Java prerequisites are in place before proceeding:

sudo yum install -y wget
java -version
echo $JAVA_HOME

Both commands should produce output without errors. If JAVA_HOME is empty, source the profile script or set it manually before continuing, as both Maven and Gradle require it at runtime.

Step 2: Download Apache Maven

Maven does not ship in the standard RHEL 7 repositories, so you must download the binary tarball directly from the Apache website. Check https://maven.apache.org/download.cgi for the latest stable release. At the time of writing, Maven 3.9.x is the current stable series.

cd /tmp
wget https://downloads.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz

Verify the download integrity using the provided SHA-512 checksum:

wget https://downloads.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz.sha512
sha512sum -c apache-maven-3.9.6-bin.tar.gz.sha512

A result of OK confirms the archive is intact.

Step 3: Extract and Install Maven to /opt

Extract the tarball to /opt/maven — a conventional location for manually installed software on Linux systems:

sudo tar -xzf /tmp/apache-maven-3.9.6-bin.tar.gz -C /opt/
sudo ln -s /opt/apache-maven-3.9.6 /opt/maven

The symbolic link /opt/maven makes future upgrades easier: when a new version is released, you extract it to /opt/apache-maven-3.x.y, update the symlink, and all scripts referencing /opt/maven automatically pick up the new version without further changes.

Step 4: Configure M2_HOME and PATH for Maven

Create a system-wide profile script so that every user and service that opens a login shell inherits the Maven environment variables:

sudo tee /etc/profile.d/maven.sh <<'EOF'
# Apache Maven environment configuration
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
EOF

Apply it to the current session:

source /etc/profile.d/maven.sh

Verify Maven is available and correctly configured:

mvn --version

Expected output:

Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /opt/maven
Java version: 1.8.0_412, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-2.el7_9.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"

Step 5: Run a Basic Maven Build

Create a minimal Maven project to confirm the tool works end-to-end:

mvn archetype:generate 
  -DgroupId=com.example 
  -DartifactId=hello-world 
  -DarchetypeArtifactId=maven-archetype-quickstart 
  -DarchetypeVersion=1.4 
  -DinteractiveMode=false

cd hello-world
mvn clean install

Maven will download its dependency plugins on the first run (requires internet access). A successful build ends with:

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.432 s

The compiled JAR is placed in the target/ directory. Common Maven lifecycle phases you will use regularly include:

  • mvn clean — removes the target/ directory
  • mvn compile — compiles source code only
  • mvn test — runs unit tests
  • mvn package — produces the JAR or WAR artifact
  • mvn install — installs the artifact to the local repository (~/.m2/repository)
  • mvn deploy — publishes the artifact to a remote repository

Step 6: Download and Install Gradle

Gradle also ships as a binary ZIP archive from https://gradle.org/releases/. Download the binary-only distribution (not the full distribution, which includes sources and documentation, and is much larger):

cd /tmp
wget https://services.gradle.org/distributions/gradle-8.7-bin.zip

Install unzip if it is not already present, then extract:

sudo yum install -y unzip
sudo unzip -q /tmp/gradle-8.7-bin.zip -d /opt/
sudo ln -s /opt/gradle-8.7 /opt/gradle

Step 7: Configure GRADLE_HOME and PATH

sudo tee /etc/profile.d/gradle.sh <<'EOF'
# Gradle environment configuration
export GRADLE_HOME=/opt/gradle
export PATH=${GRADLE_HOME}/bin:${PATH}
EOF

source /etc/profile.d/gradle.sh

Verify the installation:

gradle --version

Expected output:

------------------------------------------------------------
Gradle 8.7
------------------------------------------------------------

Build time:   2024-03-22 15:52:46 UTC
Revision:     650af14d7653aa949fce5e886e685efc9cf97c10

Kotlin:       1.9.22
Groovy:       3.0.21
Ant:          Apache Ant(TM) version 1.10.13
JVM:          1.8.0_412 (Red Hat, Inc. 25.412-b08)
OS:           Linux 3.10.0-1160.el7.x86_64 amd64

Step 8: Using the Gradle Wrapper

In practice, most Gradle projects use the Gradle Wrapper (gradlew) rather than a system-installed Gradle. The wrapper is a small shell script committed to the project repository that downloads and caches the exact Gradle version specified in the project, ensuring reproducible builds regardless of what is installed on the host.

When you clone a project that includes gradlew, use it directly instead of the system gradle command:

chmod +x ./gradlew
./gradlew --version
./gradlew clean build

To add a wrapper to a new project using the system Gradle:

mkdir myproject && cd myproject
gradle wrapper --gradle-version 8.7

This generates gradlew, gradlew.bat, and the gradle/wrapper/ directory. Always commit these files to version control.

Step 9: Verifying Both Tools Work Together

Both Maven and Gradle can coexist on the same system without conflict. Confirm both are available in your PATH simultaneously:

which mvn && mvn --version
which gradle && gradle --version
echo "M2_HOME: $M2_HOME"
echo "GRADLE_HOME: $GRADLE_HOME"
echo "JAVA_HOME: $JAVA_HOME"

Conclusion

With Apache Maven and Gradle both installed and configured on your RHEL 7 system, you are equipped for Java development and CI/CD pipelines that target either build system. By placing the tools in /opt with symbolic links and setting environment variables through /etc/profile.d/ scripts, the configuration is system-wide, easy to update when new versions are released, and consistent for all users. The next logical step is to use these tools to build and deploy a Java application — covered in the Spring Boot deployment guide.