分模块开发
分模块开发设计
(1)按照功能拆分
(2)按照模块拆分
- 将原始模块按照功能拆分成若干个子模块,方便模块间的相互调用,接口共享。
分模块开发实现
抽取domain层
创建新模块
- 创建一个名称为maven_03_pojo的jar项目,为什么项目名是从02到03这样创建,原因后面我们会提到,这块的名称可以任意。
项目中创建domain包
- 在maven_03_pojo项目中创建com.itheima.domain包,并将maven_02_ssm中Book类拷贝到该包中
删除原项目中的domain包
-
删除后,maven_02_ssm项目中用到Book的类中都会有红色提示,如下:
image.png
建立依赖关系
- 在maven_02_ssm项目的pom.xml添加maven_03_pojo的依赖
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven_03_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
将项目安装本地仓库
-
将需要被依赖的项目maven_03_pojo,使用maven的install命令,把其安装到Maven的本地仓库中。
image.png
依赖管理
- 依赖传递
- 可选依赖
- 排除依赖
依赖传递与冲突问题
image.png

image.png
-
比较大的区别就是有的依赖前面有箭头>,有的依赖前面没有。
image.png 在Dependencies中移除自己所添加maven_03_pojo依赖后,打开BookServiceImpl的类,你会发现Book类依然存在,可以被正常使用
依赖是具有传递性的:这里所说的依赖冲突是指项目依赖的某一个jar包,有多个不同的版本,因而造成类包版本冲突。情况一: 在maven_02_ssm的pom.xml中添加两个不同版本的Junit依赖:
情况一: 在maven_02_ssm的pom.xml中添加两个不同版本的Junit依赖:
特殊优先:当同级配置了相同资源的不同版本,后配置的覆盖先配置的。
情况二: 路径优先:当依赖中出现相同的资源时,层级越深,优先级越低,层级越浅,优先级越高
A通过B间接依赖到E1A通过C间接依赖到E2A就会间接依赖到E1和E2,Maven会按照层级来选择,E1是2度,E2是3度,所以最终会选择E1情况三: 声明优先:当资源在相同层级被依赖时,配置顺序靠前的覆盖配置顺序靠后的
A通过B间接依赖到D1A通过C间接依赖到D2D1和D2都是两度,这个时候就不能按照层级来选择,需要按照声明来,谁先声明用谁,也就是说B在C之前声明,这个时候使用的是D1,反之则为D2
可选依赖和排除依赖
可选依赖
- 可选依赖指对外隐藏当前所依赖的资源---不透明在maven_04_dao的pom.xml,在引入maven_03_pojo的时候,添加optional
<dependency>2 <groupId>com.itheima</groupId>3 <artifactId>maven_03_pojo</artifactId>4 <version>1.0-SNAPSHOT</version>5 <!--可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递-->6 <optional>true</optional>7 </dependency>
- 此时BookServiceImpl就已经报错了,说明由于maven_04_dao将maven_03_pojo设置成可选依赖,导致maven_02_ssm无法引用到maven_03_pojo中的内容,导致Book类找不到。
方案二:排除依赖
- 排除依赖指主动断开依赖的资源,被排除的资源无需指定版本---不需要
<dependency>2 <groupId>com.itheima</groupId>3 <artifactId>maven_04_dao</artifactId>4 <version>1.0-SNAPSHOT</version>5 <!--排除依赖是隐藏当前资源对应的依赖关系-->6 <exclusions>7 <exclusion>8 <groupId>com.itheima</groupId>9 <artifactId>maven_03_pojo</artifactId>10 </exclusion>11 </exclusions>12 </dependency>
聚合

image.png
- 所谓聚合:将多个模块组织成一个整体,同时进行项目构建的过程称为聚合- - 聚合工程:通常是一个不具有业务功能的"空"工程(有且仅有一个pom文件)
- 作用:使用聚合工程可以将多个工程编组,通过对聚合工程进行构建,实现对所包含的模块进行同步构建
创建一个空的maven项目
将项目的打包方式改为pom
<?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_01_parent</artifactId>
<version>1.0-RELEASE</version>
<packaging>pom</packaging>
</project>
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_01_parent</artifactId>
<version>1.0-RELEASE</version>
<packaging>pom</packaging>
<!--设置管理的模块名称-->
<modules>
<module>../maven_02_ssm</module>
<module>../maven_03_pojo</module>
<module>../maven_04_dao</module>
</modules>
</project>
- 聚合工程管理的项目在进行运行的时候,会按照项目与项目之间的依赖关系来自动决定执行的顺序和配置的顺序无关。
继承
- 所谓继承:描述的是两个工程间的关系,与java中的继承相似,子工程可以继承父工程中的配置信息,常见于依赖关系的继承。
创建一个空的Maven项目并将其打包方式设置为pom
在子项目中设置其父工程
- 分别在maven_02_ssm , maven_03_pojo , maven_04_dao的pom.xml中添加其父项目为maven_01_parent
优化子项目共有依赖导入问题
<?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_01_parent</artifactId>
<version>1.0-RELEASE</version>
<packaging>pom</packaging>
<!--设置管理的模块名称-->
<modules>
<module>../maven_02_ssm</module>
<module>../maven_03_pojo</module>
<module>../maven_04_dao</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
删除子项目中已经被抽取到父项目的pom.xml中的jar包,如在maven_02_ssm的pom.xml中将已经出现在父项目的jar包删除掉
优化子项目依赖版本问题
- 如果把所有用到的jar包都管理在父项目的pom.xml,看上去更简单些,但是这样就会导致有很多项目引入了过多自己不需要的jar包。
<!--定义依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
将maven_02_ssm的pom.xml中的junit依赖删除掉,刷新Maven
- <dependencyManagement>标签不真正引入jar包,而是配置可供子项目选择的jar包依赖
在maven_02_ssm的pom.xml添加junit的依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
- 这里就不需要添加版本了,这样做的好处就是当父工程dependencyManagement标签中的版本发生变化后,子项目中的依赖版本也会跟着发生变化
在maven_04_dao的pom.xml添加junit的依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
属性

