步骤3: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>
步骤4:使用聚合统一管理项目
测试发现,当maven_01_parent的compile被点击后,所有被其管理的项目都会被执行编译操作。这就是聚合工程的作用。
说明:聚合工程管理的项目在进行运行的时候,会按照项目与项目之间的依赖关系来自动决定执行的顺序和配置的顺序无关。
聚合的知识我们就讲解完了,最后总结一句话就是,聚合工程主要是用来管理项目。
3.2 继承
我们已经完成了使用聚合工程去管理项目,聚合工程进行某一个构建操作,其他被其管理的项目也会执行相同的构建操作。那么接下来,我们再来分析下,多模块开发存在的另外一个问题,重复配置的问题,我们先来看张图:
spring-webmvc、spring-jdbc在三个项目模块中都有出现,这样就出现了重复的内容spring-test只在ssm_crm和ssm_goods中出现,而在ssm_order中没有,这里是部分重复的内容- 我们使用的spring版本目前是
5.2.10.RELEASE,假如后期要想升级spring版本,所有跟Spring相关jar包都得被修改,涉及到的项目越多,维护成本越高
面对上面的这些问题,我们就得用到接下来要学习的继承
- 所谓继承:描述的是两个工程间的关系,与java中的继承相似,子工程可以继承父工程中的配置信息,常见于依赖关系的继承。
- 作用:
- 简化配置
- 减少版本冲突
接下来,我们到程序中去看看继承该如何实现?
步骤1:创建一个空的Maven项目并将其打包方式设置为pom
因为这一步和前面maven创建聚合工程的方式是一摸一样,所以我们可以单独创建一个新的工程,也可以直接和聚合公用一个工程。实际开发中,聚合和继承一般也都放在同一个项目中,但是这两个的功能是不一样的。
步骤2:在子项目中设置其父工程
分别在maven_02_ssm,maven_03_pojo,maven_04_dao的pom.xml中添加其父项目为maven_01_parent
<!--配置当前工程继承自parent工程--> <parent> <groupId>com.itheima</groupId> <artifactId>maven_01_parent</artifactId> <version>1.0-RELEASE</version> <!--设置父项目pom.xml位置路径--> <relativePath>../maven_01_parent/pom.xml</relativePath> </parent>
步骤3:优化子项目共有依赖导入问题
- 将子项目共同使用的jar包都抽取出来,维护在父项目的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> <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包删除掉
<?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_02_ssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <!--配置当前工程继承自parent工程--> <parent> <groupId>com.itheima</groupId> <artifactId>maven_01_parent</artifactId> <version>1.0-RELEASE</version> <relativePath>../maven_01_parent/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.itheima</groupId> <artifactId>maven_04_dao</artifactId> <version>1.0-SNAPSHOT</version> <!--排除依赖是隐藏当前资源对应的依赖关系--> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
删除完后,你会发现父项目中有依赖对应的jar包,子项目虽然已经将重复的依赖删除掉了,但是刷新的时候,子项目中所需要的jar包依然存在。
当项目的标签被移除掉,会发现多出来的jar包依赖也会随之消失。
- 将
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_04_dao</artifactId> <version>1.0-SNAPSHOT</version> <!--配置当前工程继承自parent工程--> <parent> <groupId>com.itheima</groupId> <artifactId>maven_01_parent</artifactId> <version>1.0-RELEASE</version> <relativePath>../maven_01_parent/pom.xml</relativePath> </parent> </project>
刷新并查看Maven的面板,会发现maven_04_dao同样引入了父项目中的所有依赖。
<?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_dao</artifactId> <version>1.0-SNAPSHOT</version> <!--配置当前工程继承自parent工程--> <parent> <groupId>com.itheima</groupId> <artifactId>maven_01_parent</artifactId> <version>1.0-RELEASE</version> <relativePath>../maven_01_parent/pom.xml</relativePath> </parent> </project>
这样我们就可以解决刚才提到的第一个问题,将子项目中的公共jar包抽取到父工程中进行统一添加依赖,这样做的可以简化配置,并且当父工程中所依赖的jar包版本发生变化,所有子项目中对应的jar包版本也会跟着更新。
步骤4:优化子项目依赖版本问题
如果把所有用到的jar包都管理在父项目的pom.xml,看上去更简单些,但是这样就会导致有很多项目引入了过多自己不需要的jar包。如上面看到的这张图:
如果把所有的依赖都放在了父工程中进行统一维护,就会导致ssm_order项目中多引入了spring-test的jar包,如果这样的jar包过多的话,对于ssm_order来说也是一种"负担"。
那针对于这种部分项目有的jar包,我们该如何管理优化呢?
- 在父工程mavne_01_parent的pom.xml来定义依赖管理
<!--定义依赖管理--> <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
刷新完会发现,在maven_02_ssm项目中的junit依赖并没有出现,所以我们得到一个结论:
标签不真正引入jar包,而是配置可供子项目选择的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>
这个时候,maven_02_ssm和maven_04_dao这两个项目中的junit版本就会跟随着父项目中的标签dependencyManagement中junit的版本发生变化而变化。不需要junit的项目就不需要添加对应的依赖即可。
至此继承就已经学习完了,总结来说,继承可以帮助做两件事
- 将所有项目公共的jar包依赖提取到父工程的pom.xml中,子项目就可以不用重复编写,简化开发
- 将所有项目的jar包配置到父工程的dependencyManagement标签下,实现版本管理,方便维护
- dependencyManagement标签不真正引入jar包,只是管理jar包的版本
- 子项目在引入的时候,只需要指定groupId和artifactId,不需要加version
- 当dependencyManagement标签中jar包版本发生变化,所有子项目中有用到该jar包的地方对应的版本会自动随之更新
最后总结一句话就是,父工程主要是用来快速配置依赖jar包和管理项目中所使用的资源。
小结
继承的实现步骤:
- 创建Maven模块,设置打包类型为pom
<packaging>pom</packaging>
- 在父工程的pom文件中配置依赖关系(子工程将沿用父工程中的依赖关系),一般只抽取子项目中公有的jar包
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.10.RELEASE</version> </dependency> ... </dependencies>
- 在父工程中配置子工程中可选的依赖关系
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> </dependencies> ... </dependencyManagement>
- 在子工程中配置当前工程所继承的父工程
<!--定义该工程的父工程--> <parent> <groupId>com.itheima</groupId> <artifactId>maven_01_parent</artifactId> <version>1.0-RELEASE</version> <!--填写父工程的pom文件,可以不写--> <relativePath>../maven_01_parent/pom.xml</relativePath> </parent>
- 在子工程中配置使用父工程中可选依赖的坐标
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> </dependencies>
注意事项:
1.子工程中使用父工程中的可选依赖时,仅需要提供群组id和项目id,无需提供版本,版本由父工程统一提供,避免版本冲突
2.子工程中还可以定义父工程中没有定义的依赖关系,只不过不能被父工程进行版本统一管理。
3.3 聚合与继承的区别
3.3.1 聚合与继承的区别
两种之间的作用:
- 聚合用于快速构建项目,对项目进行管理
- 继承用于快速配置和管理子项目中所使用jar包的版本
聚合和继承的相同点:
- 聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个pom文件中
- 聚合与继承均属于设计型模块,并无实际的模块内容
聚合和继承的不同点:
- 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
- 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己
相信到这里,大家已经能区分开什么是聚合和继承,但是有一个稍微麻烦的地方就是聚合和继承的工程构建,需要在聚合项目中手动添加modules标签,需要在所有的子项目中添加parent标签,万一写错了咋办?
3.3.2 IDEA构建聚合与继承工程
其实对于聚合和继承工程的创建,IDEA已经能帮助我们快速构建,具体的实现步骤为:
步骤1:创建一个Maven项目
创建一个空的Maven项目,可以将项目中的src目录删除掉,这个项目作为聚合工程和父工程。
步骤2:创建子项目
该项目可以被聚合工程管理,同时会继承父工程。
创建成功后,maven_parent即是聚合工程又是父工程,maven_web中也有parent标签,继承的就是maven_parent,对于难以配置的内容都自动生成。
按照上面这种方式,大家就可以根据自己的需要来构建分模块项目。
4,属性
在这一章节内容中,我们将学习两个内容,分别是
- 属性
- 版本管理
属性中会继续解决分模块开发项目存在的问题,版本管理主要是认识下当前主流的版本定义方式。
4.1 属性
4.1.1 问题分析
讲解内容之前,我们还是先来分析问题:
前面我们已经在父工程中的dependencyManagement标签中对项目中所使用的jar包版本进行了统一的管理,但是如果在标签中有如下的内容:
你会发现,如果我们现在想更新Spring的版本,你会发现我们依然需要更新多个jar包的版本,这样的话还是有可能出现漏改导致程序出问题,而且改起来也是比较麻烦。
问题清楚后,我们需要解决的话,就可以参考咱们java基础所学习的变量,声明一个变量,在其他地方使用该变量,当变量的值发生变化后,所有使用变量的地方,就会跟着修改,即:
4.1.2 解决步骤
步骤1:父工程中定义属性
<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>
步骤2:修改依赖的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标签中所维护的jar包版本,所有子项目中的版本也就跟着更新。当然除了将spring相关版本进行维护,我们可以将其他的jar包版本也进行抽取,这样就可以对项目中所有jar包的版本进行统一维护,如:
<!--定义属性--> <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>
4.2 配置文件加载属性
Maven中的属性我们已经介绍过了,现在也已经能够通过Maven来集中管理Maven中依赖jar包的版本。但是又有新的需求,就是想让Maven对于属性的管理范围能更大些,比如我们之前项目中的jdbc.properties,这个配置文件中的属性,能不能也来让Maven进行管理呢?
答案是肯定的,具体的实现步骤为:
步骤1:父工程定义属性
<properties> <jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url> </properties>
步骤2:jdbc.properties文件中引用属性
在jdbc.properties,将jdbc.url的值直接获取Maven配置的属性
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=${jdbc.url} jdbc.username=root jdbc.password=root
步骤3:设置maven过滤文件范围
Maven在默认情况下是从当前项目的src\main\resources下读取文件进行打包。现在我们需要打包的资源文件是在maven_02_ssm下,需要我们通过配置来指定下具体的资源目录。
<build> <resources> <!--设置资源目录--> <resource> <directory>../maven_02_ssm/src/main/resources</directory> <!--设置能够解析${},默认是false --> <filtering>true</filtering> </resource> </resources> </build>
说明:directory路径前要添加../的原因是maven_02_ssm相对于父工程的pom.xml路径是在其上一层的目录中,所以需要添加。
修改完后,注意maven_02_ssm项目的resources目录就多了些东西,如下:









