9

The buildnumber-maven-plugin is able to get the revision number from git and according to the meager documentation provided it should be providing the branch as well in the ${scmBranch} property. However, all I get is UNKNOWN for the ${scmBranch} property.

Is there something else I need to do to get the branch info from buildnumber-maven-plugin?

Here are the relevant entries from my pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>create</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <doCheck>true</doCheck>
        <doUpdate>true</doUpdate>
    </configuration>
</plugin>

<manifestEntries>
    <Build-Branch>${scmBranch}</Build-Branch>
    <Build-Revision>${buildNumber}</Build-Revision>
    <Build-Timestamp>${maven.build.timestamp}</Build-Timestamp>
</manifestEntries>
Dean Schulze
  • 9,633
  • 24
  • 100
  • 165
  • If you have the opinion of a meager documentation why not [creating a issue](http://jira.codehaus.org/browse/MBUILDNUM) or best make suggestions how to improve the docs or create a patch for the docs. – khmarbaise Feb 20 '14 at 13:37
  • I was being diplomatic when I called the documentation "meager". Given the answer below I should call it misleading. The authors of this plugin must know that it only works with SVN so creating an issue would only tell them what they already know but didn't document: buildnumber-maven-plugin works with svn, not with git. – Dean Schulze Feb 20 '14 at 16:32
  • So why not making a better suggestion how to document it? – khmarbaise Feb 21 '14 at 01:23
  • 3
    The version available today (version 1.4), buildnumber-maven-plugin is setting a value for scmBranch. – HankCa May 26 '16 at 03:25

1 Answers1

16

Short answer the buildnumber plugin 1.2 doesn't print git branches, so check out the jgit-buildnumber or maven-git-commit-id plugins.


No matter how much you configure it, you're not going to get a branch out of the buildnumber plugin 1.2, because it's only looking for SVN branch information.

Instead there are a large number of community contributed git plugins for maven.

Here is an example pom that uses an instance of all three plugins, that you can quickly try out via mvn -f test.xml validate:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.test</groupId>
    <artifactId>git-branch-info</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <scm>
        <connection>scm:git:ssh://path_not_used_in_buildnumber_example/but_scm_type_is</connection>
    </scm>

    <build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>ru.concerteza.buildnumber</groupId>
                <artifactId>maven-jgit-buildnumber-plugin</artifactId>
                <version>1.2.7</version>
                <executions>
                    <execution>
                        <id>jgit-buildnumber</id>
                        <goals>
                            <goal>extract-buildnumber</goal>
                        </goals>
                        <phase>validate</phase>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.1.9</version>
                <executions>
                    <execution>
                        <id>git-commit-id</id>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                        <phase>validate</phase>
                        <configuration>
                            <!-- Only changing prefix since properties conflicts with jgit above -->
                            <prefix>git-commit-id</prefix>
                            <!-- We're using a pom in this example-->
                            <skipPoms>false</skipPoms>
                            <gitDescribe>
                                <!-- Faster to get just branch if skip = true -->
                                <skip>false</skip>
                            </gitDescribe>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>echo-properties</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>validate</phase>
                        <configuration>
                            <target>
                                <echo message="buildnumber-maven-plugin properties:"/>
                                <echo message="  $${scmBranch}:                  ${scmBranch}" />
                                <echo message="  $${buildNumber}:                ${buildNumber}" />
                                <echo message="  $${timestamp}:                  ${timestamp}" />

                                <echo message="maven-jgit-buildnumber-plugin properties:"/>
                                <echo message="  $${git.revision}:               ${git.revision}" />
                                <echo message="  $${git.branch}:                 ${git.branch}" />
                                <echo message="  $${git.tag}:                    ${git.tag}" />
                                <echo message="  $${git.commitsCount}:           ${git.commitsCount}" />
                                <echo message="  $${git.buildnumber}:            ${git.buildnumber}" />


                                <echo message="git-commit-id-plugin properties (aliased with git-commit-id):"/>
                                <echo message="  $${git.branch}:                 ${git-commit-id.branch}" />

                                <echo message="  $${git.commit.id.describe}:     ${git-commit-id.commit.id.describe}" />

                                <echo message="  $${git.build.user.name}:        ${git-commit-id.build.user.name}" />
                                <echo message="  $${git.build.user.email}:       ${git-commit-id.build.user.email}" />
                                <echo message="  $${git.build.time}:             ${git-commit-id.build.time}" />

                                <echo message="  $${git.commit.id}:              ${git-commit-id.commit.id}" />
                                <echo message="  $${git.commit.id.abbrev}:       ${git-commit-id.commit.id.abbrev}" />
                                <echo message="  $${git.commit.user.name}:       ${git-commit-id.commit.user.name}" />
                                <echo message="  $${git.commit.user.email}:      ${git-commit-id.commit.user.email}" />
                                <echo message="  $${git.commit.message.full}:    ${git-commit-id.commit.message.full}" />
                                <echo message="  $${git.commit.message.short}:   ${git-commit-id.commit.message.short}" />
                                <echo message="  $${git.commit.time}:            ${git-commit-id.commit.time}" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
Community
  • 1
  • 1
kshakir
  • 2,142
  • 1
  • 15
  • 9
  • 1
    Your first link above - LOL! How they could write code like that and then claim that the plugin would work with git is unbelievable. jgit-buildnumber does what I need. Thanks. – Dean Schulze Feb 20 '14 at 17:10
  • 3
    jgit-buildnumber doesn't check for uncommitted changes. To get that check I had to add buildnumber-maven-plugin back to the pom.xml, still using jgit-buildnumber for the manifest entries. Best of both plugins. – Dean Schulze Feb 20 '14 at 19:06
  • 9
    The plugin v1.3 will print git branch names. – cslotty Nov 19 '14 at 17:28
  • 2
    The comment about v1.3 working should be promoted as an answer because the question is specific about this plugin. – Constantino Cronemberger Nov 01 '16 at 15:57
  • @Dean I added dirty check to my fork of JGit-BuildNumber plugin as well as many other improvements: [JGit Build Number for Maven, Ant, and Gradle](https://github.com/elab/jgit-buildnumber) – Eugen Labun Apr 20 '19 at 13:25