Question

We use Maven for our builds and Mercurial for our changesets. While our software has a major version handled already we would really like to be able to know what Mercurial changeset was used to build any server that runs our software.

Does anybody know of a way in Maven to grab the working directory's changeset in Mercurial and get it into a properties file or something so we can then display it somewhere in our application when sys admins do a "sanity check" against what version is currently running?

Was it helpful?

Solution

Merge this to your pom.xml:

<project>
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>hgchangeset</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Then make a .properties file in src/main/resources with a property set as ${changeSet}. For example:

revision = ${changeSet}
modificationTime = ${changeSetDate}

OTHER TIPS

You could make an update hook which outputs the changeset ID into an unversioned .properties file:

[hooks]
update = echo changesetid=$HG_PARENT1 > version.properties

Advantage of this approach is that you can easily customize this value if needed, and the build stays independent of the versioning system (or lack thereof).

If you want to put something in the Maven build that generates it instead, have you looked at the Buildnumber Maven Plugin (hgchangeset goal) or Maven Mercurial Build Number Plugin?

If you can intercept command output (into environment variable, f.e) hg id -i will be easy way. More complex ids can be constructed with hg log --template "..." tip

You could use Maven's antrun plugin to run a <exec> or <java> task which generates a properties file with that information. That's not very elegant, though.

You can also use https://github.com/volodya-lombrozo/hg-revision-plugin, if you need more properties, than org.codehaus.mojo.buildnumber-maven-plugin.

 <build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>com.github.volodya-lombrozo</groupId>
            <artifactId>hg-revision-plugin</artifactId>
            <version>0.2</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>
                            scan
                        </goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Then you can use second properties:

hg.author=${hg.author}
hg.branch=${hg.branch}
hg.revision=${hg.rev}
hg.node=${hg.node}
hg.tags=${hg.tags}
hg.desc=${hg.desc}
hg.date=${hg.date}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top