Permalink
Browse files

Initial commit

Allows getting the current branch of a Git managed Maven project
  • Loading branch information...
0 parents commit e1603602de97006bec2c0e11008cf5ce51be40b0 @koraktor committed Apr 29, 2011
Showing with 151 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +85 −0 pom.xml
  3. +65 −0 src/main/java/com/github/koraktor/mavanagaiata/GitBranchMojo.java
@@ -0,0 +1 @@
+target
@@ -0,0 +1,85 @@
+<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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.github.koraktor</groupId>
+ <artifactId>mavanagaiata</artifactId>
+ <version>0.0.0</version>
+ <packaging>maven-plugin</packaging>
+
+ <name>Mavanagaiata</name>
+ <description>Maven plugin providing project information from Git</description>
+ <url>http://koraktor.de/mavanagaiata</url>
+ <licenses>
+ <license>
+ <name>BSD license</name>
+ <url>http://www.opensource.org/licenses/bsd-license.php</url>
+ </license>
+ </licenses>
+ <scm>
+ <url>https://github.com/koraktor/mavanagaiata</url>
+ <connection>scm:git:git://github.com/koraktor/mavanagaiata.git</connection>
+ <developerConnection>scm:git:git@github.com:koraktor/mavanagaiata.git</developerConnection>
+ </scm>
+
+ <developers>
+ <developer>
+ <id>koraktor</id>
+ <name>Sebastian Staudt</name>
+ <email>koraktor@gmail.com</email>
+ </developer>
+ </developers>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <repositories>
+ <repository>
+ <id>jgit-repository</id>
+ <name>JGit Maven Repository</name>
+ <url>http://download.eclipse.org/jgit/maven</url>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jgit</groupId>
+ <artifactId>org.eclipse.jgit</artifactId>
+ <version>0.10.1</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-plugin-plugin</artifactId>
+ <version>2.5.1</version>
+ <configuration>
+ <goalPrefix>mavanagaiata</goalPrefix>
+ </configuration>
+ <executions>
+ <execution>
+ <id>generated-helpmojo</id>
+ <goals>
+ <goal>helpmojo</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,65 @@
+package com.github.koraktor.mavanagaiata;
+
+/**
+ * This code is free software; you can redistribute it and/or modify it under
+ * the terms of the new BSD License.
+ *
+ * Copyright (c) 2011, Sebastian Staudt
+ */
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+/**
+ * This goal provides the currently checked out Git branch in the
+ * "mavanagaiata.branch" and "mvngit.branch" properties.
+ *
+ * @author Sebastian Staudt
+ * @goal branch
+ * @phase initialize
+ * @requiresProject
+ */
+public class GitBranchMojo extends AbstractMojo {
+
+ /**
+ * The project base directory
+ *
+ * @parameter expression="${basedir}/.git"
+ * @required
+ */
+ private File gitDir;
+
+ /**
+ * The maven project
+ *
+ * @parameter expression="${project}"
+ * @readonly
+ */
+ private MavenProject project;
+
+ /**
+ *
+ */
+ public void execute() throws MojoExecutionException {
+ try {
+ FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
+ Repository repository = repositoryBuilder
+ .setGitDir(this.gitDir)
+ .readEnvironment()
+ .findGitDir()
+ .build();
+
+ project.getProperties().put("mavanagaiata.branch", repository.getBranch());
+ project.getProperties().put("mvngit.branch", repository.getBranch());
+ } catch (IOException e) {
+ this.getLog().error("Unable to read Git repository");
+ }
+ }
+}

0 comments on commit e160360

Please sign in to comment.