Jenkins 无法构建多模块 Maven 项目

Jenkins fails to build multi-module Maven project

我有一个多模块 Maven 项目,其中有多个微服务作为模块,所以我的父 pom.xml 中列出了模块,如下所示:

<modules>
    <module>core</module>
    <module>model-base</module>
    <module>module1</module>
    <module>module2</module>
    ...
    <module>module5</module>
    <module>module7</module>
    <module>module6</module>
</modules>

这里 module7 依赖于 module5, 6 所以我在我的 module7 pom.xml:

中列出了如下依赖项
<parent>
    <artifactId>pojectA</artifactId>
    <groupId>com.domain</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module7</artifactId>
<dependencies>
    <dependency>
        <groupId>com.domain</groupId>
        <artifactId>core</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.domain</groupId>
        <artifactId>module5</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.domain</groupId>
        <artifactId>module6</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies> 

当我 运行 mvn clean package 在我的本地 module5, 6 按预期在 module7 之前调用但在 Jenkins 中它正在尝试构建 module 5 然后module7 构建失败说:

[ERROR] Failed to execute goal on project module7: Could not resolve dependencies for project module7:jar:1.0-SNAPSHOT: Could not find artifact module6:jar:1.0-SNAPSHOT -> [Help 1]

我是否需要 运行 任何其他作业或重新排序我的 pom.xml 中的模块,本地与 Jenkins 有何不同?感谢对此的任何帮助。

模块的顺序不相关。 Maven 识别哪个项目依赖于哪个其他项目,并相应地在反应器中设置构建顺序。见 POM Reference, Aggregation (or Multi-Module):

You do not need to consider the inter-module dependencies yourself when listing the modules, i.e. the ordering of the modules given by the POM is not important. Maven will topologically sort the modules such that dependencies are always build before dependent modules.

众所周知,问题是子模块之间的依赖关系失败,因为它们尚未安装在本地存储库中(因为它们尚未构建)。导致这个(无论如何对我来说)的目标是 mvn test,它被 mvn package 调用。您的本地构建可能有效,因为在某些时候您已经完成了 mvn install 并且这已经引导了您的系统。

在 Jenkins 中,我发现使这些构建工作的唯一方法是使用 预构建步骤 调用安装的 Maven 目标,然后构建主要步骤和往常一样。

按照下面附带的屏幕截图添加前置步骤。这将编译所有顶级模块。 然后我们可以执行我们想要的任何模块。