gpt4 book ai didi

java - Maven多模块+继承: Issue in building submodules when maintaining parent version number via placeholders

转载 作者:行者123 更新时间:2023-12-01 23:29:17 26 4
gpt4 key购买 nike

这实际上是 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'包含一个表达式,但应该是一个常量警告 - 足够公平)。

但是,如果我这样做

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 可以解析 ba${ver},但是当它扫描 a 的 POM 无法解析 aparent's ${ver} 版本(尽管 relativePath 条目就在那里);可能是因为 Maven 正在从本地存储库(~/.m2/repository/ - 相对路径没有意义)读取 a 的 POM,而不是从本地代码库?

有没有办法让它工作 - 避免错误并让部分构建工作 - 也许通过一些黑客;就像通过系统属性传递 ${ver} 的默认值一样?

附注:

是啊是啊,我知道在父版本号中使用占位符是又臭又邪恶;但我的实际项目有大约 30 个模块,其中许多模块相互依赖。所以我只想要一种方法来维护单个版本号(一行),我可以轻松更改它 - 而不必每次升级时更改和提交几百行。

基本上,我并不是要求建议在各处复制版本号(并使用 Maven 版本插件之类的东西一次性升级它们) - 我只需要一个 hack 来让当前的结构正常工作。

(所以我相信这不能被标记为重复 - 因为复制和占位符是我在 SO 中遇到的唯一两个选项,而我的问题是关于后者的具体情况。🙏)

最佳答案

如果只是在整个项目中维护一个版本,那么您可以使用revision占位符。

父级必须声明它,子级可以继承它。

<?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>${revision}</version>
<packaging>pom</packaging>

<modules>
<module>a</module>
<module>b</module>
</modules>

<properties>
<revision>1.0-SNAPSHOT</revision>
</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>${revision}</version>
<relativePath>..</relativePath>
</parent>

<artifactId>a</artifactId>
</project>

了解更多信息 https://maven.apache.org/maven-ci-friendly.html#Multi_Module_Setup

关于java - Maven多模块+继承: Issue in building submodules when maintaining parent version number via placeholders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58296447/

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