maven的单继承和多继承

原地址一
原地址二

简化了一下方便自己记录

单继承的两种方式

方式一:

1、父模块 account-aggregator

只显示主要部分 pom.xml

  <groupId>com.youzhibing.account</groupId>
  <artifactId>account-aggregator</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Account Aggrregator</name>
  <modules>
      <!-- 子模块都写在此处 -->
      <module>account-register</module>
  </modules>
  
  <!-- 配置共有依赖 -->
  <dependencies> 
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
  </dependencies>
</project>

2、子模块account-register

  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.youzhibing.account</groupId>
    <artifactId>account-aggregator</artifactId>
    <version>1.0.0-SNAPSHOT</version>
     <!-- 与不配置一样,默认就是寻找上级目录下得pom.xml -->
    <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>account-register</artifactId>
  <name>account-register</name>
  <!-- 配置自己独有依赖 -->
  <dependencies>    
      <dependency>
          <groupId>javax.mail</groupId>
          <artifactId>mail</artifactId>
          <version>1.4.3</version>
      </dependency>
  </dependencies>

方式二:配置dependencyManagement

1、父模块 account-aggregator

只显示主要部分 pom.xml

  <groupId>com.youzhibing.account</groupId>
  <artifactId>account-aggregator</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Account Aggrregator</name>
  <modules>
      <!-- 子模块都写在此处 -->
      <module>account-register</module>
  </modules>
  
  <!-- 配置共有依赖 -->
  <dependencyManagement>
     <dependencies> 
	      <dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring-core</artifactId>
	        <version>4.0.2.RELEASE</version>
		  </dependency>
 	 </dependencies>
  </dependencyManagement>
</project>

2、子模块account-register

  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.youzhibing.account</groupId>
    <artifactId>account-aggregator</artifactId>
    <version>1.0.0-SNAPSHOT</version>
     <!-- 与不配置一样,默认就是寻找上级目录下得pom.xml -->
    <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>account-register</artifactId>
  <name>account-register</name>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
  </dependencies>

父POM使用dependencyManagement能够统一项目范围中依赖的版本,当依赖版本在父POM中声明后,子模块在使用依赖的时候就无须声明版本,也就不会发生多个子模块使用版本不一致的情况,帮助降低依赖冲突的几率。如果子模块不声明依赖的使用,即使该依赖在父POM中的dependencyManagement中声明了,也不会产生任何效果

多继承

1、父模块 maven-father

只显示主要部分 pom.xml

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cbm.stu</groupId>
    <artifactId>maven-father</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>maven-father</name>

   <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、父模块 maven-monther

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cbm.stu</groupId>
    <artifactId>maven-monther</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>maven-monther</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

3、子模块 maven-child

只显示主要部分 pom.xml

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cbm.stu</groupId>
    <artifactId>maven-parent-a</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>maven-parent-b</name>

    <dependencyManagement>
        <dependencies>
            <!-- 此处继承了father 和 monther 两个项目,type为pom,scope 为 import -->
            <dependency>
                <groupId>com.cbm.stu</groupId>
                <artifactId>maven-father</artifactId>
                <version>1.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.cbm.stu</groupId>
                <artifactId>maven-monther</artifactId>
                <version>1.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
	<dependencies>
        <!-- 从继承的父项目中继承依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
    </dependencies>

原文链接:maven的单继承和多继承_kingderember的博客-CSDN博客_maven单继承编写中sdafsdfsdfdfsdffhttps://blog.csdn.net/kingderember/article/details/84326176

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值