1. The environment of this article
-
jdk1.8
-
maven3.6.2
We want to write java code, we need to install jdk, then we need to use maven, similarly, we need to install maven in our machine.
2. Install maven in linux
Maven is written in java language, so if we want to run maven, we need to install jdk first.
2.1, install jdk
2.1.1. Download jdk
This time we install jdk1.8, you can go to the oracle official website to download jdk-8u181-linux-x64.tar.gz, and put it in the /opt/jdk directory, as follows:
[root@ady01 jdk]# cd /opt/jdk/
[root@ady01 jdk]# ll
total 181300
-rw-r--r-- 1 root root 185646832 Nov 1 13:30 jdk-8u181-linux-x64.tar.gz
2.1.2, unzip jdk
[root@ady01 jdk]# tar -zvxf jdk-8u181-linux-x64.tar.gz
[root@ady01 jdk]# ll
total 181304
drwxr-xr-x 7 10 143 4096 Jul 7 2018 jdk1.8.0_181
-rw-r--r-- 1 root root 185646832 Nov 1 13:30 jdk-8u181-linux-x64.tar.gz
2.1.3, configure environment variables
Append the following lines at the end of the /etc/profile file
export JAVA_HOME=/opt/jdk/jdk1.8.0_181
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
This place expands knowledge points:
Let's talk about a knowledge point. When the system runs java or other external commands, how does the system find these commands?
In linux, it will search for this command in all the directories corresponding to PATH, and it can be run directly if it is found. If PATH is not set, we need to know the full path of the command to run it, so it is more convenient to use PATH.
In the window, there is also a system variable PATH. The value of this PATH is composed of the addresses of many directories. When we execute a command, the system will search for the command we run in all the directories corresponding to PATH. If we find it, we can directly Run, for example, if you want to quickly start some other software, you can set these software into the PATH variable, and you can quickly start it in the cmd command.
2.1.4. Run the following command to make the environment variable take effect
[root@ady01 jdk1.8.0_181]# source /etc/profile
2.1.5. Verify that jdk is normal
Check jdk version
[root@ady01 jdk]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
Create a new /opt/jdk/HelloWorld.java with the following content:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello maven!");
}
}
Run the command below:
[root@ady01 jdk]# cd /opt/jdk/
[root@ady01 jdk]# javac HelloWorld.java
[root@ady01 jdk]# java HelloWorld
hello maven!
恭喜,输出hello maven表示正常,jdk安装成功!
2.2. Install maven
2.2.1. Download maven
Let's go to the maven official website to download the latest maven, the address is as follows:
https://maven.apache.org/download.cgi
The version we use is apache-maven-3.6.2, we need to download apache-maven-3.6.2-bin.tar.gz
this in linux.
[root@ady01 jdk]# mkdir /opt/maven
[root@ady01 jdk]# cd /opt/maven/
[root@ady01 maven]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
--2019-11-01 13:47:11-- http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9142315 (8.7M) [application/octet-stream]
Saving to: ‘apache-maven-3.6.2-bin.tar.gz’
100%[==================================================================================================================================>] 9,142,315 10.2MB/s in 0.9s
2019-11-01 13:47:13 (10.2 MB/s) - ‘apache-maven-3.6.2-bin.tar.gz’ saved [9142315/9142315]
[root@ady01 maven]# ls
apache-maven-3.6.2-bin.tar.gz
Above we created the /opt/maven directory to store maven-related software, and then used the wget command, which is a command in linux, which can access an http address and download it to the current directory.
2.2.2, unzip maven
[root@ady01 maven]# tar -zvxf apache-maven-3.6.2-bin.tar.gz
[root@ady01 maven]# ls
apache-maven-3.6.2 apache-maven-3.6.2-bin.tar.gz
2.2.3, maven directory structure
[root@ady01 maven]# ll /opt/maven/maven/
total 40
drwxr-xr-x 2 root root 4096 Nov 1 13:49 bin
drwxr-xr-x 2 root root 4096 Nov 1 13:49 boot
drwxrwxr-x 3 mysql mysql 4096 Aug 27 23:01 conf
drwxrwxr-x 4 mysql mysql 4096 Nov 1 13:49 lib
-rw-rw-r-- 1 mysql mysql 12846 Aug 27 23:09 LICENSE
-rw-rw-r-- 1 mysql mysql 182 Aug 27 23:09 NOTICE
-rw-rw-r-- 1 mysql mysql 2533 Aug 27 23:01 README.txt
bin: store executable files
conf: store maven configuration files
lib: maven is written in java, and many third-party jar packages are used in it, and these jar packages are located in lib
2.2.4. Create a soft link pointing to the apache-maven-3.6.2 directory
[root@ady01 maven]# ln -s apache-maven-3.6.2 maven
[root@ady01 maven]# ll
total 8936
drwxr-xr-x 6 root root 4096 Nov 1 13:49 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 1 13:56 maven -> apache-maven-3.6.2
ln –s 源文件 目标文件
It is a command in linux. You all know the shortcuts in windows. This is equivalent to源文件
creating a shortcut for you. The name of the shortcut is called目标文件
.
ln -s apache-maven-3.6.2 maven
It means thatapache-maven-3.6.2
a shortcut is createdmaven
, and accessmaven
is equivalent to accessapache-maven-3.6.2
.Why is a shortcut needed here?
It will be more convenient to upgrade maven in the future. If we need to upgrade maven to the latest version, such as 3.7, then we only need to download 3.7 to the current directory, and then run the command to modify the direction of the shortcut, which is very convenient
ln -s apache-maven-3.7 maven
.
2.2.5. Configure maven environment variables
Append the following lines at the end of the /etc/profile file
export M2_HOME=/opt/maven/maven
export PATH=$M2_HOME/bin:$PATH
2.2.6. Run the following command to make the environment variable take effect
[root@ady01 maven]# source /etc/profile
2.2.7. Verify that maven is normal
[root@ady01 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_181, vendor: Oracle Corporation, runtime: /opt/jdk/jdk1.8.0_181/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.2.2.el7.x86_64", arch: "amd64", family: "unix"
mvn -v
Output the version number information of maven. If the output is similar to the above, then congratulations, maven is installed successfully!
3. Windows installs maven
3.1, install jdk
Window installation jdk is not repeated here
3.2. Install maven
3.2.1, download maven3.6.2
下载地址:http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
3.2.2. Decompression
Put it in the D:\installsoft\maven directory, as shown below
3.2.3, configure environment variables
Create a new environment variable M2_HOME
with the value:
D:\installsoft\maven\apache-maven-3.6.2
Modify the environment variable PATH and add it to the value of PATH%M2_HOME%\bin
3.2.4. Verify that maven is normal
C:\Users\Think>mvn -v
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: D:\installsoft\maven\apache-maven-3.6.2\bin\..
Java version: 1.8.0_121, vendor: Oracle Corporation, runtime: D:\installsoft\Java\jdk1.8.0_121\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
mvn -v
Output the version number information of maven. If the output is similar to the above, then congratulations, maven is installed successfully!
4. Detailed explanation of the operating principle of Maven
This symbol will be used later in this article
~
. Let me explain this symbol first. This symbol represents the directory of the current user.By default in window
C:\Users\用户名
The linux root user is in
/root
the directory by default, and the ~ of other users corresponds to/home/用户名
In the following articles, we will use ~ to represent the user directory, and this place will not be explained again.
Run the following command to see the effect
C:\Users\Think>mvn help:system
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom (4.7 kB at 4.0 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/12/maven-plugins-12.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/12/maven-plugins-12.pom (12 kB at 21 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/9/maven-parent-9.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/9/maven-parent-9.pom (33 kB at 44 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.jar (24 kB at 12 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom (15 kB at 13 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/16/maven-plugins-16.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/16/maven-plugins-16.pom (13 kB at 33 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.jar (209 kB at 29 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.pom (11 kB at 14 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.jar
Progress (1): 153 kB
mvn help:system
After running the command above , it seems that https://repo.maven.apache.org
a lot of things are being downloaded from the site, and finally the information of all the environment variables of the system is output.
Let's take a closer look at mvn help:system
the running process of this command:
mvn help:system
after runningThe system will look for the mvn command in all the directories corresponding to the environment variable PATH, and then
D:\installsoft\maven\apache-maven-3.6.2\bin
find the executablemvn
file inRun the mvn file, that is, execute the mvn command
Usually, when some software starts, there will be a startup configuration file, and maven also has it. When the mvn command starts, it will go to the
~/.m2
directory to find the configuration filesettings.xml
. This file is the startup configuration file of the mvn command. Where to put it, etc.), if the file~/.m2
cannot be found in the directorysettings.xml
, it will go toM2_HOME/conf
the directory to find the configuration file, and then run the maven programThe mvn command is followed by a parameter:
help:sytem
, what does this mean? This means to run the plugin, and then send a commandhelp
to the help pluginsystem
Maven checks the local cache directory (the default is
~/.m2
the directory) to find if there is a help plugin, if there is no local, continue to the following stepsMaven will go to a default site (a website [repo.maven.apache.org] provided by apache for maven, this is called the central warehouse) to download the help plugin to the
~/.m2
directoryRun the help plug-in, and then send
system
instructions to the help plug-in.system
After the help plug-in receives the command, it outputs the information of the local environment variables. If the system cannot find the specified plug-in or sends an unrecognized command to the plug-in, an error will be reported.
Let's experience the above process again. All commands in maven are provided in the form of plug-ins, so maven extension is also quite easy.
4.1, mvn plugin name: help
The above will output the help document of the plug-in, let's feel one:
C:\Users\Think>mvn clean:help
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ standalone-pom ---
[INFO] org.apache.maven.plugins:maven-clean-plugin:2.5
Maven Clean Plugin
The Maven Clean Plugin is a plugin that removes files generated at build-time
in a project's directory.
This plugin has 2 goals:
clean:clean
Goal which cleans the build.
This attempts to clean a project's working directory of the files that were
generated at build-time. By default, it discovers and deletes the directories
configured in project.build.directory, project.build.outputDirectory,
project.build.testOutputDirectory, and project.reporting.outputDirectory.
Files outside the default may also be included in the deletion by configuring
the filesets tag.
clean:help
Display help information on maven-clean-plugin.
Call
mvn clean:help -Ddetail=true -Dgoal=<goal-name>
to display parameter details.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.372 s
[INFO] Finished at: 2019-11-01T17:59:04+08:00
[INFO] Final Memory: 15M/487M
[INFO] ------------------------------------------------------------------------
These will be described in detail later.
5. Some configurations of Maven
5.1. Startup file settings
As mentioned above mvn
, when running, the startup configuration file will be loaded settings.xml
. This file is in M2_HOME/conf
the directory by default. Generally, we will copy one and put it ~/.m2
in the directory. The former is a global configuration file. All users on the entire machine will be affected by this configuration. The latter is user-wide level, only the current user will be affected by the configuration. It is recommended to use the user-level configuration and put it in ~/.m2
the directory instead of using the global configuration, so as not to affect the use of other users. It is also convenient to use in the future to upgrade the maven version. In general, we do not need to touch the entire installation directory of maven. When upgrading, we only need to replace the installation file, which is very convenient.
5.2, configure the local cache directory
There is a tag in settings.xml localRepository
, which can set the local cache directory. The plug-ins downloaded by maven from the remote warehouse and all the jar packages we will use in the future will be placed in this directory, as follows:
<localRepository>C:/Users/Think/.m1/repository</localRepository>
6. Summary
-
Master the installation process of maven
-
~
Indicates the current user directory -
The maven configuration file settings.xml is generally placed
~/.m2
in the directory to facilitate the upgrade of maven and avoid affecting the configuration of other users -
Understand the execution process of the mvn command