【发布时间】:2020-02-06 08:04:48
【问题描述】:
这是this answer on maintaining parent version numbers via placeholders的后续行动:
我在 Maven 3.6.1,并且有以下多模块 Maven 项目结构:
pom.xml
a/
pom.xml
b/
pom.xml
pom.xml(父母):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.tuna</groupId>
<artifactId>root</artifactId>
<version>${ver}</version>
<packaging>pom</packaging>
<modules>
<module>a</module>
<module>b</module>
</modules>
<properties>
<ver>1.0-SNAPSHOT</ver>
</properties>
</project>
a/pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<parent>
<groupId>com.tuna</groupId>
<artifactId>root</artifactId>
<version>${ver}</version>
<relativePath>..</relativePath>
</parent>
<artifactId>a</artifactId>
<version>${ver}</version>
</project>
b/pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<parent>
<groupId>com.tuna</groupId>
<artifactId>root</artifactId>
<version>${ver}</version>
<relativePath>..</relativePath>
</parent>
<artifactId>b</artifactId>
<version>${ver}</version>
<dependencies>
<dependency>
<groupId>com.tuna</groupId>
<artifactId>a</artifactId>
<version>${ver}</version>
</dependency>
</dependencies>
</project>
请注意,b 依赖于 a。
现在,当我用
构建它时mvn clean install
它构建成功(带有一些'version' contains an expression but should be a constant 警告 - 很公平)。
但是,如果我这样做了
mvn clean install -rf :b
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.tuna:a:jar:1.0-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ com.tuna:a:${ver}, C:\Users\janaka\code\dustbin\mvn-multi-module\a\pom.xml, line 15, column 14
[WARNING] 'version' contains an expression but should be a constant. @ com.tuna:root:${ver}, C:\Users\janaka\code\dustbin\mvn-multi-module\pom.xml, line 9, column 14
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.tuna:b:jar:1.0-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ com.tuna:b:${ver}, C:\Users\janaka\code\dustbin\mvn-multi-module\b\pom.xml, line 15, column 14
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.tuna:root:pom:1.0-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ com.tuna:root:${ver}, C:\Users\janaka\code\dustbin\mvn-multi-module\pom.xml, line 9, column 14
[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] -----------------------------< com.tuna:b >-----------------------------
[INFO] Building b 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.472 s
[INFO] Finished at: 2019-10-09T00:08:14+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project b: Could not resolve dependencies for project com.tuna:b:jar:1.0-SNAPSHOT: Failed to collect dependencies at com.tuna:a:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for com.tuna:a:jar:1.0-SNAPSHOT: Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact com.tuna:root:pom:${ver} has not been downloaded from it before. -> [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
如果我在 b/ 内运行 mvn clean install,也会发生同样的情况。
显然 Maven 可以为 b 和 a 解析 ${ver},但是当它扫描 a 的 POM 时,它无法解析 a 的 父母 ${ver} 版本(尽管有 relativePath 条目);可能是因为 Maven 正在从本地存储库(~/.m2/repository/ - 相对路径没有意义的地方)而不是从本地代码库中读取 a 的 POM?
有没有办法让它工作 - 避免错误并让部分构建工作 - 也许通过一些黑客;比如通过系统属性为${ver} 传递一个默认值?
附注:
是啊是啊,我知道在父版本号中使用占位符是很糟糕和邪恶的;但我的实际项目有大约 30 个模块,其中许多模块相互依赖。所以我只想要一种方法来维护一个版本号(一行),我可以轻松更改它 - 每次升级时都不必更改和提交几百行。
基本上,我并不是要求建议在整个地方复制版本号(并使用 Maven 版本插件之类的东西一次升级它们)——我只需要一个 hack 来让当前的结构正常工作。
(所以我相信这不能被标记为重复 - 因为复制和占位符是我在 SO 中遇到的唯一两个选项,我的问题是关于后者的具体情况。????)
【问题讨论】: