使用Plexus,使用 Maven SCM 非常容易,因为它在字段中注入所有依赖项,因此您只需编写最少的代码。
import org.apache.maven.scm.manager.ScmManager;
public class MyApp
{
private ScmManager scmManager;
public MyApp()
{
plexus = new Embedder();
plexus.start();
scmManager = (ScmManager) plexus.lookup( ScmManager.ROLE );
}
public ScmManager getScmManager()
{
return scmManager;
}
如果没有 Plexus,您必须在管理器中添加所有 SCM 提供程序,这将需要更多工作。您可以使用基本的 SCM 管理器或编写自己的:
import org.apache.maven.scm.manager.BasicScmManager;
public class MyApp
{
private ScmManager scmManager;
public MyApp()
{
scmManager = new BasicScmManager();
//Add all SCM providers we want to use
scmManager.setScmProvider( "git", new GitExeScmProvider() );
scmManager.setScmProvider( "svn", new SvnExeScmProvider() );
...
}
public ScmManager getScmManager()
{
return scmManager;
}
在调用命令之前,SCM 管理器需要一个ScmRepository。此对象包含有关 SCM 连接的所有信息。
public ScmRepository getScmRepository( String scmUrl )
throw Exception
{
ScmRepository repository;
try
{
return getScmManager().makeScmRepository( scmUrl );
}
catch ( NoSuchScmProviderException ex )
{
throw new Exception( "Could not find a provider.", ex );
}
catch ( ScmRepositoryException ex )
{
throw new Exception( "Error while connecting to the repository", ex );
}
}
public void checkOut( ScmRepository scmRepository, File workingDirectory )
throws ScmException
{
if ( workingDirectory.exists() )
{
System.err.println( "The working directory already exist: '" + workingDirectory.getAbsolutePath() + "'." );
return;
}
if ( !workingDirectory.mkdirs() )
{
System.err.println(
"Error while making the working directory: '" + workingDirectory.getAbsolutePath() + "'." );
return;
}
CheckOutScmResult result = scmManager.checkOut( scmRepository, new ScmFileSet( workingDirectory ) );
checkResult( result );
List checkedOutFiles = result.getCheckedOutFiles();
System.out.println( "Checked out these files: " );
for ( Iterator it = checkedOutFiles.iterator(); it.hasNext(); )
{
ScmFile file = (ScmFile) it.next();
System.out.println( " " + file.getPath() );
}
}
public void update( ScmRepository scmRepository, File workingDirectory )
throws ScmException
{
if ( !workingDirectory.exists() )
{
System.err.println( "The working directory doesn't exist: '" + workingDirectory.getAbsolutePath() + "'." );
return;
}
UpdateScmResult result = scmManager.update( scmRepository, new ScmFileSet( workingDirectory ) );
checkResult( result );
List updatedFiles = result.getUpdatedFiles();
System.out.println( "Updated these files: " );
for ( Iterator it = updatedFiles.iterator(); it.hasNext(); )
{
ScmFile file = (ScmFile) it.next();
System.out.println( " " + file.getPath() );
}
}
在每个示例命令代码中,我们使用checkResult方法,它不是必需的,但如果命令执行失败,它会很有用。
public void checkResult( ScmResult result )
throws Exception
{
if ( !result.isSuccess() )
{
System.err.println( "Provider message:" );
System.err.println( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
System.err.println( "Command output:" );
System.err.println( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
throw new Exception(
"Command failed." + StringUtils.defaultString( result.getProviderMessage() ) );
}
}
上面的代码可在此处获得:Maven SCM 客户端。