【问题标题】:Jenkins and maven multi module Projects missing artifactsJenkins 和 Maven 多模块项目缺少工件
【发布时间】:2024-05-17 21:00:02
【问题描述】:

这是一个 ear 项目的简化示例,父 pom 聚合了 EAR、EJB 和 jar。

我在一个 Maven 项目中有这个结构,存储在 SVN 中:

parent/
|- pom.xml
|- modulA/
|  |- pom.xml
|- modulB/
|  |- pom.xml

modulB 有一个 modulA 的 Dependency

pom.xml 有模块部分

<modules>
  <module>modulA</module>
  <module>modulB</module>
</modules>

还有一个依赖管理部分

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>group</groupId>
            <artifactId>modulA</artifactId>
            <version>0.0.2-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>group</groupId>
            <artifactId>modulB</artifactId>
            <version>0.0.2-SNAPSHOT</version>
        </dependency>
    </dependencies>
</dependencyManagement>

子模块引用父模块

<parent>
    <groupId>group</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    <relativePath>..</relativePath>
</parent>

当我第一次使用 maven 2.2.1 (windows) 编译时在我的电脑中

mvn clean compile

我没有任何问题

但是....当 Jenkins 第一次尝试编译时(Maven 2.2.1 Linux RedHat)

Missing:
----------
 1) modulA:jar:0.0.2-SNAPSHOT

   Try downloading the file manually from the project website.

   Then, install it using the command: 
      mvn install:install-file -DgroupId=group -DartifactId=modulA -Dversion=0.0.2-   SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file

   Alternatively, if you host your own repository you can deploy the file there: 
      mvn deploy:deploy-file -DgroupId=group -DartifactId=modulA -Dversion=0.0.2-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

   Path to dependency: 
      1) modulB:ejb:0.0.2-SNAPSHOT
 2) modulA:jar:0.0.2-SNAPSHOT


   ----------
   1 required artifacts are missing.

为什么??????????

之后,如果我将项目从我的 pc 部署到 Artifactory,Jenkins 就没有问题,因为 Jenkins 从存储库下载工件......但是为什么 Jenkins 依赖于存储库中的工件?

:(

提前致谢

编辑:

我认为dependencyManagement 部分仅“定义”依赖项,但如果子模块不使用依赖项,则不会将依赖项添加到子模块中。 我删除了dependencyManagement部分,Jenkins中的问题仍然存在。

它在我的电脑上运行没有问题。

【问题讨论】:

  • 是否有可能为其中一个模块获得有效的 POM?如果它在本地工作,则可能是在引入问题之前,工件已作为开发的一部分部署到本地存储库,这将有效地隐藏它,直到您尝试在新机器上构建(例如构建机器)
  • 如果你清理了你的盒子上的本地 maven 存储库,你的盒子第一次构建会失败吗?我猜是的,如果是这样,这意味着您的构建中的某些内容在构建之前引用了 modulA-0.0.2-SNAPSHOT.jar。如果没有看到更多的父级和模块 pom,很难说那是什么。

标签: maven maven-2 jenkins multi-module


【解决方案1】:

第一次构建父项目时,您的 Jenkins 用户的 maven 存储库不会安装 modulA。 clean compile 然后在 modulA 中成功运行,但没有安装任何东西。在modulB中运行时,无法解析对modulA的依赖。

如果您的 Jenkins 作业的目标是 clean install 而不是 clean compile,那么 modulA 的工件将在 modulB 构建开始之前安装到 Jenkins 用户的存储库中,并且一切正常。

这可能在您自己的机器上有效,因为您在 modulA 中至少运行了一次 mvn install,或者因为您的 IDE 的类路径为您解决了问题。

【讨论】:

    【解决方案2】:

    我希望上面的依赖管理部分在父 pom 中。根据您的要求 modulB 具有 modulA 的依赖关系。所以我建议你在 moduleB 中包含依赖项,而不是在父 pom.xml 中包含依赖项。我认为当它第一次运行时,maven 正在寻找两个依赖项,因为您在父 pom 中提到过。查看您的项目构建顺序。首先它构建模块 A,然后构建 B。在您的情况下,我希望您在模块 A 的 pom 文件中包含所有其他依赖项,并且一旦构建,它将在 m2 存储库中部署一个 jar 文件。然后 moduleB 开始构建,由于您的依赖项已经在 m2 存储库中,它不会大喊大叫,项目将成功构建。

    【讨论】: