maven 3: Accessing version of “root” corporate POM

2019-01-15 15:54发布

Using Maven 3.0.4.

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>

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

Or as simple as this:

corporate-parent
  a-simple-library

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

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?

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.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-15 16:13

You can achieve property expansion in installed/deployed POM by using a combination of two plugins: - maven-resources-plugin to generate a filtered version of your pom.xml - maven-install-plugin and maven-deploy-plugin, using the install-file and deploy-file mojos, to install/deploy the filtered POM.

This process could for instance be triggered during the releaser process

查看更多
倾城 Initia
3楼-- · 2019-01-15 16:29

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>

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

Resulting log looks like:

[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
查看更多
登录 后发表回答