Maven安装与配置

2021/10/10 Maven

# 安装

https://maven.apache.org/download.cgi (opens new window) 下载压缩包,解压即可。

tar -xzf apache-maven-3.8.4-bin.tar.gz
1

# 配置环境变量

M2_HOME = Maven解压目录

Path 添加 %M2_HOME%\bin

vi /etc/profile

M2_HOME=/usr/local/apache-maven-3.8.4 
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$M2_HOME/bin
export PATH CLASSPATH JAVA_HOME M2_HOME

source /etc/profile
1
2
3
4
5
6
7

# 测试安装是否成功

检测 Maven 是否安装配置成功,在 cmd 窗口输入:

mvn -v
1

# conf/settings.xml

<settings>
    <!-- 配置本地仓库 -->
    <localRepository>D:\Maven\repository</localRepository>
    <!-- 配置镜像 -->
    <!-- https://developer.aliyun.com/mvn/guide -->
	<mirrors>
		<mirror>
			<id>aliyun-central</id>
			<name>aliyun-central</name>
			<url>https://maven.aliyun.com/repository/central</url>
			<mirrorOf>central</mirrorOf>
		</mirror>
		<mirror>
			<id>aliyun-public</id>
			<name>aliyun-public</name>
			<url>https://maven.aliyun.com/repository/public</url>
			<mirrorOf>public</mirrorOf>
		</mirror>
		<mirror>
			<id>aliyun-google</id>
			<name>aliyun-google</name>
			<url>https://maven.aliyun.com/repository/google</url>
			<mirrorOf>google</mirrorOf>
		</mirror>
		<mirror>
			<id>aliyun-spring</id>
			<name>aliyun-spring</name>
			<url>https://maven.aliyun.com/repository/spring</url>
			<mirrorOf>spring</mirrorOf>
		</mirror>
		<mirror>
			<id>aliyun-spring-plugin</id>
			<name>aliyun-spring-plugin</name>
			<url>https://maven.aliyun.com/repository/spring-plugin</url>
			<mirrorOf>spring-plugin</mirrorOf>
		</mirror>
	</mirrors>
    <profiles>
        <!-- 修改Maven默认的JDK版本 -->
		<profile>
			<id>jdk-1.8</id>
			<activation>
				<jdk>1.8</jdk>
				<activeByDefault>true</activeByDefault>
			</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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

# pom

optional 是 maven 依赖 jar 时的一个选项,表示该依赖是可选的,项目之间依赖不传递。可以理解为此功能/此依赖可选,如果不需要某项功能,可以不引用这个包。不设置optional或者optional设置为false,表示依赖传递。

scope 是 maven 依赖 jar 时的一个选项,用于指定该依赖的作用范围和依赖传递。

可选值 作用范围 依赖传递 例子
compile 在编译、测试、运行时均有效,不指定 scope 时默认为 compile
provided 在编译、测试时有效,但是在运行时无效。
相当于compile,但是在打包阶段做了exclude的动作。
理论上可以参与编译、测试、运行等周期。
可以理解为此包不由我直接提供,需要调用者/容器提供。
lombok
runtime 在运行、测试时有效,但是在编译时无效
test 只在测试时有效 junit

# mvn

参数 说明 例子
-Pxxx 激活 id 为 xxx 的 profile (如有多个,用逗号隔开)
-Dxxx=yyy 指定系统属性 mvn package -Dmaven.test.skip=true
mvn spring-boot:run -Dspring-boot.run.profiles=dev,local
-U 强制更新 snapshot 类型的插件或依赖库
否则 maven 一天只会更新一次 snapshot 依赖

综合示例:

mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
1