
# 执行Maven构建时，多个子项目和父项目之间引用报错
#### 问题现象
Maven构建任务，pom文件存在多个子项目和父项目之间的引用，在执行任务时，日志报如下异常信息：
```
[ERROR] Project 'xxx.xxx:xxx1:1.0-SNAPSHOT' is duplicated in the reactor @
[2022-03-02 14:02:52.656] [ERROR] Project 'xxx.xxx:xxx2:1.0-SNAPSHOT' is duplicated in the reactor -> [Help 1]
[2022-03-02 14:02:52.656] [ERROR]
[2022-03-02 14:02:52.656] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[2022-03-02 14:02:52.656] [ERROR] Re-run Maven using the -X switch to enable full debug logging.
```
#### 原因分析
在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>
  ```
  
 
