Maven初学了解
# Maven 基础
pom.xml
文件
# 坐标
- groupId:这是工程组的标识。它在一个组织或者项目中通常是唯一的。
- artifactId:这是工程的标识。它通常是工程的名称。groupId 和 artifactId 一起定义了 artifact 在仓库中的位置。
- version:这是工程的版本号,用来区分不同的版本。
# 常用命令
mvn -v
:查看 Maven 的版本,也用来检查 Maven 是否安装成功mvn compile
:编译,将 Java 源文件编异成 class 文件mvn test
:执行 test 目录下的测试用例mvn package
:打包,将 Java 工程打包成 jar 包mvn clean
:清理环境,清除 target 文件夹mvn install
:安装,将当前项目安装到 Maven 的本地仓库中
# Maven 相关特性
# 传递依赖
如果我们的项目引用了一个
jar
包,而该jar
包又引用了其他的jar
包。那么,在默认情况下,项目编译时,Maven 会把直接引用和间接 引用的jar
包都下载到本地(~/.m2/repository
)
# 传递依赖
如果我们只想下载直接引用的
jar
包,那么需要在pom.xml
文件中做如下配置(给出需要排除的坐标)
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>0.9417</version>
<exclustions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclustions>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 依赖冲突
若项目中多个
jar
同时引用了相同的jar
时,会产生依赖冲突,但 Maven 采用了两种避免冲突的策略,因此在 Maven 中是不存在依赖冲突的。
短路优先
本项目 -> A.jar -> B.jar -> X.jar 本项目 -> C.jar -> X.jar 使用最短的
1
2
3
4声明优先
若引用路径长度相同时,在
pom.xml
中谁先被声明,就使用谁。
# 多模块项目/聚合
PROJECT
|
|-PROJECT-MODEL
| |
| |- SRC
| |- POM (JAR)
|
|
|
|-PROJECT-DAO
| |
| |- SRC
| |- POM (JAR)
|
|
|-PROJECT-SERVICE
| |
| |- SRC
| |- POM (JAR)
|
|-PROJECT-WEB
| |
| |- SRC
| |- POM (JAR)
|
|
|
|- POM.XML(POM)
|
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
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
父模块
pom
文件的配置:packaging
类型必须是pom
<groupId>com.xxx.xxx</groupId> <artifactI>xxx.xxx</artifactI> <packaging>pom</packaging> <version>0.0.1-SNAPSHOT</version>
1
2
3
4聚合子模块:使用
modules
标签<modules> <module>xxx-common</module> <module>xxx-template</module> <module>xxx-settlement</module> <module>xxx-distribution</module> </modules>
1
2
3
4
5
6父模块统一管理依赖包:使用
dependencyManagement
标签<!-- 标识 SpringCloud 的版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
1
2
3
4
5
6
7
8
9
10
11
12子模块需要在
pom
中声明父模块:使用parent
标签<parent> <artifactId>xxx-coupon</artifactId> <groupId>com.xxx.coupon</groupId> <version>0.0.1-SNAPSHOT</version> </parent>
1
2
3
4
5
编辑 (opens new window)
上次更新: 2021/11/04, 23:54:18