Home > howto, maven, maven shorties > Using SCM from within your Mojo

Using SCM from within your Mojo

No Gravatar

There is a guide on the topic from Maven. But that guide is missing an example on how to use it from within Mojo.

So let me show you a short example on a topic…

Prepare the POM of your plugin:

...
<properties>
	<scm.version>1.4</scm.version>
</properties>	

<dependencies>

    <dependency>
      <groupId>org.apache.maven.scm</groupId>
      <artifactId>maven-scm-providers-standard</artifactId>
      <version>${scm.version}</version>
      <type>pom</type>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.scm</groupId>
      <artifactId>maven-scm-manager-plexus</artifactId>
      <version>${scm.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.scm</groupId>
      <artifactId>maven-scm-api</artifactId>
      <version>${scm.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.scm</groupId>
      <artifactId>maven-scm-provider-svn-commons</artifactId>
      <version>${scm.version}</version>
    </dependency>
	...

It is crucial to use SCM version 1.4, as 1.5 (the current release) have a nasty bug: SCM-618

Now the example, which would checkout the current project based on the developer connection from the POM file:

import java.io.File;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.repository.ScmRepository;

public class ScmExample extends AbstractMojo {

	/** SCM Manager component to be injected.
	 * @component
	 */
	private ScmManager scmManager;

	/**
	 * The Maven project.
	 *
	 * @parameter expression="${project}"
	 * @required
	 * @readonly
	 */
	private MavenProject project;

	/** SCM working copy path.
	 *  @parameter expression="${wcDirectory}"
	 */
	private File wcDir;

	public void execute() throws MojoExecutionException, MojoFailureException {

		//get data from project
		String developerConnection = project.getScm().getDeveloperConnection();

		ScmProvider scmProvider;
		try {
			scmProvider = scmManager.getProviderByUrl(developerConnection);
			ScmRepository scmRepository = scmManager.makeScmRepository(developerConnection);
			CheckOutScmResult scmResult = scmProvider.checkOut(scmRepository, new ScmFileSet(wcDir));
			if (!scmResult.isSuccess()) {
				getLog().error(String.format("Fail to chckout artifact %s to %s", project.getArtifact(), wcDir));
			}
		} catch (Exception e) {
			throw new MojoExecutionException("Fail to checkout.", e);
		}

	}

}

That’s it.

Usually you would use other projects SCM to do something, but for a sake of simplicity I did not mixed that in.

 

Share
  1. No comments yet.
  1. No trackbacks yet.