聚合和继承的作用?
- 作用
- 聚合用于快速构建项目
- 继承用于快速配置
- 相同点:
- 聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个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>
<groupId>org.example</groupId>
<artifactId>maven_01-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--聚合 做工程管理的,管理其他所有的-->
<packaging>pom</packaging>
<!--设置管理的模块名称-->
<modules>
<module>../maven_02-ssm1</module>
<module>../maven_03-pojo1</module>
<module>../maven_04-dao</module>
</modules>
<dependencies>
//继承 *****子工程都会用到的公共依赖
</dependencies>
<!--定义依赖管理-->
<dependencyManagement>
<!--写在这里的你想用,你需要写上你想用。部分工程想用的依赖,如-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--使用属性代理-->
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--属性管理-->
<properties>
<spring.version>5.2.10.RELEASE</spring.version>
<junit.version>4.13</junit.version>
<mybatis-spring.version>1.3.0</mybatis-spring.version>
<jdbc.url>jdbc:mysql://localhost:3306/ssm_db</jdbc.url>
</properties>
<!-- 扩大maven的管理范围-->
<build>
<resources>
<resource>
<!-- 告诉它这个资源文件里也可以使用上面这种属性格式 -->
<!--1. 资源文件的目录 -->
<!--F:\idea\codes\ssm\maven\mymaven\maven_02-ssm1\src\main\resources\jdbc.properties-->
<!--从当前目录往上找一层,在那一层里在寻找02-->
<!--<directory>../maven_02-ssm1/src/main/resources</directory>-->
<!--${project.basedir}当前项目所在的目录 其他的都是继承的这个所以可以找到其他的-->
<directory>${project.basedir}/src/main/resources</directory>
<!--2. 开启过滤规则,识别${}这种符号-->
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
子类pom.xml
,只举例一个,其他与此类似
注意是怎么解决打war包的时候如果没有web.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>org.example</groupId>
<artifactId>springmvc_11-page</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<!-- gav -->
<groupId>org.example</groupId>
<artifactId>maven_01-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 继承来自哪里-->
<relativePath>../maven_01-parent/pom.xml</relativePath>
</parent>
<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>
<!-- 打war包的时候如果没有web.xml 会失败 -->
<!-- 解决 -->
<plugin>
<!--只是告诉我们要编辑这个插件-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<!--写它的配置-->
<configuration>
<!--如果没有找到web.xml文件是否报错。装作没看到,不要报错-->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!--依赖domain运行-->
<dependency>
<groupId>org.example</groupId>
<artifactId>maven_03-pojo1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--依赖dao-->
<dependency>
<groupId>org.example</groupId>
<artifactId>maven_04-dao</artifactId>
<version>1.0-SNAPSHOT</version>
<!--排除依赖,隐藏当前对应的依赖关系-->
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>