解决maven多模块项目的依赖关系时出错。

问题描述 投票:2回答:1

我建立了一个多模块的maven项目,有两个模块:dw-web和dw-test。

Parent
  - dw-web
  - dw-test

父模块pom:dw-web和dw-test。

<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>

由于我是新来的,我用这个指南作为参考。https:/books.sonatype.commvnex-bookreferencemultimodule-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仓库以解决依赖错误?多模块项目是不是应该解决这些项目之间的任何依赖关系,而不需要上传然后下载构建项目到外部实体?

java eclipse jenkins maven-2 multi-module
1个回答
1
投票

Parent "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文件的相关代码片段。


0
投票

解决这个问题的方法是,我已经声明了 dw-web 在父 pom 中既是模块又是依赖。移除依赖声明后,项目就编译完成了。

© www.soinside.com 2019 - 2024. All rights reserved.