Maven 反应器构建顺序不满足所有依赖项

Maven reactor builds in order that doesn't satisfy all dependencies

我在使用 Maven 进行 SNAPSHOT 构建时遇到构建排序问题。反应器正在以无效顺序构建所有罐子。

以下是重现问题的示例结构(我遇到问题的项目要大得多,>100 个罐子):

pom.xml        [reactor]
parent/pom.xml [parent]
jar1/pom.xml
jar2/pom.xml
jar3/pom.xml

pom.xml [反应堆]

<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>
  <packaging>pom</packaging>
  <groupId>com.test.buildorder</groupId>
  <artifactId>reactor</artifactId>
  <version>1.0</version>
  <modules>
    <module>jar3</module>
    <module>jar2</module>
    <module>jar1</module>
  </modules>
</project>

parent/pom.xml

<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.test.buildorder</groupId>
  <artifactId>parent</artifactId>
  <version>1.1</version>
  <packaging>pom</packaging>

  <dependencyManagement>
  <dependencies>
      <dependency>
        <groupId>com.test.buildorder</groupId>
        <artifactId>jar1</artifactId>
        <version>1.1</version>
      </dependency>
      <dependency>
        <groupId>com.test.buildorder</groupId>
        <artifactId>jar2</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>com.test.buildorder</groupId>
        <artifactId>jar3</artifactId>
        <version>1.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

jar1/pom.xml

<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.test.buildorder</groupId>
  <artifactId>jar1</artifactId>
  <version>1.1</version>
  <parent>
    <groupId>com.test.buildorder</groupId>
    <artifactId>parent</artifactId>
    <version>1.1</version>
  </parent>
</project>

jar2/pom.xml

<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.test.buildorder</groupId>
  <artifactId>jar2</artifactId>
  <version>1.1</version>
  <parent>
    <groupId>com.test.buildorder</groupId>
    <artifactId>parent</artifactId>
    <version>1.0</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>com.test.buildorder</groupId>
      <artifactId>jar1</artifactId>
    </dependency>
  </dependencies>
</project>

jar3/pom.xml

<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.test.buildorder</groupId>
  <artifactId>jar3</artifactId>
  <version>1.1</version>
  <parent>
    <groupId>com.test.buildorder</groupId>
    <artifactId>parent</artifactId>
    <version>1.1</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>com.test.buildorder</groupId>
      <artifactId>jar2</artifactId>
    </dependency>
  </dependencies>
</project>

我的存储库(在本例中为本地)只有每个 jar 的已发布 1.0 版本,jar3-1.0 依赖于 jar2-1.0,而 jar2-1.0 依赖于 jar1-1.0 并且每个都有 parent-1.0(我可以post 这个代码如果需要的话,但我的问题已经很长所以我会推迟,除非被问到)。

我现在安装上面的 1.1 父 pom,然后尝试 运行 在 reactor 上安装并得到以下结果。

[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.test.buildorder:jar3:jar:1.1
[WARNING] 'parent.relativePath' points at com.test.buildorder:reactor instead of com.test.buildorder:parent, please verify your project structure @ line 6, column 11
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.test.buildorder:jar2:jar:1.1
[WARNING] 'parent.relativePath' points at com.test.buildorder:reactor instead of com.test.buildorder:parent, please verify your project structure @ line 6, column 11
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.test.buildorder:jar1:jar:1.1
[WARNING] 'parent.relativePath' points at com.test.buildorder:reactor instead of com.test.buildorder:parent, please verify your project structure @ line 6, column 11
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] jar3
[INFO] jar2
[INFO] jar1
[INFO] reactor
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jar3 1.1
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/com/test/buildorder/jar1/1.1/jar1-1.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] jar3 ............................................... FAILURE [  0.645 s]
[INFO] jar2 ............................................... SKIPPED
[INFO] jar1 ............................................... SKIPPED
[INFO] reactor ............................................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.775 s
[INFO] Finished at: 2015-06-03T16:56:04-05:00
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project jar3: Could not resolve dependencies for project com.test.buildorder:jar3:jar:1.1: Could not find artifact com.test.buildorder:jar1:jar:1.1 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

