Maven管理的依赖项-从父pom解析$ {project.version} [英] Maven managed dependencies - resolving ${project.version} from parent pom

查看:4632
本文介绍了Maven管理的依赖项-从父pom解析$ {project.version}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析父pom中托管属性的占位符${project.version}?我期望它会在全局范围内解决,因此,当父pom具有版本2时,${project.version}也将被解决为版本2.

在父pom中,我有:

 <groupId>my.group</groupId>
<artifactId>parent</artifactId>
<version>2</version>
<packaging>pom</packaging>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>my.group</groupId>
            <artifactId>dep</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>     
 

在孩子中,我使用

 <parent>
    <groupId>my.group</groupId>
    <artifactId>parent</artifactId>
    <version>2</version>
</parent>
<version>1</version>
<artifactId>child</artifactId>
 

但是使用工件my.group.dep.1.jar代替my.group.dep.2.jar.因此,占位符使用托管的依赖项被解析为项目的版本,而不是被定义为依赖项的项目.

这是预期的行为吗?我正在使用 maven 3.0.4.

解决方案

您必须在子级中跳过<version>标记,但保留<parent><version> ... </parent>标记.

http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance

要注意的一个因素是这些变量是在 如上所述的继承.这意味着如果父项目 使用变量,然后在子级(而不是父级)中定义变量, 将是最终使用的那个.

How is placeholder ${project.version} resolved for managed properties from parent pom? I've expected that it is resolved globally, so when the parent pom has version 2, ${project.version} would also be resolved to version 2.

In parent pom I have:

<groupId>my.group</groupId>
<artifactId>parent</artifactId>
<version>2</version>
<packaging>pom</packaging>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>my.group</groupId>
            <artifactId>dep</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>     

In child I use

<parent>
    <groupId>my.group</groupId>
    <artifactId>parent</artifactId>
    <version>2</version>
</parent>
<version>1</version>
<artifactId>child</artifactId>

But the artifact my.group.dep.1.jar is used, instead of my.group.dep.2.jar. So the placeholder is resolved to the version of the project using the managed dependency, and not those of the project defining the dependency.

Is that expected behaviour? I'm using maven 3.0.4.

解决方案

You have to skip <version> tag in child, but keep the <parent><version> ... </parent> tag.

http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance

One factor to note is that these variables are processed after inheritance as outlined above. This means that if a parent project uses a variable, then its definition in the child, not the parent, will be the one eventually used.

这篇关于Maven管理的依赖项-从父pom解析$ {project.version}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