简体   繁体   English

Jenkins 无法构建多模块 Maven 项目

[英]Jenkins fails to build multi-module Maven project

I have a multi-module Maven project where I have multiple micro services as modules so I have modules listed in my parent pom.xml like below:我有一个多模块 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>

Here the module7 is dependent on module5, 6 so I have dependencies listed like below in my module7 pom.xml :这里module7依赖于module5, 6所以我在module7 pom.xml有如下所列的依赖module7

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

When I run mvn clean package in my local the module5, 6 called before the module7 as expected but in Jenkins it is trying to build module 5 then module7 making build fail saying:当我在本地运行mvn clean packagemodule5, 6按预期在module7之前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]

Do I need to run any other jobs or re-order the modules in my pom.xml , how is it differ from local to Jenkins?我是否需要运行任何其他作业或重新排序pom.xml的模块,它与本地和 Jenkins 有何不同? Appreciate any help on this.感谢您对此的任何帮助。

The order of modules is not relevant.模块的顺序不相关。 Maven recognizes which project depends on which other project(s) and sets the build order in the reactor accordingly. Maven 识别哪个项目依赖于哪个其他项目,并相应地在反应器中设置构建顺序。 See POM Reference, Aggregation (or Multi-Module) :请参阅POM 参考、聚合(或多模块)

You do not need to consider the inter-module dependencies yourself when listing the modules, ie the ordering of the modules given by the POM is not important.在列出模块时,您不需要自己考虑模块间的依赖关系,即 POM 给出的模块顺序并不重要。 Maven will topologically sort the modules such that dependencies are always build before dependent modules. Maven 将对模块进行拓扑排序,以便始终在依赖模块之前构建依赖项。

Add Pre-Step as per below attached screenshot.按照下面附加的屏幕截图添加 Pre-Step。 This will compile all your top modules.这将编译您所有的顶级模块。 Then we can execute which ever module we want.然后我们可以执行我们想要的任何模块。

截图链接

As is probably quite well understood, the issue is that the dependencies between the child modules fail because they aren't installed in the local repository yet (because they are yet to be built).可能很容易理解,问题在于子模块之间的依赖关系失败,因为它们尚未安装在本地存储库中(因为它们尚未构建)。 The goal that causes this (for me anyway) is mvn test , which is invoked by mvn package .导致这种情况的目标(无论如何对我来说)是mvn test ,它由mvn package调用。 Your local build probably works because at some point you've done a mvn install and this has bootstrapped your system.您的本地构建可能有效,因为在某些时候您已经完成了mvn install并且这已经引导了您的系统。

In Jenkins the only way I've found to make these builds work is to use the Pre-build step invoking a Maven target of install, and then build the main step as usual.在 Jenkins 中,我发现使这些构建工作的唯一方法是使用调用 Maven 安装目标的预构建步骤,然后像往常一样构建主要步骤。

詹金斯配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM