1.pom文件中引入jar包:
<dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.9.3</version> </dependency>
2.svn操作代碼
public class SVNKit { protected static Logger logger = LoggerFactory.getLogger(SVNKit.class); // 更新狀態 true:沒有程序在執行更新,反之則反 private static Boolean DoUpdateStatus = true; // 聲明SVN客戶端管理類 private static SVNClientManager ourClientManager; /** * 檢查任務文件是否存在 * @param userName svn用戶名 * @param password svn用戶密碼 * @param svnUrl 文件存放路徑 * @param path 相對路徑 * @return 結果 * @throws SVNException svn異常 */ public static boolean checkPath(String userName,String password,String svnUrl,String path) throws SVNException { DAVRepositoryFactory.setup(); SVNURL repositoryURL = null; boolean isExist = false; try { repositoryURL = SVNURL.parseURIEncoded(svnUrl); } catch (SVNException e) { logger.warn("svn地址格式錯誤",e); throw new SVNException(e.getErrorMessage()); } SVNRepository svnRepository = SVNRepositoryFactory.create(repositoryURL); ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil .createDefaultAuthenticationManager(userName, password); svnRepository.setAuthenticationManager(isvnAuthenticationManager); SVNNodeKind node = null; node = svnRepository.checkPath(path, -1); if(node == SVNNodeKind.DIR) { Collection entries = svnRepository.getDir(path, -1, null, (Collection)null); Iterator iterator = entries.iterator(); if(iterator.hasNext()) { SVNDirEntry entry = (SVNDirEntry)iterator.next(); if(entry.getKind() == SVNNodeKind.FILE) { logger.debug("svn中文件路徑:"+svnUrl+"/"+path + "下任務文件存在,文件名稱:" +entry.getName()); isExist = true; } } } logger.debug("檢查svn中文件路徑:"+svnUrl+"/"+path+"下任務文件是否存在,結果:"+isExist); return isExist; } /** * 增加svn目錄 * @param userName svn用戶名 * @param password svn用戶密碼 * @param svnUrl svn地址 * @param adddir 需要增加的目錄 * @return 執行結果 * @throws SVNException svn處理異常 */ public static Boolean addDir(String userName,String password,String svnUrl, String adddir) throws SVNException { DAVRepositoryFactory.setup(); SVNURL repositoryURL = null; try { repositoryURL = SVNURL.parseURIEncoded(svnUrl); } catch (SVNException e) { logger.warn("svn地址格式錯誤",e); throw new SVNException(e.getErrorMessage()); } SVNRepository svnRepository = SVNRepositoryFactory.create(repositoryURL); ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil .createDefaultAuthenticationManager(userName, password); svnRepository.setAuthenticationManager(isvnAuthenticationManager); ISVNEditor editor = null; try { editor = svnRepository.getCommitEditor("add dir", null, true, null); editor.openRoot(-1); // 新增目錄 editor.addDir(adddir, null, -1); editor.closeDir(); editor.closeDir(); editor.closeEdit(); logger.debug("在svn路徑:"+svnUrl+"下增加目錄:" + adddir +" 成功。"); } catch (SVNException e) { if(editor != null) { editor.abortEdit(); } logger.warn("在svn路徑:"+svnUrl+"下增加目錄:" + adddir+" 失敗。",e); throw new SVNException(e.getErrorMessage()); } return true; } /** * svn檢出 * @param userName svn用戶名 * @param password svn用戶密碼 * @param svnUrl svn地址 * @param filePath 檢出到文件路徑 * @return 檢出結果 * @throws SVNException svn處理異常 */ public static Boolean checkOut(String userName,String password,String svnUrl, String filePath) throws SVNException { // 初始化庫。 必須先執行此操作。具體操作封裝在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(svnUrl); } catch (SVNException e) { logger.warn("svn地址格式錯誤",e); throw new SVNException(e.getErrorMessage()); } ISVNOptions options = SVNWCUtil.createDefaultOptions(true); // 實例化客戶端管理類 ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, password); // 要把版本庫的內容check out到的目錄 File wcDir = new File(filePath); // 通過客戶端管理類獲得updateClient類的實例。 SVNUpdateClient updateClient = ourClientManager.getUpdateClient(); updateClient.setIgnoreExternals(false); // 執行check out 操作,返回工作副本的版本號。 long workingVersion = -1; workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false); logger.debug("把版本:" + workingVersion + " check out 到目錄:" + wcDir + "中。"); return true; } /** * 清理目錄下的svn信息 * @param userName svn用戶名 * @param password svn用戶密碼 * @param filePath 文件夾路徑 * @return 執行結果 */ public static Boolean doCleanup(String userName,String password,String filePath) { ISVNOptions options = SVNWCUtil.createDefaultOptions(true); // 實例化客戶端管理類 ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, password); // 要把版本庫的內容check out到的目錄 File wcDir = new File(filePath); if (wcDir.exists()) { try { ourClientManager.getWCClient().doCleanup(wcDir); } catch (SVNException e) { // e.printStackTrace(); return false; } } else { return false; } return true; } /** * 更新svn * @param userName svn用戶名 * @param password svn用戶密碼 * @param filePath 文件路徑 * @return int(-1更新失敗,1成功,0有程序在占用更新) */ public static int doUpdate(String userName,String password,String filePath) { if (!DoUpdateStatus) { logger.debug("更新程序已經在運行中,不能重復請求!"); return 0; } DoUpdateStatus = false; /* * For using over http:// and https:// */ try { DAVRepositoryFactory.setup(); ISVNOptions options = SVNWCUtil.createDefaultOptions(true); // 實例化客戶端管理類 ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, password); // 要更新的文件 File updateFile = new File(filePath); // 獲得updateClient的實例 SVNUpdateClient updateClient = ourClientManager.getUpdateClient(); updateClient.setIgnoreExternals(false); // 執行更新操作 long versionNum = updateClient.doUpdate(updateFile, SVNRevision.HEAD, SVNDepth.INFINITY, false, false); logger.debug("工作副本更新后的版本:" + versionNum); DoUpdateStatus = true; return 1; } catch (SVNException e) { DoUpdateStatus = true; // e.printStackTrace(); return -1; } } /** * 提價svn * @param userName svn用戶名 * @param password svn用戶密碼 * @param filePath 待提交文件目錄 * @return 提交結果 * @throws SVNException svn異常 */ public static Boolean doCommit(String userName,String password,String filePath) throws SVNException { // 注意:執行此操作要先執行checkout操作。因為本地需要有工作副本此范例才能運行。 // 初始化支持svn://協議的庫 和http:// and https:// SVNRepositoryFactoryImpl.setup(); DAVRepositoryFactory.setup(); ISVNOptions options = SVNWCUtil.createDefaultOptions(true); // 實例化客戶端管理類 ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, password); // 要提交的文件夾 File commitFile = new File(filePath); // 獲取此文件的狀態(是文件做了修改還是新添加的文件?) SVNStatus status = null; File addFile = null; if (filePath != null && filePath.trim().length() > 0) { addFile = new File(filePath); 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.debug("add,新增文件添加到版本庫。"); } // 提交此文件 ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY); logger.debug("commit,提交本地路徑: " + filePath +"文件成功。"); } // 如果此文件不是新增加的,直接提交。 else { ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY); logger.debug("commit,提交本地路徑: " + filePath +"文件成功。"); } return true; } /** * 將文件導入並提交到svn 同路徑文件要是已經存在將會報錯 * * @param userName svn用戶名 * @param password svn用戶密碼 * @param dirPath 文件所在文件夾的路徑 * @return Boolean 返回結果 * @throws SVNException svn異常 */ public static Boolean doImport(String userName,String password,String dirPath) throws SVNException { /* * For using over http:// and https:// */ DAVRepositoryFactory.setup(); // 相關變量賦值 SVNURL repositoryURL = null; try { repositoryURL = SVNURL.parseURIEncoded(dirPath); } catch (SVNException e) { logger.warn("svn地址格式錯誤",e); throw new SVNException(e.getErrorMessage()); } ISVNOptions options = SVNWCUtil.createDefaultOptions(true); // 實例化客戶端管理類 ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, password); // 要把此目錄中的內容導入到版本庫 File impDir = new File(dirPath); // 執行導入操作 SVNCommitInfo commitInfo = ourClientManager.getCommitClient().doImport(impDir, repositoryURL, "import operation!", null, false, false, SVNDepth.INFINITY); logger.debug(commitInfo.toString()); return true; } }