How To Install Apache Maven on CentOS Stream 10
Apache Maven stands as one of the most powerful build automation tools in the Java ecosystem, enabling developers to streamline project management and build processes. For CentOS Stream 10 users, installing and configuring Maven properly ensures a robust development environment. This comprehensive guide walks you through every step of installing Apache Maven on CentOS Stream 10, from preparation to verification and beyond.
Understanding Apache Maven
Apache Maven revolutionizes Java project management by providing a standardized approach to building, testing, and deploying applications. At its core, Maven operates on the concept of a Project Object Model (POM), represented by an XML file that describes project dependencies, build configurations, and plugins.
Unlike traditional build tools, Maven embraces “convention over configuration,” offering a predefined project structure and build lifecycle that drastically reduces configuration overhead. The build lifecycle consists of phases such as validate, compile, test, package, verify, install, and deploy—each representing a stage in the software development process.
Maven’s dependency management system is particularly noteworthy. It automatically resolves and downloads required libraries from repositories, eliminating the need to manually track and update dependencies. This repository system is hierarchical:
- Local repository: Located on your machine, storing downloaded artifacts
- Central repository: Maven’s official repository containing thousands of libraries
- Remote repositories: Custom repositories specified in your project configuration
When compared to alternatives like Gradle or Ant, Maven offers exceptional standardization and a mature ecosystem. While Gradle provides more flexibility with its Groovy-based DSL and Ant offers fine-grained control, Maven’s declarative approach and comprehensive repository system make it an excellent choice for enterprise Java development on CentOS Stream 10.
Prerequisites for Installation
Before diving into the Maven installation process, ensuring your CentOS Stream 10 system meets all requirements will help avoid potential roadblocks.
System Requirements:
Your CentOS Stream 10 installation should meet these minimum specifications:
- 2 GHz dual-core processor (or better)
- 2 GB RAM (4 GB recommended for larger projects)
- 250 MB free disk space for Maven installation
- Additional space for the local repository (starts small but grows with usage)
User Permissions:
Maven installation requires administrative privileges. You’ll need either:
- Root access
- A user account with sudo privileges
To verify your sudo capabilities, run:
sudo -v
If successful, you’ll be prompted for your password. If unsuccessful, contact your system administrator for appropriate permissions.
Java Requirements:
Maven 3.9+ requires Java 8 or higher. The latest Maven versions work optimally with JDK 11 or newer. Before proceeding, check if Java is already installed:
java -version
If Java isn’t installed or needs updating, we’ll cover this in the following section.
Network Connectivity:
Maven requires internet access to download dependencies from remote repositories. Ensure your system can access external resources, particularly:
- maven.apache.org
- repo.maven.apache.org
- Central Maven repositories
If working behind a corporate firewall, you may need to configure proxy settings later in the installation process.
Pre-installation System Check:
Run the following system checks:
# Check disk space
df -h
# Check memory resources
free -m
# Verify network connectivity
ping -c 4 repo.maven.apache.org
With these prerequisites verified, your CentOS Stream 10 system is ready for Maven installation.
Step 1: Updating Your CentOS Stream 10 System
Starting with an updated system ensures compatibility and security. CentOS Stream 10, being a rolling-release distribution leading to RHEL, benefits significantly from regular updates.
First, update the package repositories and upgrade installed packages:
sudo dnf check-update
sudo dnf upgrade -y
The `-y` flag automatically confirms all prompts, streamlining the update process. This command updates your package database and installs available upgrades.
Check your CentOS Stream version to confirm you’re working with the correct distribution:
cat /etc/centos-release
You should see output indicating CentOS Stream 10.
Verify system information, particularly architecture details:
uname -m
Most systems will show `x86_64` for 64-bit architecture, which is ideal for Maven installation.
Before proceeding, restart your system if kernel updates were applied:
sudo systemctl reboot
Keeping your CentOS Stream 10 system updated follows best practices for Linux administration and creates a solid foundation for development tools like Maven.
Step 2: Installing Java Development Kit
Maven requires a Java Development Kit (JDK) to function properly. For CentOS Stream 10, OpenJDK provides a fully compatible, open-source implementation with excellent integration.
Comparing Java Options:
While Oracle JDK offers commercial support, OpenJDK is fully featured and free. For Maven on CentOS Stream 10, OpenJDK 11 or 17 is recommended:
- OpenJDK 11: Long-term support with wide compatibility
- OpenJDK 17: Newer features with long-term support (recommended)
Installing OpenJDK 17:
To install OpenJDK 17 on CentOS Stream 10, execute:
sudo dnf install java-17-openjdk java-17-openjdk-devel -y
The `java-17-openjdk-devel` package includes development tools necessary for Maven operation.
Verifying Java Installation:
Confirm the installation was successful:
java -version
javac -version
You should see output indicating OpenJDK 17 (or your chosen version).
Setting up JAVA_HOME:
Maven requires the JAVA_HOME environment variable. First, locate your Java installation path:
dirname $(dirname $(readlink -f $(which java)))
This typically returns something like `/usr/lib/jvm/java-17-openjdk-17.0.x.x-x.x.x.el9_x.x86_64`.
Set the JAVA_HOME environment variable system-wide by creating a configuration file:
sudo tee /etc/profile.d/java_home.sh << 'EOF'
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$JAVA_HOME/bin:$PATH
EOF
Apply the changes:
source /etc/profile.d/java_home.sh
Verify the setting:
echo $JAVA_HOME
With Java properly installed and configured, your CentOS Stream 10 system is ready for Maven installation.
Step 3: Downloading Apache Maven
Obtaining the latest stable version of Maven ensures access to the newest features and security updates. As of writing, Maven 3.9.5 is the latest release, but check the official Maven website for more recent versions.
Finding the Latest Maven Release:
Visit the Apache Maven Project website or use the following command to check the Maven download page:
curl -s https://maven.apache.org/download.cgi | grep -oP 'apache-maven-\d+\.\d+\.\d+' | sort -u | head -1
This command helps identify the current version available.
Downloading Maven:
Use `wget` to download the Maven binary package:
cd /tmp
wget https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz
Alternatively, you can use `curl`:
cd /tmp
curl -O https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz
Verifying File Integrity:
Download the checksum file and verify the integrity of your download:
wget https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz.sha512
sha512sum -c apache-maven-3.9.5-bin.tar.gz.sha512
You should see output confirming the file is valid.
Understanding Maven Versioning:
Maven follows semantic versioning with three numbers (X.Y.Z):
- X: Major version with significant changes
- Y: Minor version with new features but backward compatible
- Z: Patch version with bug fixes
For production environments, it’s advisable to use the latest stable release rather than alpha, beta, or milestone releases.
Step 4: Extracting and Installing Maven
With the Maven package downloaded, the next step is to extract and install it in an appropriate location on your CentOS Stream 10 system.
Selecting an Installation Directory:
The conventional location for Maven installation is `/opt/maven` or `/usr/local/maven`. We’ll use `/opt/maven` in this guide:
sudo mkdir -p /opt/maven
Extracting the Maven Archive:
Extract the downloaded archive to the installation directory:
sudo tar -xzf /tmp/apache-maven-3.9.5-bin.tar.gz -C /opt/maven/
Creating a Symbolic Link:
To facilitate future upgrades, create a symbolic link to the Maven installation:
sudo ln -s /opt/maven/apache-maven-3.9.5 /opt/maven/latest
This approach allows you to:
- Install multiple Maven versions side by side
- Switch between versions by updating the symbolic link
- Upgrade Maven without modifying environment variables
Understanding Maven’s Directory Structure:
After installation, explore Maven’s directory structure:
ls -la /opt/maven/latest
Key directories include:
- `bin/`: Contains executable files including `mvn`
- `boot/`: Contains the class loader framework
- `conf/`: Contains configuration files, notably `settings.xml`
- `lib/`: Contains Maven’s JAR files
Setting Appropriate Permissions:
Ensure proper permissions are set:
sudo chmod +x /opt/maven/latest/bin/*
With Maven extracted and properly set up, you’re now ready to configure the environment variables.
Step 5: Configuring Environment Variables
Proper environment configuration ensures Maven operates correctly on your CentOS Stream 10 system. Two key variables need configuration: M2_HOME and PATH.
Setting up M2_HOME:
Create a dedicated configuration file for Maven:
sudo tee /etc/profile.d/maven.sh << 'EOF'
# Maven environment setup
export M2_HOME=/opt/maven/latest
export PATH=${M2_HOME}/bin:${PATH}
EOF
The `M2_HOME` variable points to your Maven installation. Using `/opt/maven/latest` (our symbolic link) means the configuration remains valid even after upgrading Maven.
Making Environment Changes Persistent:
Apply the changes to your current session:
source /etc/profile.d/maven.sh
These changes take effect automatically for new sessions after this configuration.
User-Specific vs. System-Wide Configuration:
The approach above applies Maven configuration system-wide. For user-specific configuration, edit `~/.bashrc` or `~/.bash_profile`:
echo 'export M2_HOME=/opt/maven/latest' >> ~/.bashrc
echo 'export PATH=${M2_HOME}/bin:${PATH}' >> ~/.bashrc
source ~/.bashrc
This user-specific approach is particularly useful in multi-user environments where different users require different Maven versions.
Testing Environment Configuration:
Verify the environment variables are set correctly:
echo $M2_HOME
echo $PATH | grep maven
The output should show the Maven installation path and confirm Maven’s bin directory is in your PATH.
Common Configuration Pitfalls:
- Missing executable permissions on Maven binary files
- Conflicting Maven installations in your PATH
- Incorrect JAVA_HOME configuration
- Shell configuration precedence issues
If you’re using a non-bash shell like zsh or fish, adapt the configuration files appropriately.
Step 6: Verifying the Maven Installation
Confirming your Maven installation works correctly is essential before proceeding with development projects.
Checking Maven Version:
Run the version check command:
mvn -v
This should display:
- Maven version (e.g., 3.9.5)
- Java version and vendor
- Java home directory
- Operating system information
If this command produces errors, review the environment variable configuration and Java installation.
Understanding Version Output:
The version output confirms:
- Maven is properly installed and accessible
- Maven is using the correct Java version
- System environment is recognized correctly
Testing Basic Maven Functionality:
Create a simple test to verify Maven’s core functionality:
mkdir -p ~/maven-test
cd ~/maven-test
mvn archetype:generate -DgroupId=com.example -DartifactId=maven-test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This creates a basic Maven project structure. If successful, Maven is properly installed and functional.
Checking Maven Configuration:
Review Maven’s configuration file:
cat $M2_HOME/conf/settings.xml
The default configuration is sufficient for most users, but you might need to customize it for specific needs (proxies, repositories, etc.).
A successful verification indicates your CentOS Stream 10 system is ready for Maven-based development.
Creating Your First Maven Project
With Maven successfully installed, let’s create a real project to demonstrate its capabilities.
Generating a Project with Maven Archetypes:
Maven archetypes are project templates that establish standardized project structures. The quickstart archetype creates a simple Java application:
mkdir -p ~/maven-projects
cd ~/maven-projects
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-first-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false
Understanding the Generated Project Structure:
Explore the created project:
cd my-first-app
ls -la
This creates a standard Maven project structure:
- `src/main/java/`: Source code directory
- `src/test/java/`: Test code directory
- `pom.xml`: Project Object Model file
Working with the POM File:
The POM file is Maven’s primary configuration file. Examine it:
cat pom.xml
Key elements include:
- Project coordinates (groupId, artifactId, version)
- Dependencies
- Build configuration
- Plugins
Building and Testing Your Project:
Run basic Maven commands to build and test your project:
# Compile the project
mvn compile
# Run tests
mvn test
# Package the application into a JAR file
mvn package
# Install the package into the local repository
mvn install
Each command corresponds to a phase in Maven’s lifecycle.
Running Your Application:
After packaging, run your application:
java -cp target/my-first-app-1.0-SNAPSHOT.jar com.example.App
The output should display “Hello World!” from the generated application.
Importing Into IDEs:
Most modern IDEs support Maven projects directly:
- IntelliJ IDEA: File > Open > Select pom.xml
- Eclipse: File > Import > Maven > Existing Maven Projects
- VS Code: Open folder and install Maven extension
With your first project created, you’ve successfully verified Maven’s functionality on your CentOS Stream 10 system.
Managing Maven Repositories and Dependencies
Maven’s repository system is central to its dependency management capabilities. Understanding how to work with repositories enhances your development workflow.
Repository Types:
Maven uses three repository types:
- Local repository: Located at `~/.m2/repository`, stores downloaded artifacts
- Central repository: Maven’s official repository (repo.maven.apache.org)
- Remote repositories: Additional repositories specified in your POM or settings
Configuring Custom Repositories:
To add a custom repository, edit your project’s `pom.xml`:
<repositories>
<repository>
<id>company-repository</id>
<url>https://repo.company.com/maven2</url>
</repository>
</repositories>
For system-wide configuration, modify `$M2_HOME/conf/settings.xml` or `~/.m2/settings.xml`.
Dependency Management Best Practices:
Follow these guidelines for effective dependency management:
- Specify version numbers explicitly
- Regularly update dependencies for security patches
- Use dependency scopes appropriately (compile, test, provided, runtime)
- Consider using a Bill of Materials (BOM) for version coordination
Resolving Dependency Conflicts:
Maven uses a “nearest definition” algorithm for resolving version conflicts. To check for conflicts:
mvn dependency:tree
To enforce a specific version:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>
Working with Proxies:
If your CentOS Stream 10 system is behind a corporate proxy, configure Maven accordingly in `settings.xml`:
<proxies>
<proxy>
<id>company-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.company.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>proxypass</password>
<nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
</proxy>
</proxies>
Effective repository management ensures your Maven projects build reliably and consistently on your CentOS Stream 10 system.
Advanced Maven Configuration
As you become more comfortable with Maven on CentOS Stream 10, explore advanced configuration options to enhance your workflow.
Customizing settings.xml:
The `settings.xml` file controls Maven’s global behavior. Common customizations include:
<settings>
<!-- Local repository location -->
<localRepository>/custom/path/to/repo</localRepository>
<!-- Default build profiles -->
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
<!-- Server credentials -->
<servers>
<server>
<id>company-repository</id>
<username>username</username>
<password>password</password>
</server>
</servers>
</settings>
Working with Maven Profiles:
Profiles allow environment-specific configurations:
<profiles>
<profile>
<id>development</id>
<properties>
<db.url>jdbc:mysql://localhost/dev_db</db.url>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<db.url>jdbc:mysql://prod-server/prod_db</db.url>
</properties>
</profile>
</profiles>
Activate profiles using:
mvn compile -P production
Plugin Configuration:
Customize plugin behavior in your POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
Build Performance Optimization:
Improve Maven build performance on CentOS Stream 10:
# Parallel builds
mvn -T 4 clean install
# Offline mode for faster builds
mvn -o clean install
# Skip tests for quick builds
mvn clean install -DskipTests
Multi-Module Projects:
For complex applications, organize code into modules:
parent-project/ ├── pom.xml ├── module1/ │ └── pom.xml ├── module2/ │ └── pom.xml └── module3/ └── pom.xml
The parent POM lists all modules:
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
Build all modules with a single command:
mvn clean install
These advanced configurations enhance Maven’s functionality on your CentOS Stream 10 system.
Troubleshooting Common Installation Issues
Even with careful installation, you might encounter issues with Maven on CentOS Stream 10. Here are solutions to common problems:
PATH and Environment Variable Problems:
If `mvn` command isn’t found:
# Check if Maven is in PATH
echo $PATH | grep maven
# Verify symbolic link
ls -la /opt/maven/latest
# Check permissions
ls -la /opt/maven/latest/bin
# Try with absolute path
/opt/maven/latest/bin/mvn -v
Java Compatibility Issues:
Maven requires compatible Java versions:
# Check Java version
java -version
# Ensure JAVA_HOME is set correctly
echo $JAVA_HOME
# Verify Java executable location
which java
For version conflicts, ensure Maven uses the correct Java version by setting JAVA_HOME properly.
Network and Repository Access Problems:
For connection issues:
# Test connectivity
ping -c 4 repo.maven.apache.org
# Check DNS resolution
nslookup repo.maven.apache.org
# Try with verbose output
mvn -X clean
For corporate networks, configure proxy settings in `settings.xml`.
Permission Problems:
Fix permission issues:
# Check file ownership
ls -la /opt/maven/
# Fix ownership if needed
sudo chown -R root:root /opt/maven/
# Fix permissions
sudo chmod -R 755 /opt/maven/
Memory Issues:
For OutOfMemoryError exceptions:
# Increase Maven memory
export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=256m"
Add this to your environment configuration for persistence.
Log Analysis:
Enable detailed logging for troubleshooting:
mvn -X clean install > maven-debug.log 2>&1
Analyze the log file for specific error messages and resolution hints.
Most Maven issues on CentOS Stream 10 stem from environment configuration, Java compatibility, or network access. Systematic troubleshooting usually resolves these problems quickly.
Upgrading and Maintaining Maven
As new Maven versions are released, keeping your installation updated ensures access to the latest features and security patches.
Upgrading to a Newer Version:
Leverage the symbolic link approach for easy upgrades:
# Download the new version
cd /tmp
wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
# Extract to the installation directory
sudo tar -xzf /tmp/apache-maven-3.9.6-bin.tar.gz -C /opt/maven/
# Update the symbolic link
sudo ln -sfn /opt/maven/apache-maven-3.9.6 /opt/maven/latest
# Verify the new version
mvn -v
This approach preserves your environment configuration while updating the Maven version.
Managing Multiple Maven Versions:
For testing or compatibility reasons, maintain multiple versions:
# Use a specific version temporarily
export M2_HOME=/opt/maven/apache-maven-3.8.8
export PATH=${M2_HOME}/bin:${PATH}
Cleaning Up Old Installations:
Periodically remove outdated versions:
# List installed versions
ls -la /opt/maven/
# Remove old version
sudo rm -rf /opt/maven/apache-maven-3.8.6
Backup and Restore:
Back up critical Maven configurations:
# Backup settings.xml
cp $M2_HOME/conf/settings.xml ~/maven-settings-backup.xml
# Backup local repository (optional - can be large)
tar -czf ~/maven-repo-backup.tar.gz ~/.m2/repository
Security Best Practices:
- Keep Maven updated to address security vulnerabilities
- Use HTTPS for repository connections
- Store sensitive repository credentials in encrypted settings
- Regular system updates on your CentOS Stream 10 system
With proper maintenance, your Maven installation on CentOS Stream 10 will remain a reliable component of your development infrastructure.
Congratulations! You have successfully installed Apache Maven. Thanks for using this tutorial for installing Apache Maven on CentOS Stream 10 system. For additional help or useful information, we recommend you check the official Apache Maven website.