通过Java代码,将文件提交到指定的SVN路径

Java 上传文件到Svn

1、在pom.xml文件中添加需要配置的svnkit版本号,然后通过maven进行更新下载配置文件

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

2,、配置svn路径和用户名密码
文件名:svnConfig.properties

svn.svnUrl =https://xxx/xxx
svn.username =admin
svn.password =admin

3、关于Svn提交、删除目录和文件的方法

package com.ascom.svn;

import org.springframework.util.StringUtils;
import org.tmatesoft.svn.core.*;
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;

import java.io.File;
import java.util.ResourceBundle;

    /**
     * @ClassName SvnUtils
     * @Description svn公共类
     * @Author wangyuxin
     * @Date 2020/6/8
     * @Version 1.0
     */
    public class SvnUpload {
   

        private SVNClientManager clientManager;
        private ISVNAuthenticationManager authManager;
        private SVNRepository repository;

        /** 读取配置文件 只能读取properties类型的文件, 读取的时候只需要文件名, 不需要后缀 */
        private static ResourceBundle rb = ResourceBundle.getBundle("svnConfig");
        /** svn地址  */
        private static String svnUrl = rb.getString("svn.svnUrl");
        private static String svnUsername = rb.getString("svn.username");
        private static String svnPassword = rb.getString("svn.password");

        /**
         * 启动svn连接
         * @throws SVNException 异常信息
         */
        public SvnUpload()throws SVNException {
   
            try {
   
                this.createDefaultAuthenticationManager(svnUsername, svnPassword);
                this.authSvn(svnUrl);
            } catch (SVNException e) {
   
                throw new RuntimeException(e.getMessage());
            }
        }

        /**
         * 通过不同的协议初始化版本库
         */
        private void setupLibrary() {
   
            DAVRepositoryFactory.setup();
            SVNRepositoryFactoryImpl.setup();
            FSRepositoryFactory.setup();
        }

        /**
         *
         * @param username svn用户名称
         * @param password svn用户密码
         * @throws SVNException 异常信息
         */
        private void createDefaultAuthenticationManager(String username, String password)throws SVNException{
   
            try {
   
                // 身份验证
                authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());
            } catch (Exception e) {
   
                throw new RuntimeException("SVN身份认证失败:" + e.getMessage());
            }
        }

        /**
         * 验证登录svn
         * @param svnUrl 用户svn的仓库地址
         * @throws SVNException 异常信息
         */
        private void authSvn(String svnUrl) throws SVNException {
   
            // 初始化版本库
            setupLibrary();
            try {
   
                repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl));
            } catch (SVNException e) {
   
                throw new RuntimeException("SVN创建库连接失败:" + e.getMessage());
            }

            // 创建身份验证管理器
            repository.setAuthenticationManager(authManager);
            DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
            try {
   
                //创建SVN实例管理器
                clientManager = SVNClientManager.
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要解析SVN路径下的.java文件并获取改动方法,您可以使用JavaSVN库和JavaParser库。 JavaSVN库是一个Java库,用于访问Subversion存储库。您可以使用这个库来检索文件历史记录和版本信息。 JavaParser库是一个Java库,用于解析和分析Java代码。您可以使用这个库来解析.java文件并获取文件中的方法。 以下是一个简单的示例代码,展示如何使用这两个库来实现这个任务: ```java import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.IOUtils; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.Edit; import org.eclipse.jgit.diff.EditList; import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.AbstractTreeIterator; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import com.github.javaparser.JavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.stmt.Statement; public class SVNJavaParser { public static void main(String[] args) throws Exception { String repoUrl = "svn://example.com/svn/myproject"; String username = "myusername"; String password = "mypassword"; String filePath = "/src/main/java/com/example/MyClass.java"; String commitId = "12345"; Repository repository = SVNUtils.getRepository(repoUrl, username, password); RevWalk revWalk = new RevWalk(repository); ObjectId objectId = repository.resolve(commitId); RevCommit commit = revWalk.parseCommit(objectId); AbstractTreeIterator oldTreeParser = SVNUtils.prepareTreeParser(repository, commit.getParent(0)); AbstractTreeIterator newTreeParser = SVNUtils.prepareTreeParser(repository, commit); List<DiffEntry> diffs = new Git(repository).diff() .setOldTree(oldTreeParser) .setNewTree(newTreeParser) .call(); for (DiffEntry entry : diffs) { if (entry.getNewPath().equals(filePath)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(outputStream); formatter.setRepository(repository); formatter.format(entry); String diffString = outputStream.toString(); RawText oldRawText = new RawText(diffString.split("@@")[0]); RawText newRawText = new RawText(diffString.split("@@")[2]); EditList editList = new EditList(); editList.addAll(new EditList(oldRawText, newRawText).getEdits()); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(commit.getTree()); treeWalk.setRecursive(true); while (treeWalk.next()) { if (filePath.equals(treeWalk.getPathString())) { CanonicalTreeParser oldTreeParser2 = new CanonicalTreeParser(); ObjectId oldTree = commit.getParent(0).getTree(); try (ObjectReader reader = repository.newObjectReader()) { oldTreeParser2.reset(reader, oldTree); } String oldContent = new String(repository.open(treeWalk.getObjectId(0)).getBytes()); CompilationUnit oldCompilationUnit = JavaParser.parse(oldContent); CompilationUnit newCompilationUnit = null; for (Edit edit : editList) { int beginLine = edit.getBeginA(); int endLine = edit.getEndA(); if (beginLine < 0 || endLine < 0) continue; List<String> oldLines = new ArrayList<>(); for (int i = beginLine; i < endLine; i++) { oldLines.add(oldRawText.getString(i)); } String oldMethodContent = String.join("\n", oldLines); MethodDeclaration oldMethodDeclaration = getMethodDeclaration(oldCompilationUnit, oldMethodContent); if (oldMethodDeclaration == null) continue; int newBeginLine = edit.getBeginB(); int newEndLine = edit.getEndB(); if (newBeginLine < 0 || newEndLine < 0) continue; List<String> newLines = new ArrayList<>(); for (int i = newBeginLine; i < newEndLine; i++) { newLines.add(newRawText.getString(i)); } String newMethodContent = String.join("\n", newLines); if (newCompilationUnit == null) { String newContent = new String(repository.open(treeWalk.getObjectId(0)).getBytes()); newCompilationUnit = JavaParser.parse(newContent); } MethodDeclaration newMethodDeclaration = getMethodDeclaration(newCompilationUnit, newMethodContent); if (newMethodDeclaration == null) continue; System.out.println("Old method: " + oldMethodDeclaration.getNameAsString()); System.out.println("New method: " + newMethodDeclaration.getNameAsString()); } } } } } } private static MethodDeclaration getMethodDeclaration(CompilationUnit compilationUnit, String methodContent) { List<MethodDeclaration> methods = compilationUnit.findAll(MethodDeclaration.class); for (MethodDeclaration method : methods) { String methodString = method.toString(); if (methodString.equals(methodContent)) { return method; } } return null; } } ``` 在这个示例代码中,我们首先使用JavaSVN库来检索指定提交的特定文件的差异。然后,我们使用JavaParser库来解析旧文件中的方法和新文件中的方法,并将它们进行比较,以获取改动方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值