Maven多模块项目,无法解决依赖问题

4
我有一个多模块的Maven项目,无法编译。我在本地网络上有一个Nexus仓库,并且它正在工作(IntelliJ Idea能够解析我的依赖项,这些依赖项只存在于该仓库中),我是通过Jetbrains TeamCity进行构建的。我相当确定TeamCity正常工作,因为我设置了几个其他的构建配置仍然有效(使用相同的settings.xml)。但我对可能导致问题的原因感到有点困惑。以下是我的pom文件:
父级pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.product.plugins</groupId>
    <artifactId>plugin-parent</artifactId>
    <version>1.2-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>product-wireless-plugin</module>
        <module>product-paging-plugin</module>
    </modules>

    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://192.168.2.192:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://192.168.2.192:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

    <pluginRepositories>
        <pluginRepository>
            <id>autoincrement-versions-maven-plugin</id>
            <name>autoincrement-versions-maven-plugin</name>
            <url>http://autoincrement-versions-maven-plugin.googlecode.com/svn/repo</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>autoincrement-versions-maven-plugin</artifactId>
                <version>2.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <id>update-pom-versions</id>
                        <goals>
                            <goal>increment</goal>
                            <goal>commit</goal>
                        </goals>
                        <phase>compile</phase>
                        <configuration>
                            <autoIncrementVersion>true</autoIncrementVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

产品-无线pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>plugin-parent</artifactId>
        <groupId>com.company.product.plugins</groupId>
        <version>1.2-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.product.plugins</groupId>
    <artifactId>product-wireless-plugin</artifactId>
    <version>0.1.2</version>

    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://192.168.2.192:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://192.168.2.192:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

    <dependencies>
        <dependency>
            <groupId>com.company.product</groupId>
            <artifactId>product-common</artifactId>
            <version>0.9.1</version>
        </dependency>
    </dependencies>

</project>

产品分页 pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>plugin-parent</artifactId>
        <groupId>com.company.product.plugins</groupId>
        <version>1.2-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.product.plugins</groupId>
    <artifactId>product-paging-plugin</artifactId>
    <version>0.1.2</version>

    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://192.168.2.192:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://192.168.2.192:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

    <dependencies>
        <dependency>
            <groupId>com.company.product</groupId>
            <artifactId>product-common</artifactId>
            <version>0.9.1</version>
        </dependency>
    </dependencies>

</project>

我遇到的错误是:
com.company.product.plugins:product-wireless-plugin [13:54:16][com.company.product.plugins:product-wireless-plugin] 用'surefire'处理器从'C:/TeamCity/buildAgent/work/40ac813105cf8bd7/product-wireless-plugin/target/surefire-reports/TEST-*.xml'导入数据 [13:54:16][com.company.product.plugins:product-wireless-plugin] Surefire报告监视器 [13:54:16][com.company.product.plugins:product-wireless-plugin] 下载:repolocation/nexus/content/groups/public/com/company/product/product-parent/0.9.0/product-parent-0.9.0.pom [13:54:16][com.company.product.plugins:product-wireless-plugin] 在项目product-wireless-plugin上执行目标失败:无法解析依赖项,以收集依赖项为失败:[com.company.product:product-common:jar:0.9.1 (compile)]。
我在调试时很困惑...有没有人有什么建议?

  1. 对于这个“无法解析依赖项”的错误,几乎总会在构建日志的早期出现更详细的错误消息和/或堆栈跟踪。
  2. 使用“-X”标志重新运行构建。
- noahlz
如果您能将您的评论发布为答案,我会接受它。使用那种调试方法,我能够找到问题所在。 - Brendon Dugan
在运行/打包各个子项目之前,在根级项目中运行mvn install有助于我。 - gtiwari333
3个回答

8

解决这种问题有几种方法/工具。

  1. 针对 "could not resolve dependencies" 错误,通常在构建日志中有更详细的错误消息和/或堆栈跟踪。Maven 日志实际上非常冗长,需要从构建失败的几个屏幕之前搜索“根”错误消息。

  2. 使用 -X 标志重新运行构建。这里是 Maven 命令行开关的文档

  3. 另一个选项是使用 mvn dependency:tree 检查传递依赖的完整图形。 mvn help:effective-pom 是另一个有用的工具,它会打印出考虑了您的 settings.xml、任何活动配置文件等后的 pom.xml。同样地,mvn help:active-profiles


4

你的多模块构建存在许多问题。其中最重要的一个问题是你定义了一个依赖项:

<dependencies>
    <dependency>
        <groupId>com.company.product</groupId>
        <artifactId>product-common</artifactId>
        <version>0.9.1</version>
    </dependency>
</dependencies>

看起来要么在存储库中不存在,要么您没有访问包含它的存储库,或者由于某种原因(无法猜测!)导致下载失败。您是否正在使用Artifactory、Nexus、Archiva等存储库管理器?如果没有,我建议开始使用其中之一。

另外,在wireless-module中,您正在使用不同版本的父级和模块:

   <parent>
        <artifactId>plugin-parent</artifactId>
        <groupId>com.company.product.plugins</groupId>
        <version>1.2-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.product.plugins</groupId>
    <artifactId>product-wireless-plugin</artifactId>
    <version>0.1.2</version>

多模块构建应该只通过父级定义版本,而不是在构件内部定义版本。这意味着上述内容应该如下所示:

<modelVersion>4.0.0</modelVersion>
<parent>
    <artifactId>plugin-parent</artifactId>
    <groupId>com.company.product.plugins</groupId>
    <version>1.2-SNAPSHOT</version>
</parent>

<groupId>com.company.product.plugins</groupId>
<artifactId>product-wireless-plugin</artifactId>

模块不应该自己定义版本,因为它将从父级继承。此外,您可以看到有一个模块定义了发布版本(1.2),而父级定义了SNAPSHOT版本。正在开发的应用程序/模块应该定义一个SNAPSHOT版本,这意味着像1.2-SNAPSHOT等类似的东西。
同样,分布式管理的定义也应该在项目的父级中仅被定义一次。
顺便说一句,如果您有多个项目,最好定义一个公司父级,其中包含一些默认定义,例如分布式管理、插件管理、依赖关系管理等。

我不同意你关于子模块和父模块版本号需要相同的观点,因为这实际上是我的问题所在。库“product-common”有一个父POM。我已经保持了父POM和所有子POM之间的主要版本号同步。然而,我偶尔需要发布一个子POM的更新(次要版本)来修复一个错误,递增所有父级子库的版本号没有任何意义。版本号会级联。如果我在子POM中有一个版本号,它会覆盖父级中的版本号。 - Brendon Dugan
此外,downvote 是因为没有正确阅读问题,因为问题确实提到了我的 Nexus 存储库以及库通过我的存储库解析。 - Brendon Dugan

2

好的,感谢@noahlz的帮助,我解决了问题。在使用-X标志调试构建后,我发现“product-common”的父POM(product-parent)无法找到。在浏览我的Sonatype Nexus存储库时,我发现我的构建系统仅在添加新模块时才发布父POM的新版本。因此,即使我的父POM版本为0.9.0,存储库中的最新版本也为0.6.1。我想“product-common”库之所以能正确编译,是因为它在编译时可以访问父POM(具有0.9.0版本号)。无论如何,将“product-common”中的父POM版本更改为指向存储库中最新版本,可以解决我的插件构建问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接