java svn导出 Java批量从svn导出多个项目代码实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java svn导出 Java批量从svn导出多个项目代码实例

学无终   2021-04-21 我要评论

近期工作中要对很多项目加相同的依赖,需要将很多项目都从svn导出,感觉一个个导太慢了,由于不会写脚本就从晚上找到svn拉代码的程序,稍作修改很快就拉完了所有代码。直接上必要代码

必要pom

<dependency>
 <groupId>org.tmatesoft.svnkit</groupId>
 <artifactId>svnkit</artifactId>
 <version>1.10.1</version>
</dependency>
 
<dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <version>1.18.8</version>
</dependency>

必要代码

@Slf4j
public class SvnService {
  private SVNClientManager clientManager;
 
  public void checkOut(final SvnConfig config) {
    final String user = config.getSourceSvnUser();
    final String password = config.getSourceSvnPassword();
    final String sourceSvn = config.getSourceSvn() + config.getSourceProject();
    try {
      //初始化支持svn://协议的库。 必须先执行此操作。
      SVNRepositoryFactoryImpl.setup();
 
      //相关变量赋值
      SVNURL repositoryURL = SVNURL.parseURIEncoded(sourceSvn);
      ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
      //实例化客户端管理类
      this.clientManager = SVNClientManager.newInstance(
          (DefaultSVNOptions) options, user, password);
      //要把版本库的内容check out到的目录
      File wcDir = new File(config.getSourceCheckOutDir());
      //通过客户端管理类获得updateClient类的实例。
      SVNUpdateClient updateClient = this.clientManager.getUpdateClient();
      // sets externals not to be ignored during the checkout
      updateClient.setIgnoreExternals(false);
      //执行check out操作,返回工作副本的版本号。
      long workingVersion = updateClient.doCheckout(
          repositoryURL, wcDir,
          SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY,
          false);
 
      log.info("VERSION:{} check out to {}", workingVersion, wcDir);
    } catch (Exception e) {
      log.error("SvnService.doCheckOut error: ", e);
    }
  }
 
  public void update(final SvnConfig config) {
    final String user = config.getSourceSvnUser();
    final String password = config.getSourceSvnPassword();
    final String sourceSvn = config.getSourceSvn() + config.getSourceProject();
    try {
      //初始化支持svn://协议的库。 必须先执行此操作。
      SVNRepositoryFactoryImpl.setup();
      //相关变量赋值
      SVNURL.parseURIEncoded(sourceSvn);
      ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
      //实例化客户端管理类
      this.clientManager = SVNClientManager.newInstance(
          (DefaultSVNOptions) options, user, password);
 
      //要更新的文件
      File updateFile = new File(config.getSourceCheckOutDir());
      //获得updateClient的实例
      SVNUpdateClient updateClient = this.clientManager.getUpdateClient();
      updateClient.setIgnoreExternals(false);
      //执行更新操作
      long versionNum = updateClient.doUpdate(updateFile, SVNRevision.HEAD, SVNDepth.INFINITY, false, false);
      log.info("updated version is {}", versionNum);
    } catch (Exception e) {
      log.info(e.getMessage() + "{}", e);
    }
  }
 
  public void commit(final SvnConfig config) {
    final String user = config.getSourceSvnUser();
    final String password = config.getSourceSvnPassword();
    final String sourceSvn = config.getSourceSvn() + config.getSourceProject();
    try {
      //初始化支持svn://协议的库。 必须先执行此操作。
      SVNRepositoryFactoryImpl.setup();
      //相关变量赋值
      SVNURL.parseURIEncoded(sourceSvn);
      ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
      //实例化客户端管理类
      this.clientManager = SVNClientManager.newInstance(
          (DefaultSVNOptions) options, user, password);
      //要提交的文件
      File commitFile = new File(config.getSourceCheckOutDir());
 
      //获取此文件的状态(是文件做了修改还是新添加的文件?)
 
      SVNStatus status = this.clientManager.getStatusClient().doStatus(commitFile, true);
 
      //如果此文件是新增加的则先把此文件添加到版本库,然后提交。
      if (status.getContentsStatus() == SVNStatusType.STATUS_UNVERSIONED) {
 
        //把此文件增加到版本库中
        this.clientManager.getWCClient().doAdd(commitFile, false, false, false, SVNDepth.INFINITY, false, false);
        //提交此文件
        this.clientManager.getCommitClient().doCommit(
            new File[]{commitFile}, true, "", null, null, true, false, SVNDepth.INFINITY);
        System.out.println("add");
      }
 
      //如果此文件不是新增加的,直接提交。
      else {
        this.clientManager.getCommitClient().doCommit(
            new File[]{commitFile}, true, "", null, null, true, false, SVNDepth.INFINITY);
        System.out.println("commit");
 
      }
 
      System.out.println(status.getContentsStatus());
    } catch (Exception e) {
      log.error(e.getMessage() + "{}", e);
    }
  }
}

其余代码

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们