当我构建 1.0 版本时,Reactor 足够智能,可以实现依赖树 jar3-1.0 -> jar2-1.0 -> jar1-1.0 并按 jar1、jar2、jar3 的顺序构建以满足依赖关系。

现在虽然因为 jar3-1.1 依赖于 jar2-1.0(在 repo 中)reactor 不假设需要首先构建 jar1 但调试(使用 -X 参数)显示依赖树到是:

[DEBUG] com.test.buildorder:jar3:jar:1.1
[DEBUG]    com.test.buildorder:jar2:jar:1.0:compile
[DEBUG]       com.test.buildorder:jar1:jar:1.1:compile (version managed from 1.0 by com.test.buildorder:parent:1.1)

所以我对这个场景有几个问题:
1.为什么maven认为jar3-1.1对jar1-1.1有传递依赖?
2. 为什么reactor没有意识到这种传递依赖并按照要求的顺序构建来满足它?
3. 我能做些什么来防止这个问题的发生? (最好不要重写超过 100 个 poms。)

编辑:找到问题一的答案和问题三的一个解决方案。但还是想听听问题二

1。因为我从未定义依赖项的范围,maven 确定分配了默认范围 compile。因此,从 jar2 到 jar1 的依赖关系将显示为依赖于 jar2(例如 jar3)的 jar 的传递依赖关系。 (version managed from 1.0 by com.test.buildorder:parent:1.1) 告诉我依赖项是从 jar2-1.0 到 jar1-1.0,但是因为父项为 jar 1 (1.1) 指定了另一个版本,所以需要那个。

3。如果我将依赖项的范围从 jar2 更改为 jar1 再到 provided,则依赖项不再具有传递性。 这假定在 运行 时所需的 jar 都将存在,因此这可能不是遇到此问题的每个人的解决方案。 Here is a guide to the scopes available in Maven

[WARNING] Some problems were encountered while building the effective model for com.test.buildorder:jar3:jar:1.1 [WARNING] 'parent.relativePath' points at com.test.buildorder:reactor instead of com.test.buildorder:parent,

由于您的反应堆不是父反应堆,我假设您需要明确定义相对路径

<parent>
    <groupId>com.test.buildorder</groupId>
    <artifactId>parent</artifactId>
    <version>1.1</version>
    <relativePath>../parent</relativePath>
  </parent>

此外,您还有不同版本的 jar2 1.01.1。对齐它们

我在这里安装了所有版本设置为 1.0 的所有项目。然后我按照你说的更改了版本并得到了同样的错误。

reactor POM 更改为:

 <modules>
    <module>jar1</module>
    <module>jar2</module>
    <module>jar3</module>
</modules>

...构建成功。

我查看了 Guide to Working with Multiple Modules, Reactor Sorting,最后一个选项是:

  • the order declared in the <modules> element (if no other rule applies)

看完下一句:

Note that only "instantiated" references are used - dependencyManagement and pluginManagement elements will not cause a change to the reactor sort order

  • 我恢复了 reactor POM
  • 我评论了 parent POM 中的 <dependencyManagement> 部分,并明确地向 jar2jar3 POM 添加了 <version>

构建成功:

[INFO] Reactor Build Order
[INFO]
[INFO] jar3
[INFO] jar1
[INFO] jar2
[INFO] reactor

关于问题 2:Maven 反应堆排序不适用于传递依赖,它只计算直接依赖:

  • jar3-1.1 依赖于 jar2-1.0(不在 reactor 中)

  • jar2-1.1 依赖于 jar1-1.1

  • jar1-1.1

在这种情况下,jar3-1.1 到 jar1-1.1 没有 link

您不应将 reactor 中的 jar2-1.1 与 dependencyManagement 中的 jar2-1.0 混合使用