Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven 3: Accessing version of "root" corporate POM

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.

like image 457
user944849 Avatar asked Nov 03 '22 01:11

user944849


1 Answers

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
like image 110
user944849 Avatar answered Nov 09 '22 14:11

user944849