操作svn

public class SVNKit {
	private static Logger logger = LoggerFactory.getLogger(SVNKit.class);
	// 更新状态 true:没有程序在执行更新,反之则反
	public static Boolean DoUpdateStatus = true;

	// 声明SVN客户端管理类
	private static SVNClientManager ourClientManager;

	private String SvnPath;//svn仓库路径
	private String TargetPath;//本地仓库路径
	private String SvnUserName;
	private String SvnPassWord;

	public SVNKit(String SvnPath,String TargetPath,String SvnUserName,String SvnPassWord){
		this.SvnPath = SvnPath;
		this.TargetPath = TargetPath;
		this.SvnUserName = SvnUserName;
		this.SvnPassWord = SvnPassWord;
	}
	/**
	 * SVN检出
	 *
	 * @return Boolean
	 */
	public Boolean checkOut() {
		// 初始化库。 必须先执行此操作。具体操作封装在setupLibrary方法中。
		/*
		 * For using over http:// and https://
		 */
		DAVRepositoryFactory.setup();
		/*
		 * For using over svn:// and svn+xxx://
		 */
//		SVNRepositoryFactoryImpl.setup();

		/*
		 * For using over file:///
		 */
//		FSRepositoryFactory.setup();

		// 相关变量赋值
		SVNURL repositoryURL = null;
		try {
			repositoryURL = SVNURL.parseURIEncoded(SvnPath);
		} catch (SVNException e) {
			logger.error(e.getMessage(),e);
			return false;
		}

		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);

