gpt4 book ai didi

Maven react 器按不满足所有依赖项的顺序构建

转载 作者:行者123 更新时间:2023-12-03 23:15:40 25 4
gpt4 key购买 nike

我在使用 Maven 的 SNAPSHOT 构建中进行构建排序时遇到问题。 react 器正在以无效的顺序构建所有 jar 。

以下是重新创建问题的示例结构(我遇到问题的项目要大得多,> 100 个 jar):

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

pom.xml [ react 器]
<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>

父/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(如果需要,我可以发布此代码,但我的问题已经很长了,所以除非被问到,否则我会推迟)。

我现在安装上面的 1.1 父 pom,然后尝试在 react 器上运行安装并得到以下结果。
[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. 为什么 react 器没有实现这种传递依赖并按照要求的顺序构建来满足它?
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

最佳答案

我安装了所有版本设置为 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聚甲醛
  • 我评论了 <dependencyManagement> parent 中的部分POM 并添加 <version>jar2jar3 POM 明确

  • 并且构建成功:
    [INFO] Reactor Build Order
    [INFO]
    [INFO] jar3
    [INFO] jar1
    [INFO] jar2
    [INFO] reactor

    关于Maven react 器按不满足所有依赖项的顺序构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631988/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com