How To

Install Apache Maven on Rocky Linux 10 / AlmaLinux 10 / Fedora 42

Apache Maven is a build automation and project management tool for Java applications. It handles dependency management, project compilation, testing, and packaging using a declarative XML configuration file called pom.xml. Maven uses a standardized project structure and a central repository system that automatically downloads required libraries, making Java development significantly more manageable across teams and CI/CD pipelines. It is the default build tool for projects like Apache Tomcat web applications and enterprise Java services.

Original content from computingforgeeks.com - post 4762

This guide covers two methods to install Apache Maven on Rocky Linux 10, AlmaLinux 10, and Fedora 42 – from the default OS repositories and from the official Apache binary tarball. We also cover JAVA_HOME configuration, creating a sample Maven project, building and testing with Maven, and configuring proxy settings for corporate environments.

Prerequisites

  • A server or workstation running Rocky Linux 10, AlmaLinux 10, or Fedora 42
  • Root or sudo access
  • Internet connectivity to download packages

Method 1: Install Apache Maven from DNF Repositories

The simplest way to install Maven on Rocky Linux 10, AlmaLinux 10, or Fedora 42 is from the default system repositories. This method installs Maven along with its Java (OpenJDK) dependency automatically.

Step 1: Update System Packages

Start by updating your system packages to ensure you have the latest security patches and repository metadata.

sudo dnf update -y

Step 2: Install Maven

Install Apache Maven using dnf. This pulls in OpenJDK 21 as a dependency if Java is not already installed on the system.

sudo dnf install -y maven

Step 3: Verify Maven Installation

Confirm Maven is installed and check the version along with its Java runtime.

mvn --version

The output shows the Maven version, Java version, and OS details. On Rocky Linux 10 / AlmaLinux 10, you should see Maven 3.9.9 with OpenJDK 21:

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /usr/share/maven
Java version: 21.0.10, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-21-openjdk-21.0.10.0.7-2.el10.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.12.0-65.el10.x86_64", arch: "amd64", family: "unix"

Method 2: Install Apache Maven from Binary Tarball

If you need a newer version than what the OS repos provide, or want to manage multiple Maven versions side by side, install from the official Apache binary archive. The latest release at the time of writing is Maven 3.9.14.

Step 1: Install Java (OpenJDK)

Maven requires Java to run. Install OpenJDK 21 if it is not already present on the system.

sudo dnf install -y java-21-openjdk java-21-openjdk-devel

Verify Java is available.

java -version

You should see the OpenJDK version confirmed:

openjdk version "21.0.10" 2025-07-15 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.10.0.7-2.el10) (build 21.0.10+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.10.0.7-2.el10) (build 21.0.10+7-LTS, mixed mode, sharing)

Step 2: Download Apache Maven Binary

Download the latest Maven binary tarball from the official Apache Maven installation guide mirror.

cd /tmp
curl -LO https://dlcdn.apache.org/maven/maven-3/3.9.14/binaries/apache-maven-3.9.14-bin.tar.gz

Step 3: Extract and Install Maven

Extract the archive to /opt and create a version-neutral symlink for easy upgrades.

sudo tar -xzf /tmp/apache-maven-3.9.14-bin.tar.gz -C /opt
sudo ln -sfn /opt/apache-maven-3.9.14 /opt/maven

Step 4: Configure JAVA_HOME and Maven Environment Variables

Create an environment profile script so Maven and Java paths are available system-wide for all users.

sudo vi /etc/profile.d/maven.sh

Add the following content:

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Make the script executable and load it into your current session.

sudo chmod +x /etc/profile.d/maven.sh
source /etc/profile.d/maven.sh

Step 5: Verify the Binary Installation

Confirm Maven is working from the manually installed binary.

mvn --version

The output should show the downloaded version with the correct Maven home path:

Apache Maven 3.9.14 (...)
Maven home: /opt/maven
Java version: 21.0.10, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-21-openjdk-21.0.10.0.7-2.el10.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.12.0-65.el10.x86_64", arch: "amd64", family: "unix"

You can also verify your JAVA_HOME is set correctly.

echo $JAVA_HOME

This should return the Java installation path:

/usr/lib/jvm/java-21-openjdk

Create a Sample Maven Project

With Maven installed, create a sample Java project to verify everything works end to end. Maven uses archetypes – project templates – to scaffold new projects with a standard directory structure.

Step 1: Generate the Project

Use the maven-archetype-quickstart archetype to create a basic Java application. This generates a project with a pom.xml, a sample App.java, and a unit test.

mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=my-app \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=1.5 \
  -DinteractiveMode=false

Maven downloads the required archetype plugins and dependencies on the first run, then creates the project directory. You should see BUILD SUCCESS at the end:

[INFO] --- archetype:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.5
[INFO] Parameter: groupId, Value: com.example
[INFO] Parameter: artifactId, Value: my-app
...
[INFO] BUILD SUCCESS

Step 2: Examine the Project Structure

Maven creates a standardized directory layout. The src/main/java directory holds application code, and src/test/java holds unit tests.

tree my-app

The generated project structure looks like this:

my-app
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── example
    │               └── App.java
    └── test
        └── java
            └── com
                └── example
                    └── AppTest.java

Build, Test, and Package with Maven

Maven’s build lifecycle has several phases. The most commonly used are compile, test, and package. Each phase automatically executes all preceding phases.