		// 实例化客户端管理类
		ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, SvnUserName, SvnPassWord);

		// 要把版本库的内容check out到的目录
		File wcDir = new File(TargetPath);

		// 通过客户端管理类获得updateClient类的实例。
		SVNUpdateClient updateClient = ourClientManager.getUpdateClient();

		updateClient.setIgnoreExternals(false);

		// 执行check out 操作,返回工作副本的版本号。
		long workingVersion = -1;
		try {
			/*if (!wcDir.exists()) {
				workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false);
			} else {
				ourClientManager.getWCClient().doCleanup(wcDir);
				workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false);
			}*/
			workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false);
		} catch (SVNException e) {
			logger.error(e.getMessage(),e);
			return false;
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return false;
		}
		logger.info("把版本:" + workingVersion + " check out 到目录:" + wcDir + "中。");
		return true;

	}

	/**
	 * 解除svn Luck
	 *
	 * @return Boolean
	 */
	public Boolean doCleanup() {
		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		// 实例化客户端管理类
		ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, SvnUserName, SvnPassWord);

		// 要把版本库的内容check out到的目录
		File wcDir = new File(TargetPath);
		if (wcDir.exists()) {
			try {
				ourClientManager.getWCClient().doCleanup(wcDir);
			} catch (SVNException e) {
				logger.error(e.getMessage(),e);
				return false;
			}
		} else {
			return false;
		}
		return true;
	}

	/**
	 * 更新svn
	 *
	 * @return int(-1更新失败,1成功,0有程序在占用更新)
	 */
	public boolean doUpdate() {
		if (!SVNKit.DoUpdateStatus) {
			logger.info("更新程序已经在运行中,不能重复请求!");
			return false;
		}
		SVNKit.DoUpdateStatus = false;
		/*
		 * For using over http:// and https://
		 */
		try {
			DAVRepositoryFactory.setup();

			ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
			// 实例化客户端管理类
			ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, SvnUserName, SvnPassWord);
			// 要更新的文件
			File updateFile = new File(TargetPath);
			// 获得updateClient的实例
			SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
			updateClient.setIgnoreExternals(false);
			// 执行更新操作
			logger.info("执行更新svn仓库操作");
			updateClient.doUpdate(updateFile, SVNRevision.HEAD, SVNDepth.INFINITY, false, false);
			DoUpdateStatus = true;
			return true;
		} catch (SVNException e) {
			DoUpdateStatus = true;
			logger.error(e.getMessage(),e);
			return false;
		}
	}

	/**
	 * Svn提交 list.add("a.txt")也可直接添加单个文件; list.add("aaa")添加文件夹将添加夹子内所有的文件到svn,预添加文件必须先添加其所在的文件夹;
	 *
	 * @param
	 * @return Boolean
	 */
	public Boolean doCommit(List<String> fileRelativePathList) {
		// 注意:执行此操作要先执行checkout操作。因为本地需要有工作副本此范例才能运行。
		// 初始化支持svn://协议的库
		SVNRepositoryFactoryImpl.setup();

		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		// 实例化客户端管理类
		ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, SvnUserName, SvnPassWord);
		// 要提交的文件夹子
		File commitFile = new File(TargetPath);
		// 获取此文件的状态(是文件做了修改还是新添加的文件?)
		SVNStatus status;
		File addFile;
		String strPath;
		try {
			if (fileRelativePathList != null && fileRelativePathList.size() > 0) {
				for (int i = 0; i < fileRelativePathList.size(); i++) {
					strPath = fileRelativePathList.get(i);
					addFile = new File(TargetPath + "/" + strPath);
					status = ourClientManager.getStatusClient().doStatus(addFile, true);
					// 如果此文件是新增加的则先把此文件添加到版本库,然后提交。
					if (null == status || status.getContentsStatus() == SVNStatusType.STATUS_UNVERSIONED) {
						// 把此文件增加到版本库中
						ourClientManager.getWCClient().doAdd(addFile, false, false, false, SVNDepth.INFINITY, false, false);
						logger.info("add");
					}
				}
				// 提交此文件
				ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
				logger.info("commit");
			}
			// 如果此文件不是新增加的,直接提交。
			else {
				ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
				logger.info("commit");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return false;
		}
		// logger.info(status.getContentsStatus());
		return true;
	}

	/**
	 * Svn提交
	 *
	 * @param
	 * @return Boolean
	 */
	public Boolean doCommit(String fileRelativePath) {
		// 注意:执行此操作要先执行checkout操作。因为本地需要有工作副本此范例才能运行。
		// 初始化支持svn://协议的库
		SVNRepositoryFactoryImpl.setup();

		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		// 实例化客户端管理类
		ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, SvnUserName, SvnPassWord);
		// 要提交的文件夹子
		File commitFile = new File(TargetPath);
		// 获取此文件的状态(是文件做了修改还是新添加的文件?)
		SVNStatus status;
		File addFile;
		try {
			if (fileRelativePath != null && fileRelativePath.trim().length() > 0) {
				addFile = new File(TargetPath + "/" + fileRelativePath);
				status = ourClientManager.getStatusClient().doStatus(addFile, true);
				// 如果此文件是新增加的则先把此文件添加到版本库,然后提交。
				if (null == status || status.getContentsStatus() == SVNStatusType.STATUS_UNVERSIONED) {
					// 把此文件增加到版本库中
					ourClientManager.getWCClient().doAdd(addFile, false, false, false, SVNDepth.INFINITY, false, false);
					logger.info("add");
				}
				// 提交此文件
				ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
				logger.info("commit");
			}
			// 如果此文件不是新增加的,直接提交。
			else {
				ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
				logger.info("commit");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return false;
		}
		// logger.info(status.getContentsStatus());
		return true;
	}

	/**
	 * 将文件导入并提交到svn 同路径文件要是已经存在将会报错
	 *
	 * @param
	 * @return Boolean
	 */
	public Boolean doImport(String dirPath) {
		/*
		 * For using over http:// and https://
		 */
		DAVRepositoryFactory.setup();
		// 相关变量赋值
		SVNURL repositoryURL = null;
		try {
			repositoryURL = SVNURL.parseURIEncoded(SvnPath);
		} catch (SVNException e) {
			logger.error(e.getMessage(),e);
		}

		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		// 实例化客户端管理类
		ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, SvnUserName, SvnPassWord);
		// 要把此目录中的内容导入到版本库
		File impDir = new File(dirPath);
		// 执行导入操作
		SVNCommitInfo commitInfo = null;
		try {
			commitInfo = ourClientManager.getCommitClient().doImport(impDir, repositoryURL, "import operation!", null, false, false, SVNDepth.INFINITY);
		} catch (SVNException e) {
			logger.error(e.getMessage(),e);
			return false;
		}
		logger.info(commitInfo.toString());
		return true;
	}

}

操作git仓库

public class GitUtil {
    private static Logger logger = LoggerFactory.getLogger(GitUtil.class);

    /**
     * 拉取远程仓库内容到本地,需要账号和密码的
     */

    public static boolean gitPull(String localPath, String username, String password, String branch){
        UsernamePasswordCredentialsProvider provider = new UsernamePasswordCredentialsProvider(username, password);
        //git仓库地址
        Repository repository;
        try {
            repository = new FileRepository(localPath + "/.git");
            Git git = new Git(repository);
            git.pull().setRemoteBranchName(branch).setCredentialsProvider(provider).call();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean cloneRepository(String localPath, String username, String password, String uri,String branch) {

        //设置远程服务器上的用户名和密码
        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider = new
                UsernamePasswordCredentialsProvider(username, password);

        //克隆代码库命令
        CloneCommand cloneCommand = Git.cloneRepository();

        try {
            cloneCommand.setURI(uri) //设置远程URI
                    .setBranch(branch) //设置clone下来的分支
                    .setDirectory(new File(localPath)) //设置下载存放路径
                    .setCredentialsProvider(usernamePasswordCredentialsProvider) //设置权限验证
                    .call();
            return true;
        } catch (GitAPIException e) {
            logger.error("克隆远程仓库内容到本地异常:", e);
            return false;
        }
    }
}