举例设置一个解决父子模块依赖的例子
时间: 2023-08-11 15:04:59 浏览: 92
假设我们有一个 Maven 项目,其中包含父模块和子模块。父模块的 pom.xml 文件如下:
```xml
<project>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>child</module>
</modules>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common-utils</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
```
子模块的 pom.xml 文件如下:
```xml
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>child</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common-utils</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
```
在这个例子中,父模块依赖于 common-utils,而子模块也依赖于 common-utils。我们可以通过将 common-utils 提取到一个单独的模块中来解决这个问题,然后让父模块和子模块都依赖于它。
首先,在项目根目录下创建一个名为 common-utils 的新模块:
```
$ mkdir common-utils
$ cd common-utils
$ mvn archetype:generate -DgroupId=com.example -DartifactId=common-utils -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
```
然后,将 common-utils 的 pom.xml 文件修改为如下内容:
```xml
<project>
<groupId>com.example</groupId>
<artifactId>common-utils</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
</project>
```
接下来,将父模块和子模块的 pom.xml 文件中的 common-utils 依赖修改为对 common-utils 模块的依赖:
父模块:
```xml
<project>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>child</module>
</modules>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common-utils</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
```
子模块:
```xml
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>child</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common-utils</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
```
现在,我们已经成功地解决了父子模块之间的依赖问题。
阅读全文
相关推荐











