解决 Maven 多模块项目的依赖项时出错

Error resolving dependency for maven multi module project

我已经建立了一个包含两个模块的多模块 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 存储库以解决依赖性错误?多模块项目不应该解决这些项目之间的任何依赖关系,而无需启动然后将构建项目下载到外部实体吗?

"parent" pom应该是一个"aggregator",也就是说它应该包括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 文件中添加相关代码片段。

问题的解决方案是我在父 pom 中将 dw-web 声明为模块和依赖项。删除依赖声明后项目编译。