Compile the Project

Navigate into the project directory and compile the source code.

cd my-app
mvn compile

Maven compiles the Java source files and places the compiled classes in the target/classes directory. A successful build shows:

[INFO] --- compiler:3.13.0:compile (default-compile) @ my-app ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 1 source file with javac [debug release 8] to target/classes
[INFO] BUILD SUCCESS

Run Unit Tests

Execute the project’s unit tests with the test phase.

mvn test

Maven runs the JUnit tests defined in src/test/java and reports the results:

[INFO] --- surefire:3.2.5:test (default-test) @ my-app ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

Package as a JAR

The package phase compiles, tests, and packages the project into a JAR file. If you prefer Gradle over Maven, it uses a Groovy/Kotlin DSL instead of XML but follows similar lifecycle concepts.

mvn package

After a successful build, the JAR file is created in the target directory:

ls -lh target/*.jar

You should see the generated artifact:

-rw-r--r--. 1 root root 2.5K Mar 22 10:15 target/my-app-1.0-SNAPSHOT.jar

Run the packaged application to confirm it works.

java -cp target/my-app-1.0-SNAPSHOT.jar com.example.App

The sample application prints the default message:

Hello World!

Configure Maven settings.xml

Unlike older build tools such as Apache Ant, Maven uses convention over configuration. Maven’s behavior is controlled by two configuration files – the global settings.xml at /etc/maven/settings.xml (or /opt/maven/conf/settings.xml for binary installs) and a user-level file at ~/.m2/settings.xml. The user-level file takes precedence.

Create a user-level settings file to customize Maven for your environment. Common configurations include setting a custom local repository path, mirror repositories, and authentication credentials for private artifact servers.

mkdir -p ~/.m2
vi ~/.m2/settings.xml

Add the following base configuration:

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">

  <!-- Local repository path (default: ~/.m2/repository) -->
  <localRepository>${user.home}/.m2/repository</localRepository>

  <!-- Mirror configuration - redirect all repo requests to a local mirror -->
  <mirrors>
    <!-- Uncomment to use a local Nexus/Artifactory mirror
    <mirror>
      <id>internal-mirror</id>
      <mirrorOf>central</mirrorOf>
      <name>Internal Maven Mirror</name>
      <url>https://nexus.example.com/repository/maven-central/</url>
    </mirror>
    -->
  </mirrors>

  <!-- Server credentials for deploying to private repositories -->
  <servers>
    <!-- Uncomment to configure deployment credentials
    <server>
      <id>releases</id>
      <username>deploy-user</username>
      <password>deploy-password</password>
    </server>
    -->
  </servers>

</settings>

Configure Maven Proxy for Corporate Environments

If your server sits behind a corporate proxy, Maven needs proxy configuration to download dependencies from Maven Central and other remote repositories. Without this, builds fail with connection timeouts.

Edit the user settings file to add proxy configuration.

vi ~/.m2/settings.xml

Add the proxy block inside the <settings> element. Replace the proxy host, port, and credentials with your corporate proxy details:

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">

  <proxies>
    <proxy>
      <id>corporate-proxy</id>
      <active>true</active>
      <protocol>https</protocol>
      <host>proxy.example.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>proxypass</password>
      <nonProxyHosts>localhost|127.0.0.1|*.example.com</nonProxyHosts>
    </proxy>
  </proxies>

</settings>

The <nonProxyHosts> element defines hostnames that should bypass the proxy – use pipe-separated values with wildcards for internal domains. If your proxy does not require authentication, remove the <username> and <password> elements.

Test that Maven can download dependencies through the proxy by running a build.

mvn dependency:resolve

If the proxy configuration is correct, Maven downloads all dependencies and reports BUILD SUCCESS.

Useful Maven Commands Reference

CommandDescription
mvn cleanRemoves the target directory and all build output
mvn compileCompiles source code in src/main/java
mvn testRuns unit tests using Surefire plugin
mvn packageCompiles, tests, and packages the project (JAR/WAR)
mvn installPackages and installs the artifact to local ~/.m2 repository
mvn deployUploads the artifact to a remote repository
mvn dependency:treeDisplays the project dependency tree
mvn clean package -DskipTestsBuild without running tests
mvn versions:display-dependency-updatesShows available dependency version updates

Conclusion

Maven is installed and working on your Rocky Linux 10, AlmaLinux 10, or Fedora 42 system. You can install from DNF repos for a quick setup, or use the binary tarball for the latest version and more control over the installation. The sample project demonstrated the core build lifecycle – compile, test, and package.

For production Java projects, set up a local artifact repository like Nexus or Artifactory to cache dependencies and speed up builds. Use the ~/.m2/settings.xml file to configure mirrors, credentials, and proxy settings for your specific environment. Integrate Maven with your CI/CD pipeline using Jenkins or similar tools for automated builds and deployments.

Related Articles

AlmaLinux Install PHP OPcache on Rocky / Alma / CentOS 9|8 Desktop Install Pantheon Desktop Environment on Fedora 41/40/39 AlmaLinux Install Brave Browser on Rocky Linux 8 | AlmaLinux 8 | CentOS 8 AlmaLinux Install Kamailio SIP Server on Rocky Linux 10 / AlmaLinux

Leave a Comment

Press ESC to close