Maven:Pom文件-将依赖项版本外部化为属性文件 [英] Maven: Pom file - externalizing dependency versions to a properties file

查看:313
本文介绍了Maven:Pom文件-将依赖项版本外部化为属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将POM文件中的依赖项版本外部化为属性文件.

I'd like to externalize the dependency versions in the POM file to a properties file.

示例:pom.xml

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${externalized.junit.version}</version>
</dependency>

abc.properties

externalized.junit.version=4.8.2

可以做到吗?如果是这样,最好的方法是什么?

Can this be done? If so, what would be the best way to do it?

推荐答案

不,您不能这样做.

在项目上启动Maven命令时,它要执行的第一步是创建项目的有效模型.特别是,这意味着读取您的POM文件,使用激活的配置文件进行推理,应用继承,对属性执行插值...所有这些都将构建最终的Maven模型.这项工作由 Maven模型构建器组件完成.入口点是

When you're launching a Maven command on a project, the first action it takes is creating the effective model of the project. This notably means reading your POM file, reasoning with activated profiles, applying inheritance, performing interpolation on properties... all of this to build the final Maven model. This work is done by the Maven Model Builder component, whose entry point is the ModelBuilder interface, and exit point is the Model class.

构建模型的过程非常复杂,许多步骤可能分为两个阶段,但是问题的症结很简单:在该步骤的最后,有一个有效的有效模型可供使用由Maven.这意味着所有依赖项必须具有有效的组ID,工件ID,版本;没有重复的依赖项,没有具有相同ID的插件执行,等等.(

The process of building the model is quite complicated, with a lot of steps divided in possibly 2 phases, but the crux of the issue here is simple: at the end of that step, there is a valid effective model to be used by Maven. Which means that all dependencies must have valid group ids, artifact ids, versions; that there are no duplicate dependencies, no plugin execution's with the same ids, etc. (refer to the model description).

请注意,在模型构建期间,插值正在发生,这意味着如果某个属性当时可用,并且已被使用,例如${myProperty},那么它将被其对应的值替换.可用的属性包括:

Take note that during the model building, interpolation is happening, meaning that if a property is available at that time, and it was used, like ${myProperty}, then it will be replaced with its corresponding value. Among the properties available are:

  • POM内容,例如${project.version}${project.artifactId};
  • ${project.basedir},与pom.xml所在的目录相对应;
  • 在命令行上使用-D开关(如-DmyProperty=myValue)设置
  • 用户属性;
  • 直接在POM文件中的<properties>元素内设置的任何属性;
  • 几个内置的Maven属性,例如${maven.build.timestamp},它表示构建开始的日期/时间,或者是当前Maven版本的${maven.version}
  • Java系统属性,由 System.getProperties()
  • 环境变量;
  • 最后在settings.xml文件内部设置属性 .
  • POM content, like ${project.version} or ${project.artifactId};
  • ${project.basedir}, which corresponds to the directory where the pom.xml is;
  • user properties set on the command line with the -D switch (like -DmyProperty=myValue);
  • any properties set directly in the POM file, inside the <properties> element;
  • several built-in Maven properties, like ${maven.build.timestamp}, which represents the date/time at which the build started, or ${maven.version} for the current Maven version;
  • Java system properties, as returned by System.getProperties()
  • environment variables;
  • and finally properties set inside the settings.xml file.

此处的关键点是,在构建有效模型时,依赖项的版本必须有效.这是确保依赖关系图在构建过程中稳定且一致的唯一方法.

The critical point point here, is that the version of dependencies must be valid when the effective model is built. This is the only way to make sure the dependency graph is stable and consistent during the build.

这正是此处失败的原因:您希望Maven能够读取属性文件中的版本,因此这意味着将插件绑定到生命周期的特定阶段以读取该文件,并以某种方式引用读取的属性使用标准的Maven属性.麻烦的是,这一切都将在模型已经建立之后发生,因此为时已晚.所有这些过程都是在构建生命周期发生之前进行的甚至开始在此之前没有机会调用插件.

This is exactly what is failing here: you would want Maven to be able to read versions inside a properties file, so it means binding a plugin to a specific phase of the lifecycle to read that file and somehow refer to the properties read using a standard Maven properties. Trouble is, this would all be happening after the model is already built, so it is too late for that. All of this process is happening before the build lifecycle has even started; no chance to invoke a plugin before that.

这还意味着,如果您将该属性定义为命令行属性,它将起作用(因为如上所述,由于在构建模型时在插值过程中可用).但这不是最佳实践:将依赖项版本指定为命令行属性会使生成非常难以复制.最好将其指定为POM的<properties>元素内的Maven属性,或使用

This also implies that it would work if you were to define the property as a command-line property (since, as outlined above, it is available during the interpolation process when building the model). But that is not a best practice: specifying dependency version as a command-line property makes the build very hard to reproduce. Better to specify it as a Maven property inside the <properties> element of the POM, or to make use of the <dependencyManagement> scheme in a parent POM or also importing a BOM.

这篇关于Maven:Pom文件-将依赖项版本外部化为属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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