Maven 3:访问​​"root"版本公司POM [英] maven 3: Accessing version of "root" corporate POM

查看:73
本文介绍了Maven 3:访问​​"root"版本公司POM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Maven 3.0.4.

Using Maven 3.0.4.

我的任务是为我们的组织提供公司母公司POM.我的团队将为开发人员在使用此POM时遇到的问题提供支持.通常,他们会将构建日志附加到支持通知单上.因此,我希望我的公司POM在任何构建中都将公司父级的版本回显到控制台.我正在为此使用antrun插件.

I am tasked with providing a corporate parent POM for our organization. My team will provide support for questions or issues developers have when using this POM. Often they will attach a build log to a support ticket. So, I want my corporate POM to echo the version of the corporate parent to the console with any build. I'm using the antrun plugin for this.

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
  <execution>
    <id>echo-build-environment</id>
    <phase>validate</phase>
    <goals><goal>run</goal></goals>
    <configuration>
      <target>
        <echo level="info" message="Maven ${maven.version}" taskname="Version" />
        <echo level="info" message="Corporate POMs ${want.the.version.here}" taskname="Version" />
      ....
     </target>

技巧是,我不知道公司父级和构建中使用的POM之间可能会有多少个"POM"继承级别.我们可以有一个像这样的继承结构:

Trick is, I have no idea how many "levels" of POM inheritance may come between the corporate parent and the POM used in the build. We could have an inheritance structure something like this:

corporate-parent
  team-parent
    application-parent
      child-module

或者像这样简单:

corporate-parent
  a-simple-library

我无法回显${project.version},因为它将是当前(子)项目的版本.我不能使用${project.parent.version},因为我不知道可能有多少个继承级别.我尝试定义一个<corporate.pom.version>属性,并将其硬编码为公司POM版本,但是当我发布公司POM时,发行插件不知道要更新该属性(这很有意义,它不是依赖版本,它只是一个属性,发行版本不知道要对其进行更新.)

I cannot echo ${project.version} as that will be the version of the current (child) project. I cannot use ${project.parent.version} because I have no idea how many levels of inheritance there might be. I tried defining a <corporate.pom.version> property, hardcoded to the corporate POM version, however when I release my corporate POM the release plugin doesn't know to update that property (which makes sense, it's not a dependency version, it's just a property, the release version can't know to update it).

理想情况下,我希望能够直接通过诸如${some.groupId.corporate-parent.version}之类的属性来获取特定POM的版本.像那样的东西存在吗?

Ideally, I'd love to be able to get the version of a specific POM directly through a property, something like ${some.groupId.corporate-parent.version}. Does anything like that exist?

如果没有,发布期间是否可以用发布的项目的releaseVersion更新POM属性?

If not, is there a way during a release to update a POM property with the releaseVersion of the project being released?

我可以恢复为在每次发布之前手动编辑属性的蛮力方式.我的队友不会喜欢这种方法.

I could revert to the brute force way of manually editing the property before each release. My teammates wouldn't appreciate that approach.

我希望我不必编写自定义插件即可完成乍一看似乎并不困难的事情.

I'm hoping I don't have to write a custom plugin to do something that at first glance didn't seem to be difficult.

推荐答案

我停止使用maven-antrun-plugin并切换到 GMaven 代替.我可以通过简单的POM层次结构遍历获得所需的信息.

I stopped using maven-antrun-plugin and switched to GMaven instead. I can get the info required with a simple POM hierarchy traversal.

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <id>echo-build-environment</id>
            <phase>validate</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>
                <![CDATA[
                def rootPom = project;
                while (rootPom.parent != null) {
                    rootPom = rootPom.parent;
                }

                project.properties.setProperty('root.pom.version', 
                                                rootPom.version);                            
                log.info("      Maven Home:  " + project.properties['maven.home']);
                log.info("       Java Home:  " + project.properties['java.home']);
                log.info("   Built By User:  " + project.properties['user.name']);
                log.info("Corp POM Version:  " + rootPom.version);
                ]]>
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

project.properties.setProperty部分存储计算出的属性,以便子POM可以访问它.

The project.properties.setProperty part stores the calculated property so it may be accessed by child POMs.

结果日志如下:

[INFO]       Maven Home:  c:\dev\maven\apache-maven-3.0.4
[INFO]        Java Home:  c:\Program Files\Java\jdk1.7.0_13\jre
[INFO]    Built By User:  user944849
[INFO] Corp POM Version:  0.26-SNAPSHOT

这篇关于Maven 3:访问​​"root"版本公司POM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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