maven多个子项目和父项目之间的引用问题

举报
DevFeng 发表于 2021/12/24 21:36:14 2021/12/24
【摘要】 原因分析在Maven中,parent模块组织好childA和childB,叫做"聚合"。多个模块联合编译实现起来很简单,按照以下方式即可:在parent的pom文件里加入以下内容:<modelVersion>4.0.0</modelVersion><groupId>com.demo</groupId><artifactId>parent</artifactId><version>1.0</v...

原因分析

在Maven中,parent模块组织好childA和childB,叫做"聚合"。多个模块联合编译实现起来很简单,按照以下方式即可:

  1. 在parent的pom文件里加入以下内容:
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>parent</artifactId>
    <version>1.0</version>
    <modules>
       <module>childA</module>
       <module>childB</module>
    </modules>

  2. 在childA和childB的pom文件中添加相应的标签来标记父模块。
    • childA:
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.demo</groupId>
      <artifactId>childA</artifactId>
      <version>1.0</version>
      <parent>
         <groupId>com.demo</groupId>
         <artifactId>parent</artifactId>
         <version>1.0</version>
      </parent>

    • childB:
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.demo</groupId>
      <artifactId>childB</artifactId>
      <version>1.0</version>
      <parent>
         <groupId>com.demo</groupId>
         <artifactId>parent</artifactId>
         <version>1.0</version>
      </parent>

在上述的配置形式中指定了一个父项目,下面有两个同级的子项目A和B,如果A项目的pom文件中把B项目当做自己的子项目来引用或者把parent项目来作为子项目就会引起冲突,构建时就是出现上面的报错。

处理办法

检查项目的pom的引用情况 ,如果真的要B项目作为A的子项目,则需要从parent的pom中把B项目的引用去掉,把B项目的父标签指向A项目,变成下面的这种形式:

  • parent项目:
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>parent</artifactId>
    <version>1.0</version>
    <modules>
       <module>childA</module>
    </modules>

  • A项目:
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>childA</artifactId>
    <version>1.0</version>
    <parent>
       <groupId>com.demo</groupId>
       <artifactId>parent</artifactId>
       <version>1.0</version>
    </parent>
    <modules>
       <module>childB</module>
    </modules>

  • B项目:
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>childA</artifactId>
    <version>1.0</version>
    <parent>
       <groupId>com.demo</groupId>
       <artifactId>childA</artifactId>
       <version>1.0</version>
    </parent>
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。