可以将父版本作为属性提供给孩子吗?

时间:2016-11-01 12:18:51

标签: java maven dependencies pom.xml dependency-management

这是关于Maven POM的

如果我希望我的父版本也是我的依赖版本,我必须设置一个属性值为$ {project.parent.version}。

问题然后发生在我的主POM的一个孩子(其中包含$ {project.parent.version}属性,因为它是一个项目我不管理的父项)重新计算属性并认为该值创建的Property现在是我的Main POM的版本。

--SuperParent (not in my Administration) | Version = 1.2.3

----MainPom | Version = 1.0.0 | Property <test>${project.parent.version}</test> -> 1.2.3

------Child Pom | Version 1.0.0 | Property ${test} is now 1.0.0
<project>
<!-- Super Pom -->
 <groupId>groupId</groupId>
    <artifactId>artifactId</artifactId>
    <version>1.2.3</version>
</project>

<project>
<!-- MainPom -->
    <groupId>othergroupId</groupId>
    <artifactId>otherartifactId</artifactId>
    <version>1.0.0</version>
    <parent>
         <groupId>groupId</groupId>
         <artifactId>artifactId</artifactId>
         <version>1.2.3</version>
     </parent>
     <properties>
     <dependency.version>${project.parent.version}</dependency.version>
     </properties>
     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>dependencyGroupId<groupId>
                <artifactId>dependency</artifactId>
                <version>${dependency.version}</version>
            </dependency>
        </dependencies>
     </dependencyManagement>
</project>
<project>
<!-- ChildPom -->
    <groupId>childGroupId</groupId>
    <artifactId>childArtifactId</artifactId>
    <version>1.0.0</version>
    <parent>
         <groupId>othergroupId</groupId>
         <artifactId>otherartifactId</artifactId>
         <version>1.0.0</version>
     </parent>
        <dependencies>
            <dependency>
                <groupId>dependencyGroupId<groupId>
                <artifactId>dependency</artifactId>
            </dependency>
        </dependencies>
</project>

End是Child Pom 1.0.0中的Property $ {dependency.version}而不是1.2.3。 这是Maven的通缉行为吗?我能做些什么让它起作用?

无法改变的事情:

  • SuperPom
  • 主要Pom版本

1 个答案:

答案 0 :(得分:4)

Maven第一个进程继承构建有效的pom然后进程变量扩展。

换句话说,父和子pom内容被处理为每个子pom的单个合并文件。所以当你的孩子pom被处理时,$ {project.parent.version}是1.0.0,而不是1.2.3。

我找不到引用pom的“祖父母”的方法,所以似乎唯一的解决方案是将版本作为静态数字放在parent.version和properties.dependency.version中。 / p>