Java执行bat脚本提交github和gitee

2020-01-14   阅读(75)  

目的:
希望给网站发送http请求,自动生成 github 和 gitee markdown 页面,然后提交

 

实操:

1、win7 环境制作 bat 脚本,更新和提交 git

更新脚本 git_pull.bat

@echo off
echo Starting git pull constxiong
E:
rem 更新github
cd E:\ConstXiong_github\JavaInterviewQuestion
echo %cd%
git pull

rem 更新gitee
cd E:\ConstXiong_gitee\JavaInterviewQuestion
echo %cd%
git pull

 

提交脚本 git_push.bat,自动复制 github 目录文件内容到 gitee 目录

rem 提交github
@echo off
echo Starting git push constxiong
E:
cd E:\ConstXiong_github\JavaInterviewQuestion
echo %cd%
git add ./*
git commit -m 'JavaInterviewQuestion'
git push

rem 复制github文件到gitee文件目录,提交
xcopy E:\ConstXiong_github\JavaInterviewQuestion\* E:\ConstXiong_gitee\JavaInterviewQuestion\ /y /e
cd E:\ConstXiong_gitee\JavaInterviewQuestion
echo %cd%
git add ./*
git commit -m 'JavaInterviewQuestion'
git push

 

注:提交脚本的自动提交,需要配置 .git/config 文件中 url 字段值携带用户名和密码

如我的用户名:constxiong,密码:123456
url = https://constxiong:123456@github.com/ConstXiong/JavaInterviewQuestion.git

 

2、用代码生成自己希望格式的 markdown 文件

 

3、Java 执行 bat 可执行脚本

package constxiong.xtools.processor;

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

/**
 * windows中 执行 cmd 或 bat
 * @author ConstXiong
 * @date 2020-01-03 11:07:48
 */
public class CmdProcessor {
	
	public static void main(String[] args) {
		//提交github
		callCmd("E:\\resources\\bat\\git_pull.bat");
		//生成内容,省略
		//提交github
		callCmd("E:\\resources\\bat\\git_push.bat");
	}
	
	/**
	 * windows 调用 cmd 或 bat 
	 * @param cmdFilePath
	 */
	public static void callCmd(String cmdFilePath) {
		BufferedReader br = null;
		try {
			Process p = Runtime.getRuntime().exec(cmdFilePath);
			InputStream is = p.getInputStream();
			br = new BufferedReader(new InputStreamReader(is, "GBK"));
			String line;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			p.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

 

执行结果(比脚本中多包含了 xtools 目录):

Starting git pull constxiong
E:\ConstXiong_gitee\tools
Already up-to-date.
E:\ConstXiong_github\xtools
Already up-to-date.
E:\ConstXiong_github\JavaInterviewQuestion
Already up-to-date.
E:\ConstXiong_gitee\JavaInterviewQuestion
Already up-to-date.
Starting git push constxiong
.
.
.
复制了 369 个文件
E:\ConstXiong_gitee\JavaInterviewQuestion
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
E:\ConstXiong_github\JavaInterviewQuestion
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
E:\ConstXiong_github\xtools
[master de5ae2d] 'xtools'
 1 file changed, 3 insertions(+), 1 deletion(-)

 

ConstXiong 备案号:苏ICP备16009629号-3