英文:
Error resolving dependency for maven multi module project
问题
我已经设置了一个多模块的Maven项目,其中包括两个模块: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文件。我是否需要通过<scope>system</scope>
标签导入JAR文件,然后指示Maven定位JAR文件,或者将它上传到Nexus仓库以解决依赖错误?难道多模块项目不应该解决这些项目之间的依赖,而无需将构建项目上传并从外部实体下载吗?
英文:
I have set up a multi module maven project with two modules, dw-web and dw-test.
Parent
- dw-web
- dw-test
The parent 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>
Since im new to maven i used this guide as a reference: https://books.sonatype.com/mvnex-book/reference/multimodule-sect-simple-web.html.
It suggests to import the module that dw-test depends on (dw-web) to import with this dependency declaration:
<dependency>
<groupId>com.dw</groupId>
<artifactId>dw-web</artifactId>
<version>1.0</version>
</dependency>
When executing mvn clean install on the parent pom the import for this dependency fails on my test server, but not on my machine.
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
Some resources suggest that either the local m2 repository or my IDE(eclipse) seems to cache the generated jar? Do i have to import the jar via the <scope>system</scope> tag and then point maven to the jar or alternatively upload it to a nexus repository in order to resolve the dependency errors? Isn't the multi module project supposed to resolve any dependencies between these projects without having to up and then download the build projects to an external entity?
答案1
得分: 1
"parent" pom应该是一个"聚合器",也就是说,它应该包括dw-web和dw-test的模块,并且本身具有packaging类型为pom:
<project>
...
<packaging>pom</packaging>
<modules>
<module>dw-web</module>
<module>dw-test</module>
</modules>
</project>
有了这个设置,Maven将自动解析依赖图并按正确的顺序编译所有内容(首先是dw-web
,然后是dw-test
)。
因此,请确保您有这样的设置。
当然,还可能有其他原因,最好是从问题中添加所有pom文件的相关代码片段。
英文:
The "parent" pom should be an "aggregator", that is it should include modules of both dw-web and dw-test and itself have a packaging pom:
<project>
...
<packaging>pom</packaging>
<modules>
<module>dw-web</module>
<module>dw-test</module>
</modules>
</project>
With this setup, maven will automatically resolve the dependency graph and compile everything in the correct order (dw-web
first, and dw-test
afterwards).
So make sure that you have such a setup.
Of course there can be other reasons as well, the best would be adding relevant code snippets from all the pom files in the question.
答案2
得分: 0
问题的解决方案是我在父POM中既将dw-web
声明为模块,又声明为依赖项。在移除依赖声明后,项目编译成功。
英文:
The solution to the problem was that I had declared dw-web
in the parent pom both as a module and dependency. After removing the dependency declaration the project compiled.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论