Maven高级-----分模块开发、聚合与继承,父类pom文件配置原理

聚合和继承的作用?

  • 作用
    • 聚合用于快速构建项目
    • 继承用于快速配置
  • 相同点:
    • 聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个pom文件中
    • 聚合与继承均属于设计型模块,并无实际的模块内容
  • 不同点:
    • 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
    • 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己
    • 聚合加载顺序及原因:
      image-20220318154627856

代码&&原理解释

父类文件 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱代码的猿猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值