【Java程序与Jenkins交互的设计与实现】Java结合SVNKit API实现获取svn分支两个版本间的merge_log,并写到本地文件中,打成jar包实现与Jenkins的交互总结

11 篇文章 0 订阅
7 篇文章 1 订阅

【Java结合SVNKit API与Jenkins交互f的设计与实现】:

  • 背景描述:
    Java结合SVNKit API实现获取svn分支的两个版本间的merge_log,并写到本地文件中,打成jar包实现与Jenkins的交互方案的设计与实现总结。

一、实现思路总结:

  1. 目前使用Java结合SVNKit 1.10.8实现SVNKit API中的io.SVNRepository的log()方法、wc.SVNLogClient的doLog()方法两种方法实现获取分支两个版本间的merge log;并将log信息写到文件中。
  2. 将java程序打成jar包,将jar包传到SVN远程库,在jenkins端输入SVN分支的URL、name、password、startRevision、endRevision五个参数。,若startRevision > endRevision,则降序输出 merge log 信息。
  3. 在java程序中实现获取Jenkinsfile中的这几个环境变量的值,Jenkins端填写这几个变量的值点击构建后,会自动替换jar包里的这几个参数。
  4. 输出文件采用FileWriter(,append:true)字符流追加模式输出。
  5. 每一列的对齐方式采用String.format(“%-30s%-30s%-25s\t\t%s”)与\t换行结合实现对齐输出。
  6. 点击构建后生成的log信息文件会默认存在jenkins的workspace下_jar所在文件夹内。

二、详细java_demo代码设计及jenkins页面展示:

package com.test;

import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
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.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

public class branch_mergeLog_RW {
    public static void main(String[] args) throws SVNException, IOException {

        //set url=http://xxx && set name=username && set password=password && set startRevision=15 && set endRevision=4 && java -jar svn_merge_log.jar

        //获取当前系统环境变量
        Map<String,String> env = System.getenv();
        String url = "http://xxx";
        SVNURL repositoryURL = SVNURL.parseURIEncoded(url);
        String name = "useranme";
        String password = "password";
        //若startRevision > endRevision,log信息降序输出;
        int startRevision = 15;
        int endRevision = 4;

        if(env.containsKey("url")){
            url = env.get("url");
        }

        if(env.containsKey("name")){
            name = env.get("name");
        }

        if(env.containsKey("password")){
            password = env.get("password");
        }

        if(env.containsKey("startRevision")){
            String startstr = env.get("startRevision").trim();
            if(startstr.matches("\\d+")) {
                startRevision = Integer.parseInt(startstr);
            } else {
                System.out.println("Invalid start value: " + startstr);
            }
        }

        if(env.containsKey("endRevision")){
            String startstr = env.get("endRevision").trim();
            if(startstr.matches("\\d+")) {
                endRevision = Integer.parseInt(startstr);
            } else {
                System.out.println("Invalid start value: " + startstr);
            }
        }

        DAVRepositoryFactory.setup();

//        define paths and revision ranges
//        File[] path = new File[] {new File("branch本地working copy地址")};
        Collection<SVNRevisionRange> revisionRanges = new ArrayList<SVNRevisionRange>();
        revisionRanges.add(new SVNRevisionRange(SVNRevision.create(startRevision),SVNRevision.create(endRevision)));


        //create authentication manager and set credentials
        SVNRepository repository = null;
        repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded( String.valueOf(repositoryURL)));
        ISVNAuthenticationManager authManagier = SVNWCUtil.createDefaultAuthenticationManager(name,password);
        repository.setAuthenticationManager(authManagier);


        //create a client manager and a log client
        SVNClientManager clientManager = SVNClientManager.newInstance();
        SVNLogClient logClient = clientManager.getLogClient();

        System.out.println(String.format("%-30s%-30s%-25s\t\t\t%s","Revision","Author","Date","Message"));
        System.out.println("-------------------------------------------------------------------------------------------------");

        File mergeLogFile = new File("branch_merge_log.txt");
        FileWriter writer = new FileWriter(mergeLogFile,true);
        System.out.println(String.format("%-30s%-30s%-25s\t\t\t%s","Revision","Author","Date","Message"));
        System.out.println("-------------------------------------------------------------------------------------------------");
        writer.close();


        //define the log entry hanlder to process the log entries(创建日志消息管理器)
        ISVNLogEntryHandler handler = new ISVNLogEntryHandler() {
            @Override
            public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {

                try {
                    if (logEntry.getMessage()!=null){

                        System.out.println(String.format("%-30s%-30s%-25s\t\t%s",logEntry.getRevision(),logEntry.getAuthor(),logEntry.getDate(),logEntry.getMessage()));
                    }

                    if (logEntry.getMessage()!=null){
                        File mergeLogFile = new File("branch_merge_log.txt");
                        FileWriter writer = new FileWriter(mergeLogFile,true);
                        writer.write(String.format("%-30s%-30s%-25s\t\t%s",logEntry.getRevision(),logEntry.getAuthor(),logEntry.getDate(),logEntry.getMessage()));
                        writer.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

//        retrieve the log entries and process them with the handler
        logClient.doLog(repositoryURL,null,null,revisionRanges,true,true,true,0,null,handler);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值