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

发布时间:2020-10-07 07:58:39 作者:学无终
来源:脚本之家 阅读:156

近期工作中要对很多项目加相同的依赖,需要将很多项目都从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);
    }
  }
}

其余代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. 关于Android 项目从svn中检出无R.java文件问题
  2. 搭建svn,创建svn项目

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java svn 导出

上一篇:详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失

下一篇:Java 中 Date 与 Calendar 之间的编辑与转换实例详解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》