linux跑了一个需要经常pull代码的java项目…
每次从gitee拉代码无论是用.sh还是直接命令行都很麻烦而且担心搞错分支…
pom.xml
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.12.0.202106070339-r</version>
</dependency>
application.properties
# 应用名称
spring.application.name=mygit2
# 应用服务 WEB 访问端口
server.port=10000
# git仓库地址
GitRepository.Url = https://gitee.com/dongbao126/springboot
# git分支名称
GitRepository.BranchName = master
# git项目名字
GitRepository.ProjectName = springboot
# git登录名字
GitRepository.UserName = XX@XX.com
# git登录密码
GitRepository.Password = XXXXXX
# git本地路径
GitRepository.LocalRootPath = D:\\my_project
GitController.java
package com.dongtech.mygit2;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PullResult;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.x509.OtherName;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class GitController {
private static final Logger logger = LoggerFactory.getLogger(GitController.class);
private Git git;
private Repository localRepo;
@Autowired
private GitConfig gitConfig;
@RequestMapping("/Git-Sync")
@ResponseBody
public String GitSync(){
logger.info("Git Sync: start..."+getCurrentTime());
boolean isSuccess = false;
UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(gitConfig.GitUserName,gitConfig.GitPassword);
try {
logger.info("============localPath==========" + getLocalPath());
localRepo = new FileRepository(getLocalPath() + "/.git");
git = new Git(localRepo);
File localPathFile = new File(getLocalPath());
if (!localPathFile.exists()) {
Git git = GitClone(gitConfig.GitUrl, gitConfig.GitBranchName, getLocalPath(),credentialsProvider);
if (git != null){
isSuccess = true;
}
} else {
isSuccess = GitPull(gitConfig.GitBranchName,credentialsProvider);
}
} catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
logger.info("Git Sync: end..."+getCurrentTime());
return "git sync failed";
}finally{
logger.info("Git Sync: end..."+getCurrentTime());
}
if (isSuccess)
return "git sync success";
return "git sync failed";
}
/**
* 获取本地路径
* @return
*/
private String getLocalPath(){
return gitConfig.LocalRootPath + File.separator + gitConfig.GitProjectName;
}
/**
* 获取当前时间
* @return
*/
private String getCurrentTime(){
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
return formatter.format(date);
}
/**
* 执行GitClone
*/
private Git GitClone(String gitUrl, String branch, String localPath, UsernamePasswordCredentialsProvider credentialsProvider) throws Exception {
return Git.cloneRepository().setURI(gitUrl).setBranch(branch).setDirectory(new File(localPath)).setCredentialsProvider(credentialsProvider).call();
}
/**
* 执行GitPull
*/
private boolean GitPull(String branch,UsernamePasswordCredentialsProvider credentialsProvider) throws Exception {
// 强制stash 保证不会冲突
git.stashCreate();
//TODO 更多命令
//git.pull();
//git.add();
//git.push();
//git.merge();
PullResult result = git.pull().setRemoteBranchName(branch).setCredentialsProvider(credentialsProvider).call();
return result.isSuccessful();
}
}
直接运行
java -jar mygit2-1.0.0.jar
本地分支git 落后于 remote分支
访问localhost:10000/Git-Sync
本地分支已被更新至最新
其实这个库支持pull/stash/add/merge/push多种命令的…需要的可以自由摸索…
github