Maven:如何读取父POM版本 [英] Maven: How to read the Parent POM version

查看:2550
本文介绍了Maven:如何读取父POM版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我的大多数Java项目都有很多样板配置,最终我将它们写到POM中. (例如,sourceEncoding始终为UTF-8,组织详细信息永远不会更改).为了省去复制粘贴配置块的麻烦,我创建了自己的标准父级" POM,可以将其扩展到每个项目.

Background: Most of my Java project have a lot of boiler-plate configuration that I end up writing into my POM. (For example the sourceEncoding is always UTF-8, the organization details never change). To save having to copy-paste blocks of configuration I've created my own standard 'parent' POM I can extend for each of my projects.

我还有很多我在工作的每个Java项目中使用的工具(例如checkstyle,PMD等),因此我已将每个工具的插件添加到我的标准父pom中.

I also have a number of tools that I use in every Java project I work on (e.g. checkstyle, PMD etc) so I've added the plug-ins for each tool into my standard parent pom.

PMD(和其他一些工具)有一个有趣的问题-它们需要一组配置文件才能正确运行.由于Maven倾向于使用每个模块一个可部署的资源"的概念,因此我创建了以下(简化的)结构:

PMD (and a few other tools) have an interesting problem - they requires a set of configuration files to operate correctly. Since Maven prefers to work with the concept of 'one deployable resource per module' I've created the following (simplified) structure:

<My Template>
  |--- Config Files
  \--- Parent Pom

  • 我的模板:是一个由maven控制的项目,具有两个模块
  • 配置文件:是一个Maven模块,它将PMD配置文件捆绑到ZIP中,并将它们部署到Maven存储库中.
  • 父Pom:是我所有的模板代码.它与配置文件"有关,并且设置为从ZIP中提取配置
    • My Template: is a maven controlled project with two modules
    • Config Files: is a maven module that bundles the PMD config files into a ZIP and deploys them to a maven repo
    • Parent Pom: is all my boiler plate code. It has a dependency on "Config Files" and is set up to extract the configuration from the ZIP
    • 到目前为止一切都很好....

      So far so good....

      要求:父Pom"和配置文件"始终一起构建和部署-即父Pom"版本1.2.3只能与配置文件" 1.2版一起使用.3,所以当我在"Parent POM"中编写依赖项时,我需要执行以下操作:

      The Requirment: "Parent Pom" and "Config Files" are always built and deployed together - that is "Parent Pom" version 1.2.3 will only ever work with "Config Files" version 1.2.3, so when I write the dependency in "Parent POM" I need to do something like:

      <dependency>
          <groupId>org.my-org</groupId>
          <artifactId>config-files</artifactId>
          <version>${project.version}</version>
          <type>zip</type>
          <scope>test</scope>
      </dependency>
      

      问题:当我开始启动应用程序(例如0.0.1版)时,我以"Parent Pom"作为父母. Maven将计算项目的有效POM,$ {project.version}将解释为0.0.1(应用程序版本)而不是1.2.3(父POM版本).然后,Maven无法找到配置文件.

      The Problem: When I come to start my application (let's say version 0.0.1) I use "Parent Pom" as my parent. Maven will calculate the effective POM for the project and ${project.version} will be interpreted as 0.0.1 (the application version) rather than 1.2.3 (the parent POM version). Maven then fails to find the config files.

      问题:我如何告诉Maven给我POM xxx版本"?

      The Question: How can I tell Maven to "Give me version for POM xxx"?

      我真正不做的是创建与$ {project.version}紧紧配合的其他属性,因为我可以保证当我们为发行版的POM贴标时我们会忘记更新它!

      What I really don't to do is creating additional properties that are in lockstep with ${project.version} because I can guarantee that we'll forget to update it when we Bump the POMs for a release!

      推荐答案

      您可以做一些事情.

      您可以使用${project.parent.version}代替${project.version}.

      ,但是更好的方法可能是在父pom中定义pluginManagement和/或dependencyManagement-具有适当的版本和配置.在您的子项目中,您仅使用" dependenciesbuild/plugins中的托管"版本/配置,而无需指定具体版本或配置.

      But probably a better way would be to define pluginManagement and/or dependencyManagement in your parent pom - with appropriate versions and configuration. In you child projects you just "use" the "managed" version/configurations in dependencies or build/plugins without specification of concrete versions or configuration.

      例如,请参见 org.sonatype.oss:oss-parent POM,广泛用于开源项目.

      For example see the org.sonatype.oss:oss-parent POM which is widely used in open-source projects.

      这篇关于Maven:如何读取父POM版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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