Java Maven多模块&x2B;继承:通过占位符维护父版本号时,生成子模块时出现问题
这是对以下问题的后续行动: 我在Maven3.6.1上,拥有以下多模块Maven项目结构:Java Maven多模块&x2B;继承:通过占位符维护父版本号时,生成子模块时出现问题,java,maven,maven-3,Java,Maven,Maven 3,这是对以下问题的后续行动: 我在Maven3.6.1上,拥有以下多模块Maven项目结构: pom.xml a/ pom.xml b/ pom.xml pom.xml(父项): b/pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apach
pom.xml
a/
pom.xml
b/
pom.xml
pom.xml
(父项):
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>a</artifactId>
<version>${ver}</version>
</project>
<?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>
它成功构建(有些“version”包含一个表达式,但应该是一个常量警告-足够公平)
然而,如果我这样做
mvn clean install -rf :b
[INFO]正在扫描项目。。。
[警告]
[警告]为com.tuna:a:jar:1.0-SNAPSHOT构建有效模型时遇到一些问题
[警告]“version”包含表达式,但应为常量。@com.tuna:a:${ver},C:\Users\janaka\code\dustbin\mvn multi-module\a\pom.xml,第15行,第14列
[警告]“version”包含表达式,但应为常量。@com.tuna:root:${ver},C:\Users\janaka\code\dustbin\mvn multi-module\pom.xml,第9行,第14列
[警告]
[警告]为com.tuna:b:jar:1.0-SNAPSHOT构建有效模型时遇到一些问题
[警告]“version”包含表达式,但应为常量。@com.tuna:b:${ver},C:\Users\janaka\code\dustbin\mvn multi-module\b\pom.xml,第15行,第14列
[警告]
[警告]为com.tuna:root:pom:1.0-SNAPSHOT构建有效模型时遇到一些问题
[警告]“version”包含表达式,但应为常量。@com.tuna:root:${ver},C:\Users\janaka\code\dustbin\mvn multi-module\pom.xml,第9行,第14列
[警告]
[警告]强烈建议修复这些问题,因为它们会威胁到构建的稳定性。
[警告]
[警告]出于这个原因,未来的Maven版本可能不再支持构建这种格式错误的项目。
[警告]
[信息]
[信息]------------------------------------------------------------
[信息]楼宇b 1.0-SNAPSHOT
[信息]------------------------------------[jar]---------------------------------
[信息]------------------------------------------------------------------------
[信息]生成失败
[信息]------------------------------------------------------------------------
[信息]总时间:0.472秒
[信息]完成时间:2019-10-09T00:08:14+05:30
[信息]------------------------------------------------------------------------
[错误]无法在项目b上执行目标:无法解析项目com的依赖项。tuna:b:jar:1.0-SNAPSHOT:无法在com上收集依赖项。tuna:a:jar:1.0-SNAPSHOT:无法读取com的工件描述符。tuna:a:jar:1.0-SNAPSHOT:无法访问中心(https://repo.maven.apache.org/maven2)在脱机模式下,工件com.tuna:root:pom:${ver}以前从未从中下载过。->[帮助1]
[错误]
[错误]要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。
[错误]使用-X开关重新运行Maven以启用完整调试日志记录。
[错误]
[错误]有关错误和可能的解决方案的更多信息,请阅读以下文章:
[错误][帮助1]http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
如果我从b/
中运行mvn clean install
,也会发生同样的情况
显然,Maven可以为b
和a
解析${ver}
,但是当它扫描a
的POM时,它无法解析a
的父级${ver}
版本(尽管相对路径
条目存在);可能是因为Maven从本地存储库(~/.m2/repository/
-相对路径没有意义)而不是从本地代码库读取a
的POM
有没有一种方法可以让它工作-避免错误,让部分构建工作-也许通过一些黑客;像是通过系统属性传递${ver}
的默认值吗
p.S.:
是的,是的,我知道在父版本号中使用占位符是很糟糕的;但我的实际项目有大约30个模块,其中许多模块相互依赖。因此,我只想找到一种方法来维护一个单一的版本号(一行),我可以轻松地更改它,而不必每次升级时都更改和提交几百行
基本上,我并不是在征求建议,以复制各地的版本号(并使用类似Maven versions plugin的东西一次升级所有版本)-我只是需要一个黑客来让当前的结构工作
(所以我认为这不能被标记为重复-因为复制和占位符是我在So中遇到的唯一两个选项,我的问题是关于后者的一个具体案例。如果只是在整个项目中维护一个版本,那么您可以使用修订版
占位符
父级必须声明它,子级可以继承它
<?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>
4.0.0
金枪鱼
谢谢!但它仍然不适用于部分构建(-rf:b
,或直接构建b
)。失败,错误与以前相同:未能在项目b上执行目标:无法解析项目com的依赖项。tuna:b:jar:1.0-SNAPSHOT:未能在com上收集依赖项。tuna:a:jar:1.0-SNAPSHOT:未能读取com的工件描述符。tuna:a:jar:1.0-SNAPSHOT:无法访问中心(https://repo.maven.apache.org/maven2)在脱机模式下,工件com.tuna:root:pom:${revision}以前没有从中下载过。
当您可以使用-pl
进行构建时,为什么要使用部分构建?您也处于脱机模式。我需要构建所有剩余的模块,因此-rf:
而不是-pl
;脱机模式很好,因为我已经在本地提供了所有依赖项。刚刚确认-pl
给出了同样的错误。(此外,撇开-pl
,-rf:
等,我应该能够构建单个模块(例如cd b/&mvn clean install
)-
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
<?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>
<?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>