How to Install Apache Maven on Fedora 40 or 39

Apache Maven is a powerful build automation and project management tool primarily used in Java development. It simplifies the process of building, packaging, and managing dependencies for software projects. Maven uses a standardized project structure and a central repository to manage project libraries and plugins, making it a preferred tool for developers working on large-scale Java applications.

To install Apache Maven on Fedora 40 or 39, you have two main methods: using Fedora’s AppStream, which is recommended for most users due to its simplicity and stability, or downloading the source archive directly from Apache Maven for more control over the version and installation process. This guide will cover both methods to help you get Maven up and running on your Fedora system.

Update Fedora Before Apache Maven Installation

Before installing Apache Maven, updating and upgrading your Fedora system is crucial. This step ensures that all packages are current, reducing potential conflicts during the Maven installation. Execute the following command in the terminal to refresh your system’s package index and upgrade the packages:

sudo dnf upgrade --refresh

This command combines two actions: upgrade updates all installed packages to their latest versions, and –refresh forces a refresh of the repository metadata.

Method 1: Install Apache Maven via Appstream (Default Method)

Initiating Installation Using Fedora’s Default Repository

Installing Apache Maven on Fedora is straightforward with the Fedora Appstream, the operating system’s default repository. This repository typically contains a stable version of Maven, updated biannually to include major version upgrades. This ensures a balance between up-to-date features and system stability.

To install Apache Maven, run the following command in the terminal with root access:

sudo dnf install maven

This command retrieves and installs the latest version of Maven available in Fedora’s default repository. The dnf package manager handles dependencies and version compatibility, streamlining the installation process.

Verifying Maven Installation

Post-installation, it’s essential to verify the successful installation of Apache Maven. This step confirms that Maven is correctly installed and operational. In the terminal, execute the following command:

mvn -version

This command displays the installed version of Apache Maven. A successful installation will output the version details, confirming that Maven is ready.

Method 2: Install Apache Maven on Fedora via Tarball (Latest Version)

Preparing Java Installation

Before installing Apache Maven, ensure Java is installed on your Fedora system. Java is a prerequisite for Maven. Install Java OpenJDK by executing the following command:

sudo dnf install java-openjdk

This command installs the Java OpenJDK package, which Maven requires to function.

Downloading Apache Maven

To download the latest version of Apache Maven, first visit the official Apache Maven website to identify the most recent release. Then, use the wget command to download the specified Maven version.

For instance, to download Maven 3.9.6, execute:

wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz

This command fetches the specified Apache Maven binary archive and saves it to your local machine.

Extracting and Installing Maven

After downloading, navigate to the directory containing the downloaded file. Extract the contents of the archive using:

tar xzf apache-maven-3.9.6-bin.tar.gz

This command creates a new directory named “apache-maven-3.9.6” containing the extracted files.

Configuring Environment Variables

Temporary Environment Configuration

For one-time use, set the Maven environment variables manually in the terminal:

export M2_HOME=/path/to/maven
export PATH=$PATH:$M2_HOME/bin

Replace /path/to/maven with the path to your extracted Maven directory.

For example:

export M2_HOME=/home/$USER/apache-maven-3.9.6
export PATH=$PATH:$M2_HOME/bin

Replace /path/to/maven with the path to your Maven directory, like /home/$USER/apache-maven-3.9.6.

Permanent Environment Configuration

To permanently add Maven to your environment, append the export commands to your .bashrc file:

echo export M2_HOME=/home/$USER/apache-maven-3.9.6 >> ~/.bashrc
echo export PATH=$PATH:$M2_HOME/bin >> ~/.bashrc

Important Reminder: It’s crucial to enter these commands in the above sequence. Reversing the order or mixing them up can lead to problems when attempting to use the mvn command. Correct ordering ensures that your environment variables are set up correctly for Maven.

After appending these lines, apply the changes by sourcing the .bashrc file:

source ~/.bashrc

This command updates your session, adding the Maven binaries to your system path.

Verifying Maven Installation

To confirm the successful installation of Maven, check its version with:

mvn -version

This command displays the installed Maven version and its configuration. If the installation is successful, you will see the version details, confirming Maven is ready for use.

Create a Test Apache Maven Project

Setting Up a New Maven Project

After installing Apache Maven on your Fedora Linux system, validate the installation by creating a test Maven project. Start by creating a new directory for the project and navigating to it in the terminal.

To generate a new Maven project, run:

mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command sets up a new Maven project with com.example as the group ID and my-project as the artifact ID using the maven-archetype-quickstart archetype. The -DinteractiveMode=false option instructs Maven to skip additional input requests during project creation.

Conclusion

With Apache Maven successfully installed on your Fedora system, you can streamline your Java development workflow and manage your projects efficiently. Using Fedora’s AppStream provides a quick and stable setup, while downloading the source archive gives you more flexibility in managing Maven versions. Regularly update Maven to ensure compatibility with the latest tools and libraries. Enjoy the robust project management and build capabilities that Apache Maven brings to your development environment.

Leave a Comment