jfinal+SVNKit 關聯svn伺服器進行檔案檢出、更新、上傳
阿新 • • 發佈:2018-12-20
感謝兩位博主
參考文章1:https://blog.csdn.net/dai_haijiao/article/details/80221660
參考文章2:https://blog.csdn.net/bfhx1314/article/details/17072517
1、maven專案使用依賴
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.9.2</version>
</dependency>
2、根據將所需的檔案進行配置放置在配置檔案下
#svn賬號密碼
SvnUserName = test
SvnPassWord = test
#專案在svn伺服器端的地址
SvnPath =
3、SVNRelation類
其中xx.getConfig(“SvnUserName”) 可直接使用字串代替,對應為賬號與密碼
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn. core.SVNURL;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft. svn.core.wc.*;
import java.io.File;
import java.util.List;
public class SVNRelation {
// 更新狀態 true:沒有程式在執行更新,反之則反
public static Boolean DoUpdateStatus = true;
// 宣告SVN客戶端管理類
private static SVNClientManager ourClientManager;
/**
* SVN檢出
* @param svnPath svn伺服器端地址
* @param targetPath 檢出到的本地地址(絕對路徑)
* @return
*/
public static Boolean checkOut(String svnPath,String targetPath) {
// 初始化庫。 必須先執行此操作。具體操作封裝在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) {
e.printStackTrace();
return false;
}
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
// 例項化客戶端管理類
ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, xx.getConfig("SvnUserName") ,xx.getConfig("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, true);
} else {
// ourClientManager.getWCClient().doCleanup(wcDir);
workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, true);
}
} catch (SVNException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
System.out.println("把版本:" + workingVersion + " check out 到目錄:" + wcDir + "中。");
return true;
}
/**
* 解除svn Luck
* @param targetPath 檢出到的本地地址
* @return
*/
public static Boolean doCleanup(String targetPath) {
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
// 例項化客戶端管理類
ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, xx.getConfig("SvnUserName") ,xx.getConfig("SvnPassWord"));
// 要把版本庫的內容check out到的目錄
File wcDir = new File(targetPath);
if (wcDir.exists()) {
try {
ourClientManager.getWCClient().doCleanup(wcDir);
} catch (SVNException e) {
e.printStackTrace();
return false;
}
} else {
return false;
}
return true;
}
/**
* 更新svn
*
* @return int(-1更新失敗,1成功,0有程式在佔用更新)
*/
/**
* 更新svn
*
* @param targetPath 專案對應的本地路徑
* @return
*/
public static int doUpdate(String targetPath) {
if (!SVNRelation.DoUpdateStatus) {
System.out.println("更新程式已經在執行中,不能重複請求!");
return 0;
}
SVNRelation.DoUpdateStatus = false;
/*
* For using over http:// and https://
*/
try {
DAVRepositoryFactory.setup();
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
// 例項化客戶端管理類
ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, xx.getConfig("SvnUserName") ,xx.getConfig("SvnPassWord"));
// 要更新的檔案
File updateFile = new File(targetPath);
// 獲得updateClient的例項
SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
// 執行更新操作
long versionNum = updateClient.doUpdate(updateFile, SVNRevision.HEAD, SVNDepth.INFINITY, false, false);
System.out.println("工作副本更新後的版本:" + versionNum);
DoUpdateStatus = true;
return 1;
} catch (SVNException e) {
DoUpdateStatus = true;
e.printStackTrace();
return -1;
}
}
/**
* Svn提交
*
* *說明:副文字路徑+相對路徑 = 檔案在伺服器的完整路徑。
* @param fileRelativePath 檔案相對路徑(待提交的檔案)
* @param targetPath 專案對應的svn副本路徑(本地路徑)
* @return
*/
public static Boolean doCommit(String fileRelativePath,String targetPath) {
// 注意:執行此操作要先執行checkout操作。因為本地需要有工作副本此範例才能執行。
// 初始化支援svn://協議的庫
SVNRepositoryFactoryImpl.setup();
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
// 例項化客戶端管理類
ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, xx.getConfig("SvnUserName") ,xx.getConfig("SvnPassWord"));
// 要提交的資料夾子
File commitFile = new File(targetPath);
// 獲取此檔案的狀態(是檔案做了修改還是新新增的檔案?)
SVNStatus status = null;
File addFile = null;
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);
System.out.println("add");
}
// 提交此檔案
ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
System.out.println("commit");
}
// 如果此檔案不是新增加的,直接提交。
else {
ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
System.out.println("commit");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
// System.out.println(status.getContentsStatus());
return true;
}
}