maven 3:访问​​“root"版本企业POM [英] maven 3: Accessing version of "root" corporate POM

查看:35
本文介绍了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 时,发布插件不知道更新该属性(这是有道理的,不是依赖版本,只是属性,release版本不知道更新).

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).

理想情况下,我希望能够直接通过属性获取特定 POM 的版本,例如 ${some.groupId.corporate-parent.version}.有这样的东西吗?

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:devmavenapache-maven-3.0.4
[INFO]        Java Home:  c:Program FilesJavajdk1.7.0_13jre
[INFO]    Built By User:  user944849
[INFO] Corp POM Version:  0.26-SNAPSHOT

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

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