Download, upload and install maven under Linux

Posted by JoelyUK on Wed, 06 Nov 2019 23:47:19 +0100

To run maven, you must have a JDK, which is written in the Java language

I. installation of JDK

Download on ORACLE official websiteJDK jdk-8u231-linux-x64.tar.gz

Upload JDK to linux server

create folder

[root@VM_0_17_centos /]# cd /opt
[root@VM_0_17_centos opt]# mkdir jdk

Using xftp tool to upload compressed files

  • Connecting to a Linux server

  • If the folder permission is not enough, the file upload may fail, and the folder permission needs to be modified
# Modify opt folder permissions
chmod 777 /opt  
# Permission to modify the opt folder and its subdirectories
chmod -R 777 /opt  

Install JDK

Unzip the file after uploading successfully

[root@VM_0_17_centos jdk]# tar -zxvf jdk-8u231-linux-x64.tar.gz 
[root@VM_0_17_centos jdk]# ll
total 189608
drwxr-xr-x 7   10  143      4096 Oct  5 18:13 jdk1.8.0_231
-rw-r--r-- 1 root root 194151339 Nov  6 20:07 jdk-8u231-linux-x64.tar.gz

Configure JDK environment variables

Append the following lines to the end of the / etc/profile file

 [root@VM_0_17_centos jdk]# vi /etc/profile
# Enter vi mode, input two big G, and locate to the end of the file
  • Appended variable

Enter i to enter edit mode

export JAVA_HOME=/opt/jdk/jdk1.8.0_231
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

Press Esc, enter: wq save and exit vi

  • Make environment variables effective
[root@VM_0_17_centos jdk]# source /etc/profile
  • Check whether the JDK is effective
[root@VM_0_17_centos jdk]# java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

II. Installation of maven

Download maven Apache Maven 3.6.2

Download maven

  • linux uses wget command to download maven
[root@VM_0_17_centos opt]# mkdir maven
[root@VM_0_17_centos opt]# cd maven
[root@VM_0_17_centos maven]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz

Create a folder and switch to the directory. wget will download the file corresponding to the address to the directory

Install maven

  • Unzip the installation file
[root@VM_0_17_centos maven]# ls
apache-maven-3.6.2-bin.tar.gz
[root@VM_0_17_centos maven]# tar -zxvf apache-maven-3.6.2-bin.tar.gz 
  • Create a soft connection to the apache-maven-3.6.2 directory
[root@VM_0_17_centos maven]# ln -s apache-maven-3.6.2 maven
[root@VM_0_17_centos maven]# ll
total 8936
drwxr-xr-x 6 root root    4096 Nov  6 21:40 apache-maven-3.6.2
-rw-r--r-- 1 root root 9142315 Sep  3 05:43 apache-maven-3.6.2-bin.tar.gz
lrwxrwxrwx 1 root root      18 Nov  6 21:43 maven -> apache-maven-3.6.2

The target file of the ln – s source file is a command in linux. You know all the shortcuts in windows. This is equivalent to creating a shortcut for the source file. The name of the shortcut is called the target file.
Ln-S apache-maven-3.6.2 Maven is to create a shortcut Maven for apache-maven-3.6.2. Visiting Maven is equivalent to visiting apache-maven-3.6.2.
Why do I need shortcuts here?
It's more convenient to upgrade Maven in the future. If we need to upgrade Maven to the latest version, such as 3.7, we just need to download 3.7 to the current directory and run Ln-S apache-maven-3.7 Maven command to change the shortcut point. It's very convenient.

Configure maven environment

Append the following lines to the end of the / etc/profile file

export M2_HOME=/opt/maven/maven
export PATH=$M2_HOME/bin:$PATH
  • Make environment variables effective
[root@VM_0_17_centos maven]# source /etc/profile
  • Check whether maven is effective
[root@VM_0_17_centos maven]# mvn -v
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: /opt/maven/maven
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: /opt/jdk/jdk1.8.0_231/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.21.3.el7.x86_64", arch: "amd64", family: "unix"

Configure setting.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">


  <localRepository>/opt/maven/repository</localRepository>

  <pluginGroups>


  </pluginGroups>

  <proxies>

  </proxies>

  <servers>

  </servers>


  <mirrors>
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>ui</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://uk.maven.org/maven2/</url>
    </mirror>
    <mirror>
        <id>jboss-public-repository-group</id>
        <mirrorOf>central</mirrorOf>
        <name>JBoss Public Repository Group</name>
        <url>http://repository.jboss.org/nexus/content/groups/public</url>
    </mirror>
    <mirror>
        <id>repo2</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo2.maven.org/maven2/</url>
    </mirror>
    <mirror>
        <id>OSChina</id>
        <name>OSChina Central</name>
        <url>http://maven.oschina.net/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>nexus-osc-thirdparty</id>
        <mirrorOf>thirdparty</mirrorOf>
        <name>Nexus osc thirdparty</name>
        <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
    </mirror>
  </mirrors>


  <profiles>
    <profile>    
        <id>jdk-1.8</id>    
        <activation>    
            <activeByDefault>true</activeByDefault>    
            <jdk>1.8</jdk>    
        </activation>    
        <properties>    
            <maven.compiler.source>1.8</maven.compiler.source>    
            <maven.compiler.target>1.8</maven.compiler.target>    
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
        </properties>    
    </profile> 
  </profiles>


</settings>

Reference link: https://mp.weixin.qq.com/s/7lgakbjvqujiveugdse'aw

Topics: Java Maven JDK Apache Linux