1

I am using the "Merge before build" option of the git plugin to build a maven job. I have tried using ${GIT_BRANCH} and ${GIT_COMMIT} in my maven pom to write the commit information into my built artifacts but these variable are set to the branch being merged and its commit SHA-1.

Is there a way that I can find the SHA-1 of merged code and pass it into maven?

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
Steve Brown
  • 195
  • 1
  • 8
  • I can add a pre-build step to run a shell and execute `cd $WORKSPACE; export GIT_MERGE_COMMIT=`git rev-parse HEAD`` which gets the SHA-1 into the environment variable `GIT_MERGE_COMMIT` but there down't seem to be any good way to get this [into the maven goal](http://stackoverflow.com/questions/3627144/using-a-variable-obtained-using-a-pre-build-shell-command-to-set-an-option-for-t) – Steve Brown Aug 16 '12 at 12:13

2 Answers2

1

The Mojo's Buildnumber Maven Plugin can fetch this information for you.

The project site has not been fully updated to reflect the fact that it works with GIT.

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

That will set the ${buildNumber} property to the full GIT hash of your workspace.

It also will have the side-effect of ensuring that the build is the same on either a developers machine or the CI build server.

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
Stephen Connolly
  • 13,872
  • 6
  • 41
  • 63
  • Thanks, the plugin lets me get the hash I want. For anyone else trying this, note that you need to configure the `maven-scm-plugin` in your pom before the buildnumber plugin can get the hash information – Steve Brown Aug 23 '12 at 00:40
0

You can also try the maven git commit id plugin. It will provide you with a lot of information from Git, it supports fancy formatting and many different use cases.

Direct link to the documentation.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820