我已经建立了一个包含两个模块的多模块 maven 项目,dw-web 和 dw-test。
Parent
- dw-web
- dw-test
父pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.dw</groupId>
<artifactId>dw-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>dw-web</module>
<module>dw-test</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
dw-test pom:
<modelVersion>4.0.0</modelVersion>
<artifactId>dw-test</artifactId>
<parent>
<groupId>com.dw</groupId>
<artifactId>dw-parent</artifactId>
<version>1.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.dw</groupId>
<artifactId>dw-web</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
dw-web pom:
<modelVersion>4.0.0</modelVersion>
<artifactId>dw-web</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<parent>
<groupId>com.dw</groupId>
<artifactId>dw-parent</artifactId>
<version>1.0</version>
</parent>
由于我是 Maven 的新手,所以我使用本指南作为引用:https://books.sonatype.com/mvnex-book/reference/multimodule-sect-simple-web.html . 它建议导入 dw-test 依赖的模块 (dw-web) 以使用此依赖声明导入:
<dependency>
<groupId>com.dw</groupId>
<artifactId>dw-web</artifactId>
<version>1.0</version>
</dependency>
在父 pom 上执行 mvn clean install 时,此依赖项的导入在我的测试服务器上失败,但在我的机器上却没有。
Failed to execute goal on project dw-test: Could not resolve dependencies for project com.dw:dw-test:jar:1.0: Failure to find com.dw:dw-web:jar:1.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
一些资源表明本地 m2 存储库或我的 IDE(eclipse) 似乎缓存了生成的 jar?我是否必须通过系统标签导入 jar,然后将 maven 指向 jar 或者将其上传到 nexus 存储库以解决依赖性错误?多模块项目难道不应该解决这些项目之间的任何依赖关系,而无需启动然后将构建项目下载到外部实体吗?
最佳答案
“父”pom 应该是一个“聚合器”,即它应该包括 dw-web 和 dw-test 的模块,并且它本身有一个包装 pom:
<project>
...
<packaging>pom</packaging>
<modules>
<module>dw-web</module>
<module>dw-test</module>
</modules>
</project>
使用此设置,maven 将自动解析依赖关系图并以正确的顺序编译所有内容(首先是 dw-web
,然后是 dw-test
)。
所以请确保您有这样的设置。 当然也可能有其他原因,最好是从问题中的所有 pom 文件中添加相关代码片段。
关于java - 解决 Maven 多模块项目的依赖项时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61888450/