Skip to content

Commit

Permalink
MOJO-1199 - Build number plugin to support GIT SCM
Browse files Browse the repository at this point in the history
The build number plugin assumes that SVN is being used.
It would be nice to support GIT as this creates SHA1 revision numbers.
Attached is a patch to do this - the pom.xml will need to be adjusted to use the GIT SCM revision from maven when it is released.
  • Loading branch information
magnayn authored and astubbs committed Apr 13, 2010
1 parent 9e96ed7 commit a7f99ff
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 21 deletions.
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@
<maven.api.version>2.0.6</maven.api.version>
</properties>

<contributors>
<contributor>
<name>Nigel Magnay</name>
<email>nigel.magnay@gmail.com</email>
<roles>
<role>GIT-Patch</role>
</roles>
</contributor>
<contributor>
<name>Markus Heberling</name>
<email>markus@tisoft.de</email>
<roles>
<role>GIT-Patch</role>
</roles>
</contributor>
</contributors>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down Expand Up @@ -144,6 +161,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>${maven.scm.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down Expand Up @@ -216,6 +238,12 @@
</pluginManagement>

<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classifier>git</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-changes-plugin</artifactId>
Expand Down
68 changes: 47 additions & 21 deletions src/main/java/org/codehaus/mojo/build/CreateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.CommandParameter;
import org.apache.maven.scm.CommandParameters;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.ScmRevision;
import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
import org.apache.maven.scm.command.status.StatusScmResult;
import org.apache.maven.scm.command.update.UpdateScmResult;
import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
Expand All @@ -51,9 +55,12 @@
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.apache.maven.scm.provider.git.AbstractGitScmProvider;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
import org.apache.maven.scm.provider.svn.AbstractSvnScmProvider;
import org.apache.maven.scm.provider.svn.command.info.SvnInfoItem;
import org.apache.maven.scm.provider.svn.command.info.SvnInfoScmResult;
import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;
import org.codehaus.plexus.util.StringUtils;

Expand Down Expand Up @@ -602,15 +609,21 @@ public String getScmBranch()
try
{
ScmRepository repository = getScmRepository();
SvnInfoScmResult scmResult = info( repository, new ScmFileSet( scmDirectory ) );
if ( ! scmResult.isSuccess() )
{
getLog().debug( "Cannot get the branch information from the scm repository : " +
scmResult.getCommandOutput() );
return DEFAULT_BRANCH_NAME;
}
SvnInfoItem info = (SvnInfoItem) scmResult.getInfoItems().get( 0 );
scmUrl = info.getURL();

if (repository.getProviderRepository() instanceof SvnScmProviderRepository) {
SvnInfoScmResult scmResult = info( repository, new ScmFileSet( scmDirectory ) );
if ( ! scmResult.isSuccess() )
{
getLog().debug( "Cannot get the branch information from the scm repository : " +
scmResult.getCommandOutput() );
return DEFAULT_BRANCH_NAME;
}
SvnInfoItem info = (SvnInfoItem) scmResult.getInfoItems().get( 0 );
scmUrl = info.getURL();
} else if (repository.getProviderRepository() instanceof GitScmProviderRepository) {
//currently not supported
scmUrl="UNKNOWN";
} else throw new ScmException("No implementation for "+repository.getProvider());
}
catch ( ScmException e )
{
Expand Down Expand Up @@ -654,18 +667,31 @@ public String getRevision()
{
ScmRepository repository = getScmRepository();

SvnInfoScmResult scmResult = info( repository, new ScmFileSet( scmDirectory ) );

checkResult( scmResult );

SvnInfoItem info = (SvnInfoItem) scmResult.getInfoItems().get( 0 );

if ( useLastCommittedRevision )
{
return info.getLastChangedRevision();
}

return info.getRevision();
if (repository.getProviderRepository() instanceof SvnScmProviderRepository) {
SvnInfoScmResult scmResult = info( repository, new ScmFileSet( scmDirectory ) );

checkResult( scmResult );

SvnInfoItem info = (SvnInfoItem) scmResult.getInfoItems().get( 0 );

if ( useLastCommittedRevision )
{
return info.getLastChangedRevision();
}

return info.getRevision();
} else if (repository.getProviderRepository() instanceof GitScmProviderRepository) {

GitRevParseCommand command=new GitRevParseCommand();

command.setLogger(getLogger());
command.setRev("HEAD");

return command.execute(repository.getProviderRepository(),
new ScmFileSet( scmDirectory ),
new CommandParameters()).getCommandOutput().substring(0, 7);

} else throw new ScmException("No implementation for "+repository.getProvider());
}
catch ( ScmException e )
{
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/org/codehaus/mojo/build/GitRevParseCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.codehaus.mojo.build;

import org.apache.maven.scm.CommandParameters;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.command.AbstractCommand;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.apache.maven.scm.provider.git.command.GitCommand;
import org.apache.maven.scm.provider.git.command.info.GitInfoScmResult;
import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;

public class GitRevParseCommand extends AbstractCommand implements GitCommand
{

String rev;



protected ScmResult executeCommand(ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
CommandParameters commandParameters) throws ScmException
{
return executeRevParseCommand(scmProviderRepository, scmFileSet, commandParameters);
}

protected GitInfoScmResult executeRevParseCommand(ScmProviderRepository repo, ScmFileSet fileSet,
CommandParameters commandParameters)
throws ScmException
{

Commandline cl = createCommandLine((GitScmProviderRepository) repo, fileSet);

cl.createArg().setValue(getRev());

GitRevParseConsumer stdin = new GitRevParseConsumer(getLogger());

CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

int exitCode;

exitCode = GitCommandLineUtils.execute(cl, stdin, stderr, getLogger());
if (exitCode != 0)
{
// git-status returns non-zero if nothing to do
getLogger().info("Not a git revision");
return null;
}

return new GitInfoScmResult(cl.toString(), null, stdin.getCommandOutput(), true);
}

// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------

public static Commandline createCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet)
{
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rev-parse");

return cl;
}

public String getRev()
{
return rev;
}

public void setRev(String rev)
{
this.rev = rev;
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/codehaus/mojo/build/GitRevParseConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.codehaus.mojo.build;

import org.apache.maven.scm.log.ScmLogger;
import org.codehaus.plexus.util.cli.StreamConsumer;

public class GitRevParseConsumer
implements StreamConsumer
{
private ScmLogger logger;

// private Map properties = new HashMap();

private StringBuffer commandOutput = new StringBuffer();

// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------

public GitRevParseConsumer(ScmLogger logger) {
this.logger = logger;
}

// ----------------------------------------------------------------------
// StreamConsumer Implementation
// ----------------------------------------------------------------------

public void consumeLine(String line) {
commandOutput.append(line);
}

/**
* The raw output from the command.
*
* @return the raw output from the command
*/
public String getCommandOutput() {
return commandOutput.toString();
}


}

0 comments on commit a7f99ff

Please sign in to comment.