错误解决跨模块 Maven 项目的依赖

huangapple go评论77阅读模式
英文:

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:

&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

	&lt;groupId&gt;com.dw&lt;/groupId&gt;
	&lt;artifactId&gt;dw-parent&lt;/artifactId&gt;
	&lt;version&gt;1.0&lt;/version&gt;
	&lt;packaging&gt;pom&lt;/packaging&gt;

	&lt;modules&gt;
		&lt;module&gt;dw-web&lt;/module&gt;
		&lt;module&gt;dw-test&lt;/module&gt;
	&lt;/modules&gt;

	&lt;build&gt;
		&lt;pluginManagement&gt;
			&lt;plugins&gt;
				&lt;plugin&gt;
					&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
					&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
					&lt;version&gt;3.8.0&lt;/version&gt;
					&lt;configuration&gt;
						&lt;release&gt;11&lt;/release&gt;
					&lt;/configuration&gt;
				&lt;/plugin&gt;
			&lt;/plugins&gt;
		&lt;/pluginManagement&gt;
	&lt;/build&gt;

dw-test pom:

&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;artifactId&gt;dw-test&lt;/artifactId&gt;
&lt;parent&gt;
	&lt;groupId&gt;com.dw&lt;/groupId&gt;
	&lt;artifactId&gt;dw-parent&lt;/artifactId&gt;
	&lt;version&gt;1.0&lt;/version&gt;
&lt;/parent&gt;

&lt;dependencies&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;com.dw&lt;/groupId&gt;
	&lt;artifactId&gt;dw-web&lt;/artifactId&gt;
	&lt;version&gt;1.0&lt;/version&gt;
  &lt;/dependency&gt;
&lt;/dependencies&gt;

dw-web pom:

&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;artifactId&gt;dw-web&lt;/artifactId&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;version&gt;1.0&lt;/version&gt;

&lt;parent&gt;
	&lt;groupId&gt;com.dw&lt;/groupId&gt;
	&lt;artifactId&gt;dw-parent&lt;/artifactId&gt;
	&lt;version&gt;1.0&lt;/version&gt;
&lt;/parent&gt;

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:

&lt;dependency&gt;
  &lt;groupId&gt;com.dw&lt;/groupId&gt;
  &lt;artifactId&gt;dw-web&lt;/artifactId&gt;
  &lt;version&gt;1.0&lt;/version&gt;
&lt;/dependency&gt;

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:

&lt;project&gt;
    ...
     &lt;packaging&gt;pom&lt;/packaging&gt;

     &lt;modules&gt;
        &lt;module&gt;dw-web&lt;/module&gt;
        &lt;module&gt;dw-test&lt;/module&gt;
     &lt;/modules&gt;
&lt;/project&gt;

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.

huangapple
  • 本文由 发表于 2020年5月19日 18:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61888450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定