SVNKIT操作SVN版本库的完整例子
http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments
2.SVNClientManager包括了各种Client来完成日常的SVN操作,如下图:
类 | 命令 | 备注 |
SVNLogClient.doLog() |
svn log [PATH|URL] |
从库中显示log消息 |
SVNLogClient.doList() | svn list [TARGET[@REV]...] | 显示目标下的文件和目录列表 |
SVNUpdateClient.doCheckout() | svn checkout URL[@REV]... [PATH] | 检出 |
SVNUpdateClient.doUpdate() | svn update [PATH...] | 更新workcopy |
SVNUpdateClient.doExport() | svn export URL|PATH PATH1 | 导出一个干净的目录树,不包含所有的受控信息。可以选择从URL或WC中导出 |
SVNWCClient.doAdd() | svn add [path] | 添加文件至workcopy |
SVNWCClient.doGetFileContents() | svn cat TARGET[@REV] | 输出指定目标的内容 |
SVNWCClient.doDelete() | svn delete PATH... | 从版本库删除 |
SVNWCClient.doInfo() | svn info [TARGET[@REV]...] | 显示指定WC和URL信息 |
SVNWCClient.doLock() | svn lock TARGET... | 加锁 |
SVNWCClient.doUnlock() | svn unlock | 释放锁 |
SVNCommitClient.doCommit() | svn commit [PATH...] | 提交 |
SVNCommitClient.doDelete() | svn delete URL | 删除服务器文件 |
SVNCommitClient.doMkDir() | svn mkdir PATH|URL | 在WC或库路径创建目录 |
SVNLogClinet.doAnnotate() | svn blame Target[@REV] | 显示某个已受控文件的每一行的最后修改版本和作者 |
SVNWCClient.doCleanup() | svn cleanup [PATH...] | 递归的清理WC中过期的锁和未完成的操作 |
SVNCopyClient.doCopy() | svn copy SRC[@REV]... DST | copy操作可以从WC到WC |
SVNDiffClient.doDiff() | svn diff | 用来比较并显示修改点 |
SVNCommitClient.doImport() | svn import [PATH] URL | 导入本地一个目录到库中 |
SVNDiffClient.doMerge() |
svn merge |
合并两个受控源的不同之处,存放到一个WC里 |
SVNMoveClinet.doMove() | svn move SRC... DST | 移动 |
SVNWCClient.resovle() | svn resolve PATH | 将冲突文件标记为已解决,删掉冲突产生的临时文件 |
SVNWCClient.revert() | svn revert PATH | 还原WC中所有的本地更改 |
SVNStatusClient.doStatus() | svn status [PATH...] | 输出WC中文件和目录的状态 |
http://blog.sina.com.cn/s/blog_74d4b8a501019cx5.html
报错:
org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for '<http://ip:80>' at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:62) at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:51) at org.tmatesoft.svn.core.internal.io.dav.DAVUtil.findStartingProperties(DAVUtil.java:136) at org.tmatesoft.svn.core.internal.io.dav.DAVUtil.getBaselineProperties(DAVUtil.java:226) at org.tmatesoft.svn.core.internal.io.dav.DAVUtil.getBaselineInfo(DAVUtil.java:184) at org.tmatesoft.svn.core.internal.io.dav.DAVRepository.getLatestRevision(DAVRepository.java:179) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgRepositoryAccess.getRevisionNumber(SvnNgRepositoryAccess.java:118) at org.tmatesoft.svn.core.internal.wc2.SvnRepositoryAccess.getLocations(SvnRepositoryAccess.java:184) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgRepositoryAccess.createRepositoryFor(SvnNgRepositoryAccess.java:45) at org.tmatesoft.svn.core.internal.wc2.remote.SvnRemoteExport.run(SvnRemoteExport.java:55) at org.tmatesoft.svn.core.internal.wc2.remote.SvnRemoteExport.run(SvnRemoteExport.java:40) at org.tmatesoft.svn.core.internal.wc2.SvnOperationRunner.run(SvnOperationRunner.java:21) at org.tmatesoft.svn.core.wc2.SvnOperationFactory.run(SvnOperationFactory.java:1235) at org.tmatesoft.svn.core.wc2.SvnOperation.run(SvnOperation.java:294) at org.tmatesoft.svn.core.wc.SVNUpdateClient.doExport(SVNUpdateClient.java:897) at tools.SVNUtil.exportFromSVN(SVNUtil.java:83) at tools.SVNUtilTest.testExportFromSVN(SVNUtilTest.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for '<http://ip:80>' at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.authenticationFailed(SVNErrorManager.java:47) at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.authenticationFailed(SVNErrorManager.java:41) at org.tmatesoft.svn.core.internal.wc.DefaultSVNAuthenticationManager.getNextAuthentication(DefaultSVNAuthenticationManager.java:218) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:696) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:375) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:363) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.performHttpRequest(DAVConnection.java:710) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.doPropfind(DAVConnection.java:131) at org.tmatesoft.svn.core.internal.io.dav.DAVUtil.getProperties(DAVUtil.java:73) at org.tmatesoft.svn.core.internal.io.dav.DAVUtil.getResourceProperties(DAVUtil.java:79) at org.tmatesoft.svn.core.internal.io.dav.DAVUtil.getStartingProperties(DAVUtil.java:103) at org.tmatesoft.svn.core.internal.io.dav.DAVUtil.findStartingProperties(DAVUtil.java:125) ... 37 more
解决办法:
使用自定义的ISVNAuthenticationProvider对象。
scene1:
setupLibrary(); SVNURL url = SVNURL.parseURIEncoded(this.svnUrl); SVNRepository repository = SVNRepositoryFactory.create(url); ISVNAuthenticationManager authManager = SVNWCUtil .createDefaultAuthenticationManager(userName, password); authManager.setAuthenticationProvider(new CustomSVNAuthenticationProvider(userName,password)); repository.setAuthenticationManager(authManager);
scene2:
setupLibrary(); SVNURL url = SVNURL.parseURIEncoded(svnUrl); DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true); ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, password); authManager.setAuthenticationProvider(new CustomSVNAuthenticationProvider(userName,password)); SVNClientManager clientManager = SVNClientManager.newInstance(options,authManager); SVNUpdateClient updateClient = clientManager.getUpdateClient(); updateClient.setIgnoreExternals(false);
class CustomSVNAuthenticationProvider implements ISVNAuthenticationProvider{ private String userName; private String password; public CustomSVNAuthenticationProvider(String userName,String password) { this.userName=userName; this.password=password; } @Override public SVNAuthentication requestClientAuthentication(String kind, SVNURL url, String realm, SVNErrorMessage errorMessage, SVNAuthentication previousAuth, boolean authMayBeStored) { return new SVNPasswordAuthentication(userName,password, true, url, false); } @Override public int acceptServerAuthentication(SVNURL url, String realm, Object certificate, boolean resultMayBeStored) { return 0; } }
package org.tmatesoft.svn.core; /** * This class contains enumeration that describes depth, * that is used. * The order of these depths is important: the higher the number, * the deeper it descends. You can use it to compare two depths * numerically to decide which goes deeper. * * @version 1.3 * @author TMate Software Ltd. * @since 1.2 */ public class SVNDepth implements Comparable { /** * Depth undetermined or ignored. */ public static final SVNDepth UNKNOWN = new SVNDepth(-2, "unknown"); /** * Exclude (don't descend into) directory D. */ public static final SVNDepth EXCLUDE = new SVNDepth(-1, "exclude"); /** * Just the named directory D, no entries. For instance, update will not pull in * any files or subdirectories. */ public static final SVNDepth EMPTY = new SVNDepth(0, "empty"); /** * D and its file children, but not subdirectories. For instance, updates will pull in any * files, but not subdirectories. */ public static final SVNDepth FILES = new SVNDepth(1, "files"); /** * D and its immediate children (D and its entries). Updates will pull in * any files or subdirectories without any children. */ public static final SVNDepth IMMEDIATES = new SVNDepth(2, "immediates"); /** * D and all descendants (full recursion from D). For instance, updates will pull in any files or subdirectories recursively. */ public static final SVNDepth INFINITY = new SVNDepth(3, "infinity");
demo:
package svn; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tmatesoft.svn.core.SVNDepth; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNNodeKind; import org.tmatesoft.svn.core.SVNProperties; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; 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.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; import org.tmatesoft.svn.core.wc.SVNClientManager; import org.tmatesoft.svn.core.wc.SVNRevision; import org.tmatesoft.svn.core.wc.SVNUpdateClient; import org.tmatesoft.svn.core.wc.SVNWCUtil; public class SVNUtil { private static final Logger LOGGER=LoggerFactory.getLogger(SVNUtil.class); private String svnUrl; private String filePath; private String userName; private String password; private File localFile; private final int latest_revision=-1; public SVNUtil(String svnUrl,String userName,String password,File file) { this(userName,password,file); this.svnUrl=svnUrl; } public SVNUtil(String userName,String password,File localFile) { this.userName=userName; this.password=password; this.localFile=localFile; } public SVNUtil(String url, String filePath, String userName, String password, File localFile) { this(url, userName, password,localFile); this.filePath=filePath; } public void checkOutFromSVN() throws SVNException { System.setProperty("javax.net.debug", "all"); System.setProperty("svnkit.http.sslProtocols", "SSLv3"); setupLibrary(); SVNURL url = SVNURL.parseURIEncoded(this.svnUrl); DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true); SVNClientManager clientManager = SVNClientManager.newInstance(options, userName, password); SVNUpdateClient updateClient = clientManager.getUpdateClient(); updateClient.setIgnoreExternals(false); long workVersion = updateClient.doCheckout(url, this.localFile, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.FILES, false); LOGGER.info("Current version:{}", workVersion); } public void updateFromSVN() throws SVNException { System.setProperty("svnkit.http.sslProtocols", "SSLv3"); setupLibrary(); DefaultSVNOptions options=SVNWCUtil.createDefaultOptions(true); SVNClientManager clientManager=SVNClientManager.newInstance(options, userName, password); SVNUpdateClient updateClient=clientManager.getUpdateClient(); updateClient.setIgnoreExternals(false); long workVersion=updateClient.doUpdate(localFile, SVNRevision.HEAD,SVNDepth.INFINITY,false,false); LOGGER.info("Current version:{}",workVersion); } public void getFileFromSVN() throws SVNException, FileNotFoundException { System.setProperty("svnkit.http.sslProtocols", "SSLv3"); setupLibrary(); SVNRepository repository=SVNRepositoryFactory.create(SVNURL.parseURIEncoded(this.svnUrl)); ISVNAuthenticationManager authManager=SVNWCUtil.createDefaultAuthenticationManager(userName, password); repository.setAuthenticationManager(authManager); SVNNodeKind nodeKind=repository.checkPath(this.filePath,latest_revision); if (nodeKind==SVNNodeKind.FILE) { SVNProperties properties=new SVNProperties(); OutputStream contents=new FileOutputStream(this.localFile); long workVersion=repository.getFile(this.filePath,latest_revision, properties, contents); LOGGER.info("Current version:{}",workVersion); }else { LOGGER.error("Must be a file!current:{},url:{}",nodeKind,this.svnUrl); } } /* * Initializes the library to work with a repository via * different protocols. */ private void setupLibrary() { /* * For using over http:// and https:// */ DAVRepositoryFactory.setup(); /* * For using over svn:// and svn+xxx:// */ SVNRepositoryFactoryImpl.setup(); /* * For using over file:/// */ FSRepositoryFactory.setup(); } }
Supported Protocols | Factory to setup |
svn://, svn+xxx:// | SVNRepositoryFactoryImpl (org.tmatesoft.svn.core.internal.io.svn) |
http://, https:// | DAVRepositoryFactory (org.tmatesoft.svn.core.internal.io.dav) |
file:/// (FSFS only) | FSRepositoryFactory (org.tmatesoft.svn.core.internal.io.fs) |
Also SVNRepositoryFactory may be used to create local FSFS-type repositories.
http://www.svnkit.com/kb/javadoc/org/tmatesoft/svn/core/io/SVNRepositoryFactory.html
package svnkit; import java.io.File; 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.wc.DefaultSVNOptions; import org.tmatesoft.svn.core.wc.ISVNOptions; import org.tmatesoft.svn.core.wc.SVNClientManager; import org.tmatesoft.svn.core.wc.SVNRevision; import org.tmatesoft.svn.core.wc.SVNUpdateClient; import org.tmatesoft.svn.core.wc.SVNWCUtil; public class CheckOut { // 声明SVN客户端管理类 private static SVNClientManager ourClientManager; public static void main(String[] args) throws Exception { // 初始化支持svn://协议的库。 必须先执行此操作。 DAVRepositoryFactory.setup();// 对于使用http://和https:// // SVNRepositoryFactoryImpl.setup();//对于使用svn:/ /和svn+xxx:/ / // FSRepositoryFactory.setup();//对于使用file:// // 相关变量赋值 SVNURL repositoryURL = null; try { String url = "svn://localhost/project"; url = "https://127.0.0.1/svn/test/branch_fortest"; repositoryURL = SVNURL.parseURIEncoded(url); } catch (SVNException e) { // System.out.println("无法连接"); } String name = "admin"; String password = "admin"; String workPath = "svnkit"; ISVNOptions options = SVNWCUtil.createDefaultOptions(true); // 实例化客户端管理类 ourClientManager = SVNClientManager.newInstance( (DefaultSVNOptions) options, name, password); // 要把版本库的内容check out到的目录 // FIle wcDir = new File("d:/test") File wcDir = new File(workPath); // 通过客户端管理类获得updateClient类的实例。 SVNUpdateClient updateClient = ourClientManager.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.FILES, false); System.out.println("把版本:" + workingVersion + " check out 到目录:" + wcDir+ "中。"); } }
http://blog.csdn.net/luochen_50/article/details/7556629
报错:
org.tmatesoft.svn.core.SVNException: svn: E175002: Connection has been shutdown: javax.net.ssl.SSLException: Received fatal alert: bad_record_mac svn: E175002: OPTIONS request failed on '/svn/test/test' at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:106) at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:90) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:775) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:375) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:363) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.performHttpRequest(DAVConnection.java:710) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.exchangeCapabilities(DAVConnection.java:627) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.open(DAVConnection.java:102) at org.tmatesoft.svn.core.internal.io.dav.DAVRepository.openConnection(DAVRepository.java:1032) at org.tmatesoft.svn.core.internal.io.dav.DAVRepository.getLatestRevision(DAVRepository.java:175) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgRepositoryAccess.getRevisionNumber(SvnNgRepositoryAccess.java:118) at org.tmatesoft.svn.core.internal.wc2.SvnRepositoryAccess.getLocations(SvnRepositoryAccess.java:184) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgRepositoryAccess.createRepositoryFor(SvnNgRepositoryAccess.java:45) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgAbstractUpdate.checkout(SvnNgAbstractUpdate.java:809) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgCheckout.run(SvnNgCheckout.java:26) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgCheckout.run(SvnNgCheckout.java:11) at org.tmatesoft.svn.core.internal.wc2.ng.SvnNgOperationRunner.run(SvnNgOperationRunner.java:20) at org.tmatesoft.svn.core.internal.wc2.SvnOperationRunner.run(SvnOperationRunner.java:21) at org.tmatesoft.svn.core.wc2.SvnOperationFactory.run(SvnOperationFactory.java:1235) at org.tmatesoft.svn.core.wc2.SvnOperation.run(SvnOperation.java:294) at org.tmatesoft.svn.core.wc.SVNUpdateClient.doCheckout(SVNUpdateClient.java:777) at com.zte.ums.switchplat.tools.SVNUtil.process(SVNUtil.java:38) at com.zte.ums.switchplat.tools.SVNUtilTest.testProcess(SVNUtilTest.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: Received fatal alert: bad_record_mac at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkWrite(Unknown Source) at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source) at java.io.BufferedOutputStream.flushBuffer(Unknown Source) at java.io.BufferedOutputStream.flush(Unknown Source) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.sendData(HTTPConnection.java:325) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPRequest.dispatch(HTTPRequest.java:170) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:474) ... 43 more Caused by: javax.net.ssl.SSLException: Received fatal alert: bad_record_mac at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source) at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.getSession(Unknown Source) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:467) ... 43 more
解决办法:
I would like to add that -Dsvnkit.http.sslProtocols="SSLv3" worked for me.
However I don't understand why it works fine without using -Dsvnkit.http.sslProtocols="SSLv3" in 1.3.5 but I doesn't in 1.3.6/7. This suggests that this is rather SVNKIT issue than Java 5.
-Dsvnkit.http.sslProtocols="SSLv3" ... How and where to write this command? Can anybody help.
It is a system property. Usually one sets it when starts JVM (java),
i.e in the command script:
java -Dsvnkit.http.sslProtocols=SSLv3 -classpath a.jar:svnkit.jar:... mainClassName
Or one may set it programatically at runtime:
System.setProperty("svnkit.http.sslProtocols", "SSLv3");
http://issues.tmatesoft.com/issue/SVNKIT-176
http://stackoverflow.com/questions/8893871/eclipse-indigo-subversive-0-7-9-bad-record-mac
It is a hard to say what causing this. You need to find out by analyzing the logs, set the
System.setProperty("javax.net.debug", "all");
and check what is wrong.
A problem may be that server is not supporting TLS, which may be picked by implementation.
To make sure that you always use the plain SSLv3 by setting the property:
System.setProperty("https.protocols", "SSLv3");//This doesn't work when suing svnkit.need use System.setProperty("svnkit.http.sslProtocols", "SSLv3");
Another cause may be the poor network, causing that occasionally some packets are broken but still have the correct checksum. In that case you may only retry the request.
http://stackoverflow.com/questions/22318729/javax-net-ssl-sslexception-received-fatal-alert-bad-record-mac
It might help if you include more of the debug output for the ssl connection leading up to the error which you included.
I was seeing the same error and just solved it by including -Dhttps.protocols=SSLv3 -Dforce.http.jre.executor=true in my VM arguements ...
so if you're lucky, that might work for you too.
http://stackoverflow.com/questions/9399810/javax-net-ssl-sslexception-received-fatal-alert-bad-record-mac-with-java-and-h
httpclient with certificate:
http://stackoverflow.com/questions/995514/https-connection-android
“Received fatal alert: bad_record_mac” when deploying to sonatype:
As suggested here, I set this:
MAVEN_OPTS="-Dhttps.protocols=SSLv3 -Dforce.http.jre.executor=true"
http://stackoverflow.com/questions/12787657/received-fatal-alert-bad-record-mac-when-deploying-to-sonatype
added one properties line.
System.setProperty("https.protocols", "SSLv3,SSLv2Hello");
And now jsoup connection is able to connect properly using the correct HTTPS protocol. Note, don’t forget to include your SSL certificate as well when making connection.
System.setProperty("javax.net.ssl.trustStore", certPath);
http://kennykee.com/350/javax-net-ssl-sslexception-received-fatal-alert-bad_record_mac-simple-solution/
import java.io.File; import org.apache.log4j.Logger; import org.tmatesoft.svn.core.SVNCommitInfo; import org.tmatesoft.svn.core.SVNDepth; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNNodeKind; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; 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.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; import org.tmatesoft.svn.core.wc.SVNClientManager; import org.tmatesoft.svn.core.wc.SVNRevision; import org.tmatesoft.svn.core.wc.SVNStatus; import org.tmatesoft.svn.core.wc.SVNUpdateClient; import org.tmatesoft.svn.core.wc.SVNWCUtil; /** * SVNKit Utility * @author lena yang * */ public class SVNUtil { private static Logger logger = Logger.getLogger(SVNUtil.class); /** * 通过不同的协议初始化版本库 */ public static void setupLibrary() { DAVRepositoryFactory.setup();// 对于使用http://和https:// SVNRepositoryFactoryImpl.setup();//对于使用svn:/ /和svn+xxx:/ / FSRepositoryFactory.setup();//对于使用file:// } /** * 验证登录svn */ public static SVNClientManager authSvn(String svnRoot, String username, String password) { // 初始化版本库 setupLibrary(); // 创建库连接 SVNRepository repository = null; try { repository = SVNRepositoryFactory.create(SVNURL .parseURIEncoded(svnRoot)); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); return null; } // 身份验证 ISVNAuthenticationManager authManager = SVNWCUtil .createDefaultAuthenticationManager(username, password); // 创建身份验证管理器 repository.setAuthenticationManager(authManager); DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true); SVNClientManager clientManager = SVNClientManager.newInstance(options, authManager); return clientManager; } /** * Make directory in svn repository * @param clientManager * @param url * eg: http://svn.ambow.com/wlpt/bsp/trunk * @param commitMessage * @return * @throws SVNException */ public static SVNCommitInfo makeDirectory(SVNClientManager clientManager, SVNURL url, String commitMessage) { try { return clientManager.getCommitClient().doMkDir( new SVNURL[] { url }, commitMessage); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } return null; } /** * Imports an unversioned directory into a repository location denoted by a * destination URL * @param clientManager * @param localPath * a local unversioned directory or singal file that will be imported into a * repository; * @param dstURL * a repository location where the local unversioned directory/file will be * imported into * @param commitMessage * @param isRecursive 递归 * @return */ public static SVNCommitInfo importDirectory(SVNClientManager clientManager, File localPath, SVNURL dstURL, String commitMessage, boolean isRecursive) { try { return clientManager.getCommitClient().doImport(localPath, dstURL, commitMessage, null, true, true, SVNDepth.fromRecurse(isRecursive)); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } return null; } /** * Puts directories and files under version control * @param clientManager * SVNClientManager * @param wcPath * work copy path */ public static void addEntry(SVNClientManager clientManager, File wcPath) { try { clientManager.getWCClient().doAdd(new File[] { wcPath }, true, false, false, SVNDepth.INFINITY, false, false, true); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } } /** * Collects status information on a single Working Copy item * @param clientManager * @param wcPath * local item's path * @param remote * true to check up the status of the item in the repository, * that will tell if the local item is out-of-date (like '-u' option in the SVN client's * 'svn status' command), otherwise false * @return * @throws SVNException */ public static SVNStatus showStatus(SVNClientManager clientManager, File wcPath, boolean remote) { SVNStatus status = null; try { status = clientManager.getStatusClient().doStatus(wcPath, remote); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } return status; } /** * Commit work copy's change to svn * @param clientManager * @param wcPath * working copy paths which changes are to be committed * @param keepLocks * whether to unlock or not files in the repository * @param commitMessage * commit log message * @return * @throws SVNException */ public static SVNCommitInfo commit(SVNClientManager clientManager, File wcPath, boolean keepLocks, String commitMessage) { try { return clientManager.getCommitClient().doCommit( new File[] { wcPath }, keepLocks, commitMessage, null, null, false, false, SVNDepth.INFINITY); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } return null; } /** * Updates a working copy (brings changes from the repository into the working copy). * @param clientManager * @param wcPath * working copy path * @param updateToRevision * revision to update to * @param depth * update的深度:目录、子目录、文件 * @return * @throws SVNException */ public static long update(SVNClientManager clientManager, File wcPath, SVNRevision updateToRevision, SVNDepth depth) { SVNUpdateClient updateClient = clientManager.getUpdateClient(); /* * sets externals not to be ignored during the update */ updateClient.setIgnoreExternals(false); /* * returns the number of the revision wcPath was updated to */ try { return updateClient.doUpdate(wcPath, updateToRevision,depth, false, false); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } return 0; } /** * recursively checks out a working copy from url into wcDir * @param clientManager * @param url * a repository location from where a Working Copy will be checked out * @param revision * the desired revision of the Working Copy to be checked out * @param destPath * the local path where the Working Copy will be placed * @param depth * checkout的深度,目录、子目录、文件 * @return * @throws SVNException */ public static long checkout(SVNClientManager clientManager, SVNURL url, SVNRevision revision, File destPath, SVNDepth depth) { SVNUpdateClient updateClient = clientManager.getUpdateClient(); /* * sets externals not to be ignored during the checkout */ updateClient.setIgnoreExternals(false); /* * returns the number of the revision at which the working copy is */ try { return updateClient.doCheckout(url, destPath, revision, revision,depth, false); } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } return 0; } /** * 确定path是否是一个工作空间 * @param path * @return */ public static boolean isWorkingCopy(File path){ if(!path.exists()){ logger.warn("'" + path + "' not exist!"); return false; } try { if(null == SVNWCUtil.getWorkingCopyRoot(path, false)){ return false; } } catch (SVNException e) { logger.error(e.getErrorMessage(), e); } return true; } /** * 确定一个URL在SVN上是否存在 * @param url * @return */ public static boolean isURLExist(SVNURL url,String username,String password){ try { SVNRepository svnRepository = SVNRepositoryFactory.create(url); ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); svnRepository.setAuthenticationManager(authManager); SVNNodeKind nodeKind = svnRepository.checkPath("", -1); return nodeKind == SVNNodeKind.NONE ? false : true; } catch (SVNException e) { e.printStackTrace(); } return false; } }
/** * 创建项目框架,SVN提交、更新 * @author lena yang * */ @Service("svnProjectService") public class SvnProjectService { private Logger logger = Logger.getLogger(SvnProjectService.class); // 项目的存放位置 private String workspace = null; private ResourceBundle rb = ResourceBundle.getBundle("application"); // SVN的用户名、密码 private String username = null; private String password = null; private String templete = null; @Resource(name="xcodeService") private XcodeService xcodeService; private void init(){ String webapp = System.getProperty("webapp.root"); if(null!=webapp&&!webapp.endsWith("/") && !webapp.endsWith("\")){ webapp = webapp + "/"; } // 发布到web服务器以后,有可能WebContent没了 if(new File(webapp + "WebContent").exists()){ webapp = webapp + "WebContent"; } username = rb.getString("svn.username"); password = rb.getString("svn.password"); workspace = rb.getString("project.svn.path"); templete = webapp + "templete"; } public SvnProjectService(){ super(); init(); } /** * 创建项目框架 * @param project * Project * @return */ public boolean createProjectFrame(Project project,List<String> tableNames) { if(project == null){ return false; } File src = new File(templete); // 模板项目的位置 File ws = new File(workspace); // work copy if(!ws.exists()){ ws.mkdirs(); } File dest = new File(workspace + "/" + project.getName()); if(!dest.exists()){ dest.mkdirs(); } checkWorkCopy(project); // 确定工作空间 // 复制模板项目到工作空间 try { FileUtils.copyDirectory(src, dest); } catch (IOException e) { logger.error(e.getMessage(), e); return false; } //修改.project文件中的内容 editProjectFile(project); // 生成框架代码 xcodeService.createBaseFrameWithDatasource(project,tableNames); // 提交到SVN commitProjectToSvn(project); return true; } /** * 修改项目文件 * @param project * @throws DocumentException * @throws IOException */ @SuppressWarnings("unchecked") private void editProjectFile(Project project) { String pro = workspace + "/" + project.getName() + "/"; String settings = pro + ".settings/"; // 1. 修改.settings/org.eclipse.wst.common.component Document document = null; try { document = XmlReaderUtil.getDocument(new File(settings + "org.eclipse.wst.common.component")); } catch (DocumentException e) { e.printStackTrace(); } Element root = document.getRootElement(); root.element("wb-module").attribute("deploy-name") .setValue(project.getName()); if (root.element("wb-module").element("property").attribute("name") .getValue().equals("java-output-path")) { root.element("wb-module").element("property").attribute("value") .setValue("/" + project.getName() + "/build/classes"); } Iterator<Element> itr = (Iterator<Element>) XmlReaderUtil.getElements( document, "//wb-module//property").iterator(); while (itr.hasNext()) { Element element = itr.next(); if ("context-root".equals(element.attribute("name").getValue())) { element.attribute("value").setValue("/" + project.getName()); } } // 将修改后的值写入 XmlReaderUtil.writerXml(document, settings + "org.eclipse.wst.common.component"); // 2. 修改.project try { document = XmlReaderUtil.getDocument(new File(pro + ".project")); XmlReaderUtil.setElementText(document, "//projectDescription", "name", project.getName()); XmlReaderUtil.writerXml(document, pro + ".project"); } catch (DocumentException e) { e.printStackTrace(); } } /** * 从SVN更新项目到work copy * @param project * Project * @return */ public boolean updateProjectFromSvn(Project project) { if(null == project || null == rb.getString("svn.url")){ return false; } project.setSvnUrl(rb.getString("svn.url")); SVNClientManager clientManager = SVNUtil.authSvn(project.getSvnUrl(), username, password); if (null == clientManager) { logger.error("SVN login error! >>> url:" + project.getSvnUrl() + " username:" + username + " password:" + password); return false; } // 注册一个更新事件处理器 clientManager.getCommitClient().setEventHandler(new UpdateEventHandler()); SVNURL repositoryURL = null; try { // eg: http://svn.ambow.com/wlpt/bsp repositoryURL = SVNURL.parseURIEncoded(project.getSvnUrl()).appendPath("trunk/"+project.getName(), false); } catch (SVNException e) { logger.error(e.getMessage(),e); return false; } File ws = new File(new File(workspace), project.getName()); if(!SVNWCUtil.isVersionedDirectory(ws)){ SVNUtil.checkout(clientManager, repositoryURL, SVNRevision.HEAD, new File(workspace), SVNDepth.INFINITY); }else{ SVNUtil.update(clientManager, ws, SVNRevision.HEAD, SVNDepth.INFINITY); } return true; } /** * 提交项目到SVN * @param project * Project * @return */ public boolean commitProjectToSvn(Project project) { SVNClientManager clientManager = SVNUtil.authSvn(project.getSvnUrl(), username, password); clientManager.getCommitClient().setEventHandler(new CommitEventHandler()); File wc_project = new File( workspace + "/" + project.getName()); checkVersiondDirectory(clientManager,wc_project); SVNUtil.commit(clientManager, wc_project, false, "svnkit"); return true; } /** * 递归检查不在版本控制的文件,并add到svn * @param clientManager * @param wc */ private void checkVersiondDirectory(SVNClientManager clientManager,File wc){ if(!SVNWCUtil.isVersionedDirectory(wc)){ SVNUtil.addEntry(clientManager, wc); } if(wc.isDirectory()){ for(File sub:wc.listFiles()){ if(sub.isDirectory() && sub.getName().equals(".svn")){ continue; } checkVersiondDirectory(clientManager,sub); } } } private void checkWorkCopy(Project project){ project.setSvnUrl(rb.getString("svn.url")); SVNClientManager clientManager = SVNUtil.authSvn(project.getSvnUrl(), username, password); SVNURL repositoryURL = null; // trunk try { // eg: http://svn.ambow.com/wlpt/bsp repositoryURL = SVNURL.parseURIEncoded(project.getSvnUrl()) .appendPath("trunk", false); } catch (SVNException e) { logger.error(e.getMessage(),e); } File wc = new File(workspace); File wc_project = new File( workspace + "/" + project.getName()); SVNURL projectURL = null; // projectName try { projectURL = repositoryURL.appendPath(project.getName(), false); } catch (SVNException e) { logger.error(e.getMessage(),e); } if(!SVNUtil.isWorkingCopy(wc)){ if(!SVNUtil.isURLExist(projectURL,username,password)){ SVNUtil.checkout(clientManager, repositoryURL, SVNRevision.HEAD, wc, SVNDepth.EMPTY); }else{ SVNUtil.checkout(clientManager, projectURL, SVNRevision.HEAD, wc_project, SVNDepth.INFINITY); } }else{ SVNUtil.update(clientManager, wc, SVNRevision.HEAD, SVNDepth.INFINITY); } } }
文章主题是svnkit的api操作。所以涉及到其它类的,可以忽略。svnkit有两套api,high level和low level 。high level是操作工作拷贝即working copy的。
我这儿用的就是high level。
checkWorkCopy方法里面,先检查当前指定的路径是否是一个working copy。如果是,执行update操作;
如果不是的话,分两种情况:
1. svn上已经有以此project命名的项目,可能svn上有了,但working copy被删了什么的。就将该project checkout下来;
2. svn上没有该project。此时checkout一个空的东西。SVNDepth.EMPTY,只是让我们的路径成为一个working copy,路径下出现一个.svn目录,svn上面的文件不会被checkout下来。因为我们的svn上可能项目很多。如果把整个上级目录checkout下来会很慢也浪费硬盘空间。
用 svnkit 执行commit操作,概况来说,流程如下:
1. 保证本地path是一个working copy。 即checkWorkCopy方法
2. woking copy可能发生添加,更新,删除等变化操作
3. 检查working copy里面的文件是否under version control。即checkVersiondDirectory方法
4. 执行commit操作
http://blog.csdn.net/hardwin/article/details/7963318
http://wiki.svnkit.com/Getting_Started_With_SVNKit
http://svnkit.com/kb/javadoc/org/tmatesoft/svn/core/wc/SVNClientManager.html
http://svnkit.com/kb/javadoc/org/tmatesoft/svn/core/wc/ISVNOptions.html
http://svnkit.com/kb/javadoc/org/tmatesoft/svn/core/wc/SVNRevision.html#HEAD
http://svnkit.com/kb/javadoc/overview-summary.html
http://blog.csdn.net/feiren127/article/details/7551782
问题:
在Eclipse下运行程序无异常。
在bat中使用java -jar xx.jar时抛异常,异常信息:
org.tmatesoft.svn.core.SVNException: svn: E175002: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) svn: E175002: OPTIONS request failed on '/svn/TDS-OMMB_Code/branch/OMMBV12.13.5X/common/3xconf/ums-svr/sdrmanager' at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:106) at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:90) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:775) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:375) at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:363) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.performHttpRequest(DAVConnection.java:710) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.exchangeCapabilities(DAVConnection.java:627) at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.open(DAVConnection.java:102) at org.tmatesoft.svn.core.internal.io.dav.DAVRepository.openConnection(DAVRepository.java:1032) at org.tmatesoft.svn.core.internal.io.dav.DAVRepository.checkPath(DAVRepository.java:214)
解决思路及办法:
(1)排查eclipse和使用bat运行程序在机制上有什么不同之处。//??这个问题没搞清楚,大致是Eclipse使用launcher,bat是使用runtime.exec()
(2)排查jdk版本。发现eclipse中使用的jdk是1.6;os下jdk是1.8
更改jdk1.8为1.6后,程序在两种方式下运行均无异常
更改jdk版本的操作:http://www.cnblogs.com/softidea/p/4312434.html