Maven
- 理解分模块开发的意义
- 能够使用聚合工程快速构建项目
- 能够使用继承简化项目配置
- 能够根据需求配置生产、开发、测试环境,并在各环境间切换运行
# 分模块开发与设计
# 分模块开发的意义
- 能够使用聚合工程快速构建项目
- 能够使用继承简化项目配置
- 能够根据需要配置生产,开发,测试环境
目的:高内聚、低耦合
# 模块拆分原则
- 项目的扩展性增强,方便其他项目引用相同的功能
- 将原始模块按照功能拆分成若干个子模块,方便模块间相互调用,接口共享
# 模块拆分实现
# 环境准备
创建一个空项目
- 配置项目Maven
- 配置项目JDK
创建一个SpringBoot模块
- 将之前mybatisplus-01-quickstart模块中的代码拷贝过来,完整目录如下
- 修改pom.xml文件中的
groupid
、artifactId
,刷新Maven项目,运行之前编写的UserMapperTest
测试类
至此,我们的模块初始化工作就完成了,下面我们就来试着将该模块拆分为多个模块,拆分依据为:
注意事项
分模块开发一般是先针对模块功能进行设计,然后再行编码,不会想我们这样现将项目开发完成,然后再进行拆分,我们只是演示一下模块如何拆分,使用原来的模块可以方便我们演示也有助于大家理解。
# 拆分POJO模块
- 创建一个Maven模块,不适用骨架
- 将maven-02-service模块中
com.itheima.domain
包移动至maven-04-pojo模块对应的包下面
注意
将maven-02-service模块中的domain
包移动到maven-04-pojo模块后,两个模块都出现了编译错误。出现错误的原因有两个:
- maven-04-pojo模块没有引入Mybatis-Plus相关依赖坐标
- maven-02-service需要使用
User
类,但是现在被移走了,因此无法找到User
类
那么,我们下面就来看下如何解决上面的两个问题。
- 在maven-04-pojo模块中,引入所需的依赖坐标:mybatis-plus、lombok
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>maven-04-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
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
提示
此时,maven-04-pojo模块不再有编译错误。
- maven-02-service模块需要
User
类,现在User
在模块maven-04-pojo模块中,那么我们在maven-02-service模块的pom.xml文件中引入maven-04-pojo的坐标是否可行呢?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>maven-02-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven-04-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
提示
此时,maven-02-service模块也不再有编译错误,此时再次运行UserMapperTest
,依旧可以测试通过:
- 按照如上方法,我们继续将maven-02-service模块中的Dao层代码拆分出一个模块
提问
虽然,上面我们也完成了模块拆分,但是总觉得有点怪怪的把,这三个模块它们之间的关系到底是什么样的,又分别引入了哪些依赖坐标呢?
接下来,我们就通过学习Maven相关知识,优化我们的多模块结构。
# 依赖管理
依赖管理指当前项目运行所需的jar,一个项目可以设置多个依赖。
格式:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>maven-03-dao</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!--设置当前项目所依赖的所有jar-->
<dependencies>
<!--设置具体的依赖-->
<dependency>
<!--依赖所属群组id-->
<groupId>com.itheima</groupId>
<!--依赖所属项目id-->
<artifactId>maven-04-pojo</artifactId>
<!--依赖版本号-->
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies>
</project>
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
# 依赖传递
A依赖B,B依赖C,那么,A是否依赖于C呢?
依赖具有传递性:
- 直接依赖:在当前项目中通过依赖配置建立的依赖关系
- 简介依赖:被依赖的资源如果还依赖其他资源,那么当前项目也简介依赖这个资源
- 优先级:
- 当同级配置了相同资源的不同版本,后配置的覆盖先配置的
- 不同级,依赖层级越深,优先级越低
# 可选依赖
A依赖B,B依赖C,如果B不想让A知道它依赖了C,或者说B不想让A通过依赖传递方式自动依赖C,那么该如何做呢?
可选依赖指对外因此当前所依赖的资源--不透明
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven-04-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--可选依赖时隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递性-->
<optional>true</optional>
</dependency>
2
3
4
5
6
7
# 排除依赖
A依赖B,B依赖C,如果A不想将C依赖进来,该如何做?
排除依赖指主动断开依赖的传递,被排除的资源无需指定版本号,仅指定groupId(G)和artifactId(A)即可,无需指定version(V)
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven-03-dao</artifactId>
<version>1.0-SNAPSHOT</version>
<!--排除依赖指主动断开依赖的传递-->
<exclusions>
<exclusion>
<groupId>com.itheima</groupId>
<artifactId>maven-04-pojo</artifactId>
</exclusion>
</exclusions>
</dependency>
2
3
4
5
6
7
8
9
10
11
12
# 可选依赖和排除依赖的区别
# 聚合与继承
# 聚合工程
聚合:将多个模块组织成一个整体,同时进行项目构建的过程称为聚合。
聚合工程:通常是一个不具有业务功能的空工程(有且仅有一个pom.xml文件)
作用:使用聚合工程可以将多个工程编组,通过对聚合工程进行构建,实现对所包含模块进行同步构建,当工程中某个模块发送更新(变更)时,必须保障工程中与已更新模块有关联的模块进行同步更新,此时就可以使用聚合工程来解决批量同步构建模块的问题。
# 聚合工程开发
- 创建聚合工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>maven-00-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
</project>
2
3
4
5
6
7
8
9
10
11
12
注意
每个Maven工程或者模块都有对应的打包方式,默认为jar,Web工程打包方式为war。
- 设置当前聚合工程所包含的子模块路径和名称
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>maven-00-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!--设置模块的路径和名称-->
<modules>
<module>../maven-02-service</module>
<module>../maven-03-dao</module>
<module>../maven-04-pojo</module>
</modules>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
注意事项
- 聚合工程中所包含的模块在进行构建时会根据模块间的依赖关系设置构建顺序,与聚合工程中模块的书写位置无关
- 参与聚合的工程无法向上感知是否参与聚合,只能有聚合工程向下配置哪些模块参与聚合
# 继承关系
概念:继承描述的是两个工程间的关系,与Java中的继承相似,子工程可以继承父工程中的配置信息,常见于依赖关系的继承。
作用:
- 简化配置
- 减少版本冲突
# 继承关系开发
创建Maven模块,设置打包类型为pom,由于已经创建了一个聚合工程,在实际开发中聚合工程和父工程可以是同一个模块,所以就不在创建新模块了
在父工程的pom.xml文件中配置依赖关系,子工程将沿用父工程中的依赖关系
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>maven-00-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!--设置模块的路径和名称-->
<modules>
<module>../maven-02-service</module>
<module>../maven-03-dao</module>
<module>../maven-04-pojo</module>
</modules>
<!--配置父工程的依赖关系-->
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
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
54
55
56
57
58
59
60
61
62
- 在子工程中配置当前父工程,并使用父工程中的可选依赖坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.itheima</groupId>
<artifactId>maven-00-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../maven-00-parent/pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>maven-02-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--<dependency>-->
<!--<groupId>com.itheima</groupId>-->
<!--<artifactId>maven-04-pojo</artifactId>-->
<!--<version>1.0-SNAPSHOT</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven-03-dao</artifactId>
<version>1.0-SNAPSHOT</version>
<!--排除依赖指主动断开依赖的传递-->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>com.itheima</groupId>-->
<!--<artifactId>maven-04-pojo</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<!--子工程中使用父工程中的可选依赖时,仅需提供GA,无需提供V-->
<!--<version>3.5.3</version>-->
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<!--子工程中使用父工程中的可选依赖时,仅需提供GA,无需提供V-->
<!--<version>1.1.16</version>-->
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!--子工程中使用父工程中的可选依赖时,仅需提供GA,无需提供V-->
<!--<version>1.18.24</version>-->
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.itheima</groupId>
<artifactId>maven-00-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../maven-00-parent/pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>maven-03-dao</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven-04-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--可选依赖时隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递性-->
<!--<optional>true</optional>-->
</dependency>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<!--子工程中使用父工程中的可选依赖时,仅需提供GA,无需提供V-->
<!--<version>3.5.3</version>-->
</dependency>
</dependencies>
</project>
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>maven-04-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.itheima</groupId>
<artifactId>maven-00-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../maven-00-parent/pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<!--子工程中使用父工程中的可选依赖时,仅需提供GA,无需提供V-->
<!--<version>3.5.3</version>-->
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!--子工程中使用父工程中的可选依赖时,仅需提供GA,无需提供V-->
<!--<version>1.18.24</version>-->
<scope>provided</scope>
</dependency>
</dependencies>
</project>
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
注意实现
- 子工程中使用父工程中的可选依赖时,仅需要提供群组id和项目id,无需提供版本,版本由父工程统一提供,避免版本冲突
- 子工程中还可以定义父工程中没有定义的依赖关系
小结:聚合和继承的比较
- 作用:
- 聚合用于快速构建项目
- 继承用于快速配置
- 相同点
- 聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个pom.xml文件中
- 聚合与继承均属于设计型模块,并无实际的模块内容
- 不同点
- 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
- 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己
# 属性管理
# 属性
# 属性配置与使用
定义属性和引用属性:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
</parent>
<groupId>com.itheima</groupId>
<artifactId>maven-00-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!--设置模块的路径和名称-->
<modules>
<module>../maven-02-service</module>
<module>../maven-03-dao</module>
<module>../maven-04-pojo</module>
</modules>
<!--定义属性-->
<properties>
<mybatis.version>3.5.3</mybatis.version>
<druid.version>1.1.16</druid.version>
<lombok.version>1.18.24</lombok.version>
</properties>
<!--配置父工程的依赖关系-->
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!--引用属性-->
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- MyBaits-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<!--引用属性-->
<version>${mybatis.version}</version>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<!--引用属性-->
<version>${druid.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 资源文件引用属性
- 定义属性
<!--定义属性-->
<properties>
<mybatis.version>3.5.3</mybatis.version>
<druid.version>1.1.16</druid.version>
<lombok.version>1.18.24</lombok.version>
<jdbc.url>jdbc:mysql://localhost:3306/mp_db</jdbc.url>
</properties>
2
3
4
5
6
7
- 配置文件中引用在pom.xml文件中定义的属性
server:
port: 80
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 1234
# url: jdbc:mysql://localhost:3306/mp_db
url: @jdbc.url@
main:
banner-mode: off # 关闭SpringBoot启动图标
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false # 关闭MyBatis-Plus启动图标
db-config:
id-type: auto
table-prefix: tbl_
mapper-locations: classpath*:mapper/*.xml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- 开启资源文件目录加载属性的过滤器
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2
3
4
5
6
7
8
9
10
11
12
13
14
# 其他属性
属性列表:
- 自定义属性(常用)
- 内置属性
- Setting属性
- Java系统属性
- 环境变量属性
# 版本管理
# 工程版本
- SNAPSHOT(快照版本)
- 项目开发过程中临时输出的版本,称为快照版本
- 快照版本会随着开发的进程不断更新
- RELEASE(发布版本)
- 项目开发到一定阶段,完成阶段里程碑后,向团队外部发布较为稳定的版本,这种版本所对应的构建包是稳定的
- 即便进行功能的后续开发,也不会改变当前发布版本内容,这种版本称为发布版本
# 发布版本
- alpha版
- beta版
- 纯数字版
# 多环境配置与应用
# 多环境配置作用
Maven提供配置多种环境的设定,帮助开发者使用过程中快速切换环境。
# 多环境配置步骤
- 在pom.xml文件中定义多环境
<!--定义多环境-->
<profiles>
<!--定义具体的环境: 开发环境-->
<profile>
<!--定义环境对应的唯一名称-->
<id>dev</id>
<!--定义环境中专用的属性值-->
<properties>
<port>81</port>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--定义具体的环境:测试环境-->
<profile>
<id>test</id>
<properties>
<port>82</port>
</properties>
</profile>
</profiles>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 在application.yml文件中引用属性
server:
port: @port@
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 1234
# url: jdbc:mysql://localhost:3306/mp_db
url: @jdbc.url@
main:
banner-mode: off # 关闭SpringBoot启动图标
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false # 关闭MyBatis-Plus启动图标
db-config:
id-type: auto
table-prefix: tbl_
mapper-locations: classpath*:mapper/*.xml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- 构建过程使用多环境
# 命令
mvn 指令 -P 环境定义的id
# 范例
mvn install -P test
2
3
4
5
# 跳过测试
- 应用场景
在功能或者项目没有开发完成,需要快速出部署包时
- 跳过测试命令
mvn install -D skipTests
注意
执行的项目构建指令必须包含测试生命周期,否则无效过,例如执行compile
生命周期,本身是不经过test
生命周期的
- 也可以通过pom.xml文件提供更细粒度的控制
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
<!--设置跳过测试-->
<includes>
<!--包含指定的测试用例-->
<include>**/User*Test.java</include>
</includes>
<excludes>
<!--排除指定的测试用例-->
<exclude>**/User*TestCase.java</exclude>
</excludes>
</configuration>
</plugin
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 私服
# 私服介绍
Nexux私服是一台独立的服务器,用于解决团队内部的资源共享与资源同步问题。
Nexus:是Sonatype公司开发的一款Maven私服产品,下载地址:https://help.sonatype.com/repomanager3/download,但因为网络限制,很难下载下来,因此我们在课程资料中准备了Nexus的绿色安装包。
# Nexus安装与启动
将课程资料中的Nexus压缩包拷贝到无特殊字符非中文目录下解压。
- 启动服务器(命令行启动):
nexus.exe /run nexus
- 访问服务器(
http://localhost:8081
),默认端口:8081 - 修改基础配置信息:Nexus解压目录下etc目录中的nexus-default.properties文件保存有nexus基础配置信息,如默认访问端口等
- 修改nexus服务器运行配置信息:在bin目录下的nexus.vmoptions文件中保存有启动对应的配置信息,如默认占用内存空间等
提示
初次启动需要使用默认用户/密码,默认用户为:admin,默认密码存放位置有给提示,使用默认密码登录后,会提示修改密码。
注意
修改完默认密码后,会弹出一个提示框,在该提示框中注意选择允许匿名访问
# 私服资源操作流程
# 私服仓库分类
# 从私服中下载依赖
- 在Maven的settings.xml中的
<mirrors></mirrors>
标签中配置,此时需要注释掉aliyun相关的配置
<!-- <mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror> -->
<mirror>
<id>itheima</id>
<name>heima maven</name>
<url>http://localhost:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
2
3
4
5
6
7
8
9
10
11
12
- 在Nexus中设置允许匿名下载,如果不允许在不配置用户名/密码的情况下服务从私服中下载依赖
如果私服中没有对应的jar,那么私服将会去中央仓库下载,由于网络原因,速度会很慢,可以配置让私服去阿里云仓库中下载依赖
# 上传依赖到私服中
- 配置本地仓库访问私服的权限(在Maven的settings.xml标签中配置)
<server>
<!--id任意,多个server的id不重复揪心个,后面repository配置时会用到-->
<id>heima-nexus</id>
<username>admin</username>
<!--填写自己设定的nexus密码-->
<password>admin</password>
</server>
2
3
4
5
6
7
- 在nexus中创建两个仓库,一个Snapshot版本,一个Release版本
- 配置当前项目/模块上传到私服保存到位置(在项目的pom.xml文件中进行配置)
<distributionManagement>
<snapshotRepository>
<!--和maven/settings.xml中server中的id保持一致, 表示使用该id对应的用户名和密码-->
<id>heima-nexus</id>
<!--如果jar的版本是snapshot版本,那么就上传到这个仓库,根据自己情况修改-->
<url>http://localhost:8081/repository/heima-snapshots/</url>
</snapshotRepository>
<repository>
<!--和maven/settings.xml中server中的id保持一致, 表示使用该id对应的用户名和密码-->
<id>heima-nexus</id>
<!--如果jar的版本是release版本,那么就上传到这个仓库,根据自己情况修改-->
<url>http://localhost:8081/repository/heima-releases/</url>
</repository>
</distributionManagement>
2
3
4
5
6
7
8
9
10
11
12
13
14
- 发布资源到私服
mvn deploy