Installing and Configuring Maven on Linux Systems
Maven and JDK Version Compatibility
Reference the Apache archive for downloading: https://archive.apache.org/dist/maven/
Maven versions have specific Java version requirements:
- Maven 2.0.11 and earlier support JDK 1.3 and JDK 1.4
- Maven 2.0.11 and later support JDK 1.5 and above
- Maven 3.0+ requires JDK 1.7 or higher
- Maven 3.3+ requires JDK 1.8
- Maven 3.5.0+ supports JDK 9
- Maven 3.5.3+ supports JDK 10
- Maven 3.5.4+ supports JDK 11
- Maven 3.6.0+ supports JDK 12
- Maven 3.6.1+ supports JDK 13
- Maven 3.6.2+ supports JDK 14
- Maven 3.6.3+ supports JDK 15
- Maven 3.8.1+ suports JDK 16
- Maven 3.8.3+ supports JDK 17
Newer JDK versions can compile code for older targets, but not vice versa. For instance, running Maven with JDK 17 can produce binaries for versions 17, 11, 8, etc., but an older JDK cannot generate outputs for newer Java versions.
Maven Installation
# Create necessary directories
mkdir -p /opt/soft
mkdir -p /usr/local/maven
# Download Maven archive
cd /opt/soft
wget https://archive.apache.org/dist/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
# Extract and move
tar -xzf apache-maven-3.8.6-bin.tar.gz
mv apache-maven-3.8.6 /usr/local/maven/current
# Configure environment variables
cat > /etc/profile.d/maven.sh << 'EOF'
export MAVEN_HOME=/usr/local/maven/current
export PATH=$PATH:$MAVEN_HOME/bin
EOF
# Apply environment changes
chmod +x /etc/profile.d/maven.sh
source /etc/profile
Maven Configuration
Edit the settings.xml file:
vim /usr/local/maven/current/conf/settings.xml
Replace the content with the folllowing configuration:
<?xml version="1.0" encoding="UTF-8"?>
<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
http://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- Local repository path -->
<localRepository>/var/maven/repository</localRepository>
<servers>
</servers>
<!-- Mirror repositories -->
<mirrors>
<mirror>
<id>central-mirror</id>
<name>Aliyun Maven Repository</name>
<url>https://maven.aliyun.com/repository/central</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
</profiles>
<activeProfiles>
</activeProfiles>
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>
<proxies>
</proxies>
</settings>
Configuration explanations:
<localRepository>: Defines where Maven stores downloaded dependencies. The default location is~/.m2/repository<mirrors>: Configures repository mirrors to accelerate dependency downloads or replace the default central repository<proxies>: Sets proxy server configuration for network-restricted environments<servers>: Stores authentication credentials for remote repositories<profiles>: Defines build profiles with different repository and plugin configurations<activeProfiles>: Activates specific build profiles<pluginGroups>: Specifies plugin groups to avoid full plugin coordinates in POM files
Verification
Verify Maven installation:
mvn -version
Expected output:
Apache Maven 3.8.6 (e9bc3c3155e93b24f4ce4b2a8b8f8a7a0a5b3c4d; 2022-05-15T15:21:45+08:00)
Maven home: /usr/local/maven/current
Java version: 17.0.3, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: Linux, version: 5.10.0-1234.el8.x86_64, arch: amd64, family: unix