SoFunction
Updated on 2025-04-06

Share the Maven installation tutorial for CentOS system

Maven is a popular project management tool that helps developers manage project construction, reporting and documentation software project management tools.

Installing Maven in a CentOS system is a relatively simple process, just follow the steps below.

Preparation

Before you start installing, you need to make sure your CentOS system has Java installed, because Maven is Java-based.

You can runjava -versionCommand to check if Java is installed. If not installed, you need to install Java first.

In addition, you will also need to make sure your system has an Internet connection in order to download the Maven installation package from Maven's official website.

Download and install Maven

First, visit (Maven's official website) and download the latest version of Maven installation package. You can choose to download the compressed package in the format.

usewgetCommand or download the Maven installation package directly from the browser to your CentOS system.

For example, if you are using the wget command, you can run the following command to download the Maven installation package:

wget /maven/maven-3/3.8.4/binaries/apache-maven-3.8.

After the download is completed, usetarCommand to unzip the Maven installation package to the specified directory.

For example, you can unzip Maven to/optIn the directory:

sudo tar -xvzf apache-maven-3.8. -C /opt

After decompression is complete, you need to set environment variables so that you can use the Maven command anywhere.

You can edit/etc/profileFile to set environment variables.

Open with a text editor/etc/profileFile, and add the following at the end of the file:

export M2_HOME=/opt/apache-maven-3.8.4
export PATH=$PATH:$M2_HOME/bin

Save and close/etc/profiledocument. Then, runsource /etc/profileCommand to make environment variables take effect.

runmvn -versionCommand to verify that Maven has been installed successfully. If everything works fine, you should be able to see the version information of Maven.

Frequently Asked Questions and Solutions

You may encounter some problems during the installation of Maven.

Here are some common problems and their solutions:

  1. Java not installed: If the system prompts that Java is not installed, you need to install Java first. You can useyum install java-1.8.0-openjdkCommand to install OpenJDK.
  2. Environment variable setting error: If you are runningmvnI received an error of "Command Not Found" when I was commanded, which may be because the environment variable is not set correctly. Check, please/etc/profileSet environment variables in the file and make sureM2_HOMEThe variable points to the correct Maven installation directory.
  3. Slow download speed: If you encounter slow downloading speed when downloading the installation package from the official Maven website, you can try using a domestic mirror site to download the Maven installation package.

Practical application cases

Now, you have successfully installed Maven in CentOS. As a practical application case, you can try to build a Java project using Maven.

Here are one simple step:

  • Create a new Java project directory and create a new Java project name under that directoryMaven project file. You can use a text editor to edit this file and add some basic Maven configuration.
  • existIn the file, you can define project dependencies, build plugins, and other configurations. For example, you can add a JUnit dependency to add unit test support to your project.
  • saveAfter the file, you can run itmvn packageCommands to build your project. Maven will automatically download the dependencies required by the project and compile and package your project.
  • After the build is completed, you cantargetFind the generated JAR file or WAR file in the directory. You can use this file to deploy your application to the server.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.