5

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.

user944849
  • 14,524
  • 2
  • 61
  • 83

2 Answers2

2

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
Jim Hurne
  • 7,187
  • 4
  • 44
  • 44
user944849
  • 14,524
  • 2
  • 61
  • 83
  • 1
    Neat solution. I wish I could use it for providing dependency versions, but it doesn't look like there is a way to run the plugin before dependencies are resolved. Changing `phase` to `initialize` doesn't help. – Pawel Veselov Feb 22 '21 at 11:35
0

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

Tome
  • 3,234
  • 3
  • 33
  • 36
  • Thanks for the idea, I'll have to explore a little. There are many other properties in the POM that would need to remain unchanged by any filtering process. The install and deploy happen as part of `release:perform` already. – user944849 Mar 29 '13 at 21:39
  • I did some exploring with the `@{project.version}` used in the tagNameFormat element of release:prepare & can't get this to work. Couple of issues: release plugin doesn't filter the `` element (checked the source code) and it's hard to filter the POM itself while it's being released. Thank you though! – user944849 Apr 12 '13 at 03:05
  • How exactly does that work? And isn't that three, not two, plugins? – stackexchanger Sep 23 '15 at 17:28