简体   繁体   English

在pom.xml中调用属性的层级

[英]Call hierachy of properties in pom.xml

I have a maven project with multiple nested projects. 我有一个Maven项目,其中包含多个嵌套项目。 All of them do always have the same version. 它们确实总是具有相同的版本。 Untill now, if I want to increase the version, I went through all pom.xml files and changed the version number. 直到现在,如果我想增加版本,我会遍历所有pom.xml文件并更改版本号。

Now, I wanted to outsource the version to the properties tag of the parent pom.xml file. 现在,我想将版本外包给父pom.xml文件的properties标签。 It works, for all nested projects, but not for the parent pom itself. 它适用于所有嵌套项目,但不适用于父pom本身。

In which order are the attributes in the pom.xml called? pom.xml中的属性按什么顺序调用? I can not reference the project version in the parent pom.xml file. 我无法在父pom.xml文件中引用项目版本。

The code snippet shows the parent pom.xml 该代码段显示了父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">
    <groupId>com.my.project</groupId>
    <artifactId>my.project</artifactId>
    <packaging>pom</packaging>
    <version>${projectVersion}</version>
    <properties>
        <projectVersion>1.0.0</projectVersion>
        ...
    </properties>
    <modules>
        <module>my.project.service</module>
        <module>my.project.db</module>
        ...
    </modules>

I expect the pom to take the version number defined in the properties. 我希望pom采用属性中定义的版本号。 But it failes building with the error invalid reference format 但是由于错误的无效参考格式而无法构建

I expect the pom to take the version number defined in the properties. 我希望pom采用属性中定义的版本号。 But it failes building with the error invalid reference format 但是由于错误的无效参考格式而无法构建

By running a maven goal from the parent project, maven first binds the artifact/group/version in the current build process, so it uses the version defined here : <version>${projectVersion}</version> 通过从父项目运行maven目标,maven首先在当前构建过程中绑定工件/组/版本,因此它使用此处定义的版本: <version>${projectVersion}</version>

I can not reference the project version in the parent pom.xml file. 我无法在父pom.xml文件中引用项目版本。

In this case, you could "cheat" by creating an aggregator/multimodule project that defines a not used/fake version ( 1.0-NOT-USED for example) that has as unique module the parent pom. 在这种情况下,您可以通过创建一个聚合器/多模块项目来“作弊”,该项目定义了未使用/伪造的版本(例如1.0-NOT-USED ),该版本具有作为父模块的唯一模块。 When you build the aggregator/multimodule project, you can now pass as parameter the version to use in the parent project and all its modules. 现在,在构建聚合器/多模块项目时,您可以将要在父项目及其所有模块中使用的版本作为参数传递。

As a side note, to use a central version for a set of projects, you should use the flatten maven plugin that relieves you from the hell version management. 附带说明一下,要将集中版本用于一组项目,应使用扁平化的maven插件 ,该插件可使您摆脱地狱版本管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM