我在使用Maven的SNAPSHOT构建中进行构建排序时遇到问题。反应堆正以无效的顺序建造所有罐子。
以下是重新创建问题的示例结构(我遇到问题的项目要大得多,> 100罐):
pom.xml [reactor]
parent/pom.xml [parent]
jar1/pom.xml
jar2/pom.xml
jar3/pom.xml
pom.xml [reactor]
<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并且每个都有父级1.0(我可以发布这个代码,如果需要,但我的问题已经很长,所以我会推迟,除非问)。
我现在安装上面的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 - &gt; jar2-1.0 - &gt; jar1-1.0并以jar1,jar2,jar3的顺序构建以满足依赖性。
现在因为jar3-1.1依赖于jar2-1.0(在repo中),反应器不会假设首先需要构建jar1,但是debug(使用-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.为什么反应堆没有意识到这种传递依赖性并建立满足它的所需顺序?
3.如何防止此问题发生? (最好不要重写超过100个poms。)
编辑:找到问题一的答案,以及问题三的一个解决方案。但是仍然希望听到问题二。
1 。因为我从未为依赖关系定义范围,所以maven确定已分配默认范围compile
。因此,从jar2到jar1的依赖关系将显示为依赖于jar2的jar的传递依赖(例如jar3)。 (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
,则依赖项不再变为可传递。 这假设在运行时所需的罐子都存在,所以这可能不是每个遇到此问题的人的解决方案。 Here is a guide to the scopes available in Maven
答案 0 :(得分:4)
我安装了所有项目,所有版本都设置为1.0
。然后我按照你的说法更改了版本并得到了同样的错误。
将reactor
POM更改为:
<modules>
<module>jar1</module>
<module>jar2</module>
<module>jar3</module>
</modules>
......构建成功。
我查看了Guide to Working with Multiple Modules, Reactor Sorting,最后一个选项是:
<modules>
元素中声明的顺序(如果没有其他规则适用)在阅读下一句“注意,仅使用”实例化“引用 - dependencyManagement
和pluginManagement
元素不会导致更改反应堆排序顺序”:
reactor
POM <dependencyManagement>
POM中的parent
部分进行了评论,并明确地将<version>
添加到jar2
和jar3
POM中→构建成功了:
[INFO] Reactor Build Order
[INFO]
[INFO] jar3
[INFO] jar1
[INFO] jar2
[INFO] reactor
答案 1 :(得分:0)
[警告]构建有效时遇到了一些问题 com.test.buildorder的模型:jar3:jar:1.1 [警告] &#39; parent.relativePath&#39;指向com.test.buildorder:reactor而不是 com.test.buildorder:父母,
由于您的反应堆不是父级,我认为您需要明确定义相对路径
<parent>
<groupId>com.test.buildorder</groupId>
<artifactId>parent</artifactId>
<version>1.1</version>
<relativePath>../parent</relativePath>
</parent>
此外,您有jar2
1.0
和1.1
的不同版本。对齐他们
答案 2 :(得分:0)
问题2:Maven反应堆排序不适用于传递依赖关系,它仅计算直接依赖关系:
jar3-1.1取决于jar2-1.0(不在反应堆中)
jar2-1.1取决于jar1-1.1
jar1-1.1
在这种情况下,从jar3-1.1到jar1-1.1没有链接
我们不希望您将反应堆中的jar2-1.1与您的dependencyManagement中的jar2-1.0混合