17

I'm using Maven 3.0.3 with Git. I use an integration tool (Bamboo) to check out a branch of code from Git into a directory. The tool then uses Maven run the standard build lifecycle (compile, test, deploy). What I want is that if my Maven deploy task succeeds, I want to tag the version of my code that is checked out in Git. How can I do this from Maven? Any sample configurations you can provide are greatly appreciated.

Dave
  • 15,639
  • 133
  • 442
  • 830

4 Answers4

9

Use Maven SCM plugin. See tag functionality in advanced features, which should be relevant.

Now, git support doesn't come out of the box, so you'll need a dependency to maven-scm-provider-gitexe. Also, to overcome plexus exception issue, you'll also need to add a dependency to a later version of plexus.

This is what worked for me:

<project>
    <scm>
      <connection>scm:git:https://username@github.com/my-project.git</connection>
      <developerConnection>scm:git:https://username@github.com/my-project.git</developerConnection>
    </scm>
    <!-- snip -->
    <build>
      <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
         <dependencies>
            <dependency>
               <groupId>org.codehaus.plexus</groupId>
               <artifactId>plexus-utils</artifactId>
               <version>2.1</version>
            </dependency>
            <dependency>
               <groupId>org.apache.maven.scm</groupId>
               <artifactId>maven-scm-provider-gitexe</artifactId>
               <version>1.2</version>
           </dependency>
         </dependencies>
        <version>1.0</version>
        <configuration>
          <tag>test</tag>
          <connectionType>connection</connectionType>
        </configuration>
        <executions>
          <execution>
          <id>tag</id>
          <phase>deploy</phase>
          <goals>
            <goal>tag</goal>
          </goals>
          </execution>
        </executions>
       </plugin>
     </plugins>
    </build>
    <!-- snip -->
</project>
Lii
  • 11,553
  • 8
  • 64
  • 88
eis
  • 51,991
  • 13
  • 150
  • 199
  • COol, this does what I want, although when I run it back-to-back times, I get a "[ERROR] fatal: tag 'qa_release' already exists" error on the second run. I may have to open this as another question. – Dave Jan 18 '12 at 14:36
  • yes, you'll need something dynamic (a property) for the tag name, in this example it's static. – eis Jan 18 '12 at 15:33
  • if the latest tag can be given as a command line parameter, you can just use ${parameter.version} and run maven with -Dparameter.version=v1.2 which would be then used. If autogeneration by maven plugin is needed, some additional logic is required. – eis Jan 18 '12 at 16:06
  • `${project.version}` is reasonable as tag name. – Nowaker Jan 22 '12 at 02:27
3

maven-release-plugin needs only to declare the scm:

<scm>
    <url>https://github.com/username/repoName</url>
    <connection>scm:git:git://github.com/username/repoName.git</connection>
    <developerConnection>scm:git:git@github.com:username/repoName.git</developerConnection>
    <tag>HEAD</tag>
</scm>

generate git ssh keys

https://help.github.com/articles/generating-ssh-keys/

and run mvn release:prepare

more from https://github.com/kevinsawicki/github-maven-example

Edoardo
  • 4,485
  • 1
  • 27
  • 31
2

The maven-release-plugin can do this for you -- see an example here: http://maven.apache.org/plugins/maven-release-plugin/examples/prepare-release.html

rayd
  • 360
  • 2
  • 6
-1

I recommend the small open source project I'm part of -it's called Quicktag and works with couple of VCSes - https://code.google.com/p/quicktag-maven-plugin. Add the plugin and it will generate Java class with static fields that contain build information.