image.png
解决步骤
父工程中定义属性
<properties>
<spring.version>5.2.10.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<mybatis-spring.version>1.3.0</mybatis-spring.version>
</properties>
修改依赖的version
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
配置文件加载属性
- 父工程定义属性
<properties>
<jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
</properties>
- jdbc.properties文件中引用属性
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=root
jdbc.password=root
- 设置maven过滤文件范围
<build>
<resources>
<!--设置资源目录-->
<resource>
<directory>../maven_02_ssm/src/main/resources</directory>
<!--设置能够解析${},默认是false -->
<filtering>true</filtering>
</resource>
</resources>
</build>
- 上面的属性管理就已经完成,但是有一个问题没有解决,因为不只是maven_02_ssm项目需要有属性被父工程管理,如果有多个项目需要配置,该如何实现呢
方式一
<build>
<resources>
<!--设置资源目录,并设置能够解析${}-->
<resource>
<directory>../maven_02_ssm/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>../maven_03_pojo/src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
</build>
方式二
<build>
<resources>
<!--
${project.basedir}: 当前项目所在目录,子项目继承了父项目,
相当于所有的子项目都添加了资源目录的过滤
-->
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

image.png
- 原因就是Maven发现你的项目为web项目,就会去找web项目的入口web.xml[配置文件配置的方式],发现没有找到,就会报错。
- 解决方案1:在maven_02_ssm项目的src\main\webapp\WEB-INF\添加一个web.xml文件
- 解决方案2: 配置maven打包war时,忽略web.xml检查
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

image.png
版本管理
- SNAPSHOT(快照版本)
项目开发过程中临时输出的版本,称为快照版本
快照版本会随着开发的进展不断更新 - RELEASE(发布版本)
项目开发到一定阶段里程碑后,向团队外部发布较为稳定的版本,这种版本所对应的构件文件是稳定的
即便进行功能的后续开发,也不会改变当前发布版本内容,这种版本称为发布版本
多环境配置与应用
多环境开发
image.png
- maven提供配置多种环境的设定,帮助开发者在使用过程中快速切换环境。具体实现步骤:
父工程配置多个环境,并指定默认激活环境
<profiles>
<!--开发环境-->
<profile>
<id>env_dep</id>
<properties>
<jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
</properties>
<!--设定是否为默认启动环境-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--生产环境-->
<profile>
<id>env_pro</id>
<properties>
<jdbc.url>jdbc:mysql://127.2.2.2:3306/ssm_db</jdbc.url>
</properties>
</profile>
<!--测试环境-->
<profile>
<id>env_test</id>
<properties>
<jdbc.url>jdbc:mysql://127.3.3.3:3306/ssm_db</jdbc.url>
</properties>
</profile>
</profiles>
执行安装查看env_dep环境是否生效
切换默认环境为生产环境
<profiles>
<!--开发环境-->
<profile>
<id>env_dep</id>
<properties>
<jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>env_pro</id>
<properties>
<jdbc.url>jdbc:mysql://127.2.2.2:3306/ssm_db</jdbc.url>
</properties>
<!--设定是否为默认启动环境-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>env_test</id>
<properties>
<jdbc.url>jdbc:mysql://127.3.3.3:3306/ssm_db</jdbc.url>
</properties>
</profile>
</profiles>
执行安装并查看env_pro环境是否生效
命令行实现环境切换

image.png
跳过测试
- 可以确保每次打包或者安装的时候,程序的正确性,假如测试已经通过在我们没有修改程序的前提下再次执行打包或安装命令,由于顺序执行,测试会被再次执行,就有点耗费时间了。
- 功能开发过程中有部分模块还没有开发完毕,测试无法通过,但是想要把其中某一部分进行快速打包,此时由于测试环境失败就会导致打包失败。
- 遇到上面这些情况的时候,我们就想跳过测试执行下面的构建命令,具体实现方式有很多:
方式一:IDEA工具实现跳过测试

image.png
- 图中的按钮为Toggle 'Skip Tests' Mode ,
- Toggle翻译为切换的意思,也就是说在测试与不测试之间进行切换。
方式二:配置插件实现跳过测试
- 在父工程中的pom.xml中添加测试插件配置
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>false</skipTests>
<!--排除掉不参与测试的内容-->
<excludes>
<exclude>**/BookServiceTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
- skipTests:如果为true,则跳过所有测试,如果为false,则不跳过测试
- excludes:哪些测试类不参与测试,即排除,针对skipTests为false来设置的
- includes: 哪些测试类要参与测试,即包含,针对skipTests为true来设置的
方式三:命令行跳过测试

image.png



