Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...