Howdy,
I needed to fetch the part of an SCM url that provides a location like
"trunk" or "branches/issue_1820", so I modified
buildnumber-maven-plugin. Knowing the current branch (or tag) helps
distinguish builds since sometimes different SCM locations can have
the same revision number but have different content.
For instance, in Subversion it is possible to checkout
http://example.com/svn/trunk or
http://example.com/svn/branches/issue_1820 at revision "12798" and get
entirely different code.
This patch exposes the varible $buildScmBranch as a property,
similarly to how $buildNumber is currently exposed.
Problems with this patch:
* I probably didn't account for all error conditions properly
* only works with Subversion
* only works with "recommended" directory layout, ie: paths must
contain "/trunk", "/branches/X", or "/tags/X"
* I didn't do much documentation besides a little Javadoc and an
update of the "usage" page
This patch was created against SVN revision 9922 from a checkout
rooted at http://svn.codehaus.org/mojo/trunk/mojo/buildnumber-maven-plugin
.
Hope you find this useful too,
-Adam
Index: src/test/java/org/codehaus/mojo/build/TestCreateMojo.java
===================================================================
--- src/test/java/org/codehaus/mojo/build/TestCreateMojo.java (revision 9922)
+++ src/test/java/org/codehaus/mojo/build/TestCreateMojo.java (working copy)
@@ -126,4 +126,14 @@
}
+ public void testFilterBranchFromScmUrl()
+ {
+ CreateMojo mojo = new CreateMojo();
+ String scmUrlTrunk = "https://mifos.dev.java.net/svn/mifos/trunk";
+ assertEquals("trunk", mojo.filterBranchFromScmUrl(scmUrlTrunk));
+ String scmUrlBranch = "https://mifos.dev.java.net/svn/mifos/branches/v1.2.x";
+ assertEquals("branches/v1.2.x", mojo.filterBranchFromScmUrl(scmUrlBranch));
+ String scmUrlTag = "https://mifos.dev.java.net/svn/mifos/tags/v1.2.1";
+ assertEquals("tags/v1.2.1", mojo.filterBranchFromScmUrl(scmUrlTag));
+ }
}
Index: src/main/java/org/codehaus/mojo/build/CreateMojo.java
===================================================================
--- src/main/java/org/codehaus/mojo/build/CreateMojo.java (revision 9922)
+++ src/main/java/org/codehaus/mojo/build/CreateMojo.java (working copy)
@@ -64,7 +64,8 @@
* make sure that you have checked everything into scm, before issuing the build number. That
* behaviour can be suppressed, and then the latest local build number is used. Build numbers are
* not reflected in your artifact's filename (automatically), but can be added to the metadata. You
- * can access the build number in your pom with ${buildNumber}. You can also access ${timestamp}.
+ * can access the build number in your pom with ${buildNumber}. You can also access ${timestamp} and
+ * the scm branch of the build (if applicable) in ${buildScmBranch}
*
* @author <a href="mailto:[email protected]">Julian Wood</a>
* @version $Id$
@@ -256,6 +257,15 @@
*
*/
private boolean getRevisionOnlyOnce;
+
+ /**
+ * You can rename the buildScmBranch property name to another property name if desired.
+ *
+ * @parameter expression="${maven.buildNumber.buildScmBranchPropertyName}"
+ * default-value="buildScmBranch"
+ * @since 1.0-beta-4
+ */
+ private String buildScmBranchPropertyName;
/**
@@ -432,6 +442,10 @@
}
project.getProperties().put( timestampPropertyName, timestamp );
+ String buildScmBranch = getBuildScmBranch();
+ getLog().info("Storing buildScmBranch: " + buildScmBranch);
+ project.getProperties().put( buildScmBranchPropertyName, buildScmBranch );
+
// Add the revision and timestamp properties to each project in the reactor
if ( getRevisionOnlyOnce && reactorProjects != null )
{
@@ -556,6 +570,45 @@
}
/**
+ * Get the branch info for this revision from the repository. For svn, it is in svn info.
+ *
+ * @return
+ * @throws MojoExecutionException
+ * @throws MojoExecutionException
+ */
+ public String getBuildScmBranch() throws MojoExecutionException {
+ String scmUrl;
+ try
+ {
+ ScmRepository repository = getScmRepository();
+ SvnInfoScmResult scmResult = info( repository, new ScmFileSet( scmDirectory ) );
+ checkResult( scmResult );
+ SvnInfoItem info = (SvnInfoItem) scmResult.getInfoItems().get( 0 );
+ scmUrl = info.getURL();
+ }
+ catch ( ScmException e )
+ {
+ throw new MojoExecutionException( "Cannot get the branch information from the scm repository : \n"
+ + e.getLocalizedMessage(), e );
+ }
+
+ return filterBranchFromScmUrl(scmUrl);
+ }
+
+ protected String filterBranchFromScmUrl(String scmUrl) {
+ String scmBranch = "UNKNOWN";
+ if (scmUrl.contains("/trunk"))
+ {
+ scmBranch = "trunk";
+ }
+ else if (scmUrl.contains("/branches") || scmUrl.contains("/tags"))
+ {
+ scmBranch = scmUrl.replaceFirst(".*((branches|tags)[^/]*).*?", "$1");
+ }
+ return scmBranch;
+ }
+
+ /**
* Get the revision info from the repository. For svn, it is svn info
*
* @return
Index: src/site/apt/usage.apt
===================================================================
--- src/site/apt/usage.apt (revision 9922)
+++ src/site/apt/usage.apt (working copy)
@@ -100,6 +100,9 @@
</build>
+------------------------------------------+
+ The <<<$\{buildScmBranch\}>>> property is also populated to provide
+ the SCM branch.
+
<<Note:>> Older version of the jar and war plugin don't require the
manifest/addDefaultImplementationEntries element, and in fact will fail
if present. The new versions require this element.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email