Windows SVN变更发送邮件通知(JAVA实现)

之前有过一篇python的实现http://blog.csdn.net/wiker_yong/article/details/10334967

 

1,新增文件post-commit.bat

内容:

rem REPOS-PATH (the path to this repository)
set REPOS=%1
rem REV (the number of the revision just committed)
set REV=%2

set HOOK_DIR=F:/Repositories/版本库名/hooks
java -jar %HOOK_DIR%/SendMail.jar %REPOS% %REV%


放在F:/Repositories/版本库名/hooks下(依具体路径而定,此处仅做参考)

2,SVNSendMail.java

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Wiker Yong Email:<a href="mailto:[email protected]">[email protected]</a>
 * @date 2013-8-27 上午10:28:54
 * @version 1.0-SNAPSHOT
 */
public class SVNSendEmail {
    
    private static String VERSION = null;
    private static String REPOSITORIES = null;
    private static String SVNLOOK_BIN_PATH = "C:/Program Files/VisualSVN Server/bin/svnlook.exe";  //VisualSVN Server安装目录
    
    private static String emailSubject = "SVN 变更通知"; //邮件主题
    
    public static void main(String[] args) {
        REPOSITORIES = args[0];
        VERSION = args[1];
        
        StringBuffer emailContent = new StringBuffer();
        emailContent.append("<html> <h2 style=\"color:#FFFFFF; background: #008040;\">基本信息</h2>");
        emailContent.append("<div> <b>版本库:</b> <a href=\"http://blog.csdn.net/wiker_yong\">"
                + REPOSITORIES + "</a> </div>");
        emailContent.append("<div> <b>版本号:</b>" + VERSION + " </div>");
        emailContent.append(" <div> <b>提交者:</b>" + getAuthor() + " </div>");
        emailContent.append("<div> <b>提交时间:</b>" + getDate() + " </div>");
        emailContent.append("<h2 style=\"color:#FFFFFF; background: #4682B4;\">提交说明</h2>");
        emailContent.append("<font size=\"4\" color=\"#BF6000\"><xmp>" + getLog() + "</xmp></font>");
        emailContent.append("<h2 style=\"color:#FFFFFF; background: #5353A8;\">文件清单</h2>");
        emailContent.append("<xmp>" + getChangeList() + "</xmp> <hr>");
        emailContent.append("<center> ☆ Powered by <a href=\"http://blog.csdn.net/wiker_yong\">Wiker Yong</a> </center> </html>");
        
        MailUtils mail = new MailUtils();
        mail.setSubject(emailSubject);
        mail.setContent(emailContent.toString());
        mail.sendMail();
    }
    
    public static String getRepoName() {
        return REPOSITORIES;
    }
    
    public static String getAuthor() {
        try {
            String[] cmd = {
                SVNLOOK_BIN_PATH, "author", "-r", VERSION, REPOSITORIES
            };
            Process proc = Runtime.getRuntime().exec(cmd);
            BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String output = "";
            String returnStr = "";
            while ((output = stdout.readLine()) != null) { // 接受bat执行结果.有可能bat会执行不成功
                System.out.println(output); // 打印exe执行的结果
                returnStr += output;
            }
            return returnStr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    public static String getDate() {
        try {
            String[] cmd = {
                SVNLOOK_BIN_PATH, "date", "-r", VERSION, REPOSITORIES
            };
            Process proc = Runtime.getRuntime().exec(cmd);
            BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String output = "";
            String returnStr = "";
            while ((output = stdout.readLine()) != null) { // 接受bat执行结果.有可能bat会执行不成功
                System.out.println(output); // 打印exe执行的结果
                returnStr += output;
            }
            return returnStr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    public static String getLog() {
        try {
            String[] cmd = {
                SVNLOOK_BIN_PATH, "log", "-r", VERSION, REPOSITORIES
            };
            Process proc = Runtime.getRuntime().exec(cmd);
            BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String output = "";
            String returnStr = "";
            while ((output = stdout.readLine()) != null) { // 接受bat执行结果.有可能bat会执行不成功
                System.out.println(output); // 打印exe执行的结果
                returnStr += output;
            }
            return returnStr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    public static String getChangeList() {
        try {
            String[] cmd = {
                SVNLOOK_BIN_PATH, "changed", "-r", VERSION, REPOSITORIES
            };
            Process proc = Runtime.getRuntime().exec(cmd);
            BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String output = "";
            String returnStr = "";
            while ((output = stdout.readLine()) != null) { // 接受bat执行结果.有可能bat会执行不成功
                System.out.println(output); // 打印exe执行的结果
                returnStr += output;
            }
            return returnStr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

同样放在F:/Repositories/版本库名/hooks
可以测试提交一个文件。效果如下:

 

Windows SVN变更发送邮件通知(JAVA实现)_第1张图片


源代码下载地下(包含相关class和引用jar包。免积分):http://download.csdn.net/detail/yangwei19680827/6018571


 

你可能感兴趣的:(windows)