当前位置:   article > 正文

java操作svn工具类_java 操作svn

java 操作svn

依赖包

<dependency>
  <groupId>org.tmatesoft.svnkit</groupId>
  <artifactId>svnkit</artifactId>
  <version>1.10.1</version>
</dependency>

svn路径等配置类

  1. import lombok.Getter;
  2. import lombok.Setter;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. @Getter
  5. @Setter
  6. @ConfigurationProperties("svn.resource")
  7. public class SvnConnectProperties {
  8. private String url;
  9. private String remote;
  10. private String name;
  11. private String password;
  12. }

操作svn工具类

  1. import com.xxx.Exception.HZException;
  2. import com.xxx.enums.HttpStatus;
  3. import com.xxx.ExceptionUtils;
  4. import com.xxx.config.SvnConnectProperties;
  5. import lombok.AllArgsConstructor;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.tmatesoft.svn.core.*;
  11. import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
  12. import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
  13. import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
  14. import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
  15. import org.tmatesoft.svn.core.io.SVNRepository;
  16. import org.tmatesoft.svn.core.wc.ISVNOptions;
  17. import org.tmatesoft.svn.core.wc.SVNClientManager;
  18. import org.tmatesoft.svn.core.wc.SVNUpdateClient;
  19. import org.tmatesoft.svn.core.wc.SVNWCUtil;
  20. import java.io.*;
  21. import java.util.Collection;
  22. @Configuration
  23. @AllArgsConstructor
  24. @EnableConfigurationProperties(SvnConnectProperties.class)
  25. public class SvnHandler {
  26. private final SvnConnectProperties connectProperties;
  27. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  28. public SVNRepository getRepository(String url, String name, String password) {
  29. SVNRepository repository = null;
  30. try {
  31. repository = DAVRepositoryFactory.create(SVNURL.parseURIEncoded(url));
  32. ISVNAuthenticationManager authManager = new BasicAuthenticationManager(name, password);
  33. repository.setAuthenticationManager(authManager);
  34. } catch (SVNException e) {
  35. ExceptionUtils.handlerException(e, logger);
  36. }
  37. return repository;
  38. }
  39. public SVNRepository getRepository() {
  40. return getRepository(connectProperties);
  41. }
  42. public SVNRepository getRepository(SvnConnectProperties config) {
  43. SVNRepository repository = getRepository(config.getUrl(), config.getName(), config.getPassword());
  44. SVNNodeKind kind = checkPath(repository, "");
  45. return (SVNNodeKind.UNKNOWN == kind || SVNNodeKind.NONE == kind) ?
  46. getRepository(config.getRemote(), config.getName(), config.getPassword()) : repository;
  47. }
  48. public String getActiveUrl() {
  49. SVNRepository repository = getRepository(connectProperties.getUrl(), connectProperties.getName(), connectProperties.getPassword());
  50. SVNNodeKind kind = checkPath(repository, "");
  51. return (SVNNodeKind.UNKNOWN == kind || SVNNodeKind.NONE == kind) ? connectProperties.getRemote() : connectProperties.getUrl();
  52. }
  53. public SVNUpdateClient getSvnUpdataClient() {
  54. setupDavLibrary();
  55. ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
  56. SVNClientManager manager = SVNClientManager.newInstance((DefaultSVNOptions) options, connectProperties.getName(), connectProperties.getPassword());
  57. SVNUpdateClient client = manager.getUpdateClient();
  58. client.setIgnoreExternals(false);
  59. return client;
  60. }
  61. public Collection<SVNDirEntry> listEntries(String path) {
  62. setupDavLibrary();
  63. return listEntries(getRepository(), path);
  64. }
  65. public SVNNodeKind checkPath(SVNRepository repository, String path) {
  66. SVNNodeKind result = SVNNodeKind.UNKNOWN;
  67. try {
  68. result = repository.checkPath(path, -1);
  69. } catch (SVNException ignored) {
  70. }
  71. return result;
  72. }
  73. public SVNNodeKind checkPath(String path) {
  74. setupDavLibrary();
  75. return checkPath(getRepository(), path);
  76. }
  77. private Collection<SVNDirEntry> listEntries(SVNRepository repository, String path) {
  78. Collection<SVNDirEntry> result = null;
  79. try {
  80. result = repository.getDir(path, -1, null, (Collection) null);
  81. } catch (SVNException e) {
  82. ExceptionUtils.handlerException(e, logger);
  83. }
  84. return result;
  85. }
  86. public void setupDavLibrary() {
  87. DAVRepositoryFactory.setup();
  88. }
  89. public SvnConnectProperties getConnectProperties() {
  90. return connectProperties;
  91. }
  92. public void getOutputStream(SVNRepository repository, String path, SVNProperties properties, OutputStream outputStream) {
  93. try {
  94. SVNNodeKind nodeKind = repository.checkPath(path, -1);
  95. if (nodeKind == SVNNodeKind.NONE) {
  96. throw new HZException(HttpStatus.error.getCode(), path + "不存在");
  97. } else if (nodeKind != SVNNodeKind.FILE) {
  98. throw new HZException(HttpStatus.error.getCode(), path + "不是文件");
  99. }
  100. repository.getFile(path, -1, properties, outputStream);
  101. } catch (SVNException e) {
  102. ExceptionUtils.handlerException(e, logger);
  103. }
  104. }
  105. public boolean downloadFile(File file, String path) {
  106. SVNProperties properties = new SVNProperties();
  107. OutputStream outputStream = null;
  108. try {
  109. if (!file.exists()) {
  110. file.createNewFile();
  111. }
  112. outputStream = new FileOutputStream(file);
  113. setupDavLibrary();
  114. getOutputStream(getRepository(), path, properties, outputStream);
  115. } catch (IOException e) {
  116. ExceptionUtils.handlerException(e, logger);
  117. } finally {
  118. if (null != outputStream) {
  119. try {
  120. outputStream.flush();
  121. outputStream.close();
  122. } catch (IOException ignored) {
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/847064
推荐阅读
相关标签
  

闽ICP备14008679号