開發過程當中遇到這樣的需求,Java拉取指定代碼庫指定分支的代碼java代碼,而後有maven打包,將打包好的jar上傳到文件服務器。java
解決思路分三步:git
1.從Git倉庫下載代碼文件github
2.用maven打包下載好的代碼文件apache
3.上傳jarapi
解決方案:服務器
1.利用eclipse提供的JGit工具包實現拉包,Java執行Windows/Linux腳本實現maven構建(Linux相似)eclipse
參考地址:https://github.com/eclipse/jgitmaven
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import java.io.File; public class GitTest { public static void main(String[] arg) { String url = "http://git.mrpei.com/peizhouyu/HelloTest.git"; String localPath = "D:\\project\\"; String branchName = "master"; String username = "peizhouyu"; String password = "123456."; try { //1. 克隆代碼 Git.cloneRepository() .setURI(url).setBranch(branchName) .setDirectory(new File(localPath)) .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password)) .call(); //2.執行mvn打包 並等待命令執行完成 String commond = "cmd /c d: && cd d:\\project && mvn clean package -Dmaven.test.skip=true"; Process process = Runtime.getRuntime().exec(commond); process.waitFor(); System.out.println("package finish"); //3.上傳文件服務 target文件夾 後綴 jar文件 } catch (Exception e) { e.printStackTrace(); } } }
2.通過閱讀JGit源碼發現JGit其實也是依靠本地安裝的git工具來執行代碼下載,因此咱們能夠直接經過命令調用本地Git工具實現(Linux相似)ide
public class LocalTest { public static void main(String[] args) { String url = "http://git.mrpei.com/peizhouyu/HelloTest.git"; String localPath = "D:\\project\\"; String branchName = "master"; String username = "peizhouyu"; String password = "123456."; String newUrl = "http://peizhouyu:123456.@git.jd.com/peizhouyu/HelloTest.git"; try { //0.命令拼接 String[] result = url.split("/"); String projectName = result[result.length - 1].split("\\.")[0]; System.out.println(projectName); //1. 克隆代碼 String cloneCommand = "cmd /c d: && cd d:\\project && git clone -b " + branchName + " " + result[0] + "//" + username + ":" + password + "@" + result[2] + "/" + result[3] + "/" + result[4]; System.out.println(cloneCommand); Process cloneProcess = Runtime.getRuntime().exec(cloneCommand); cloneProcess.waitFor(); System.out.println("clone success"); //2.執行mvn打包 並等待命令執行完成 //String real = "cmd /c d: && cd d:\\project && git clone -b master http://peizhouyu:123456.@git.mrpei.com/peizhouyu/HelloTest.git && cd d:\\project\\HelloTest && mvn clean package -Dmaven.test.skip=true"; String packageCommand = "cmd /c d: && cd d:\\project\\" + projectName + " && mvn clean package -Dmaven.test.skip=true"; System.out.println(packageCommand); Process process = Runtime.getRuntime().exec(packageCommand); process.waitFor(); System.out.println("package finish"); //3.上傳文件服務 target文件夾 後綴 jar文件 } catch (Exception e) { e.printStackTrace(); } } }
3.利用GitLab API 經過http來下載源碼包,本地解壓,調用maven命令編譯。工具
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.tools.tar.TarEntry; import org.apache.tools.tar.TarInputStream; import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.zip.GZIPInputStream; public class GitApiTest { private final static int BUFFER = 1048576; public static void main(String[] args) { String apiUrl = "http://git.xx.com/api/v4/projects/"; String projectId = "11111"; String branchName = "master"; String privateToken = "c1EssB1213232ZAvTdL"; String path = "D:\\project\\hello.tar.gz"; String sourceFolder = "D:\\project\\hello"; String projectDir = null; String url = apiUrl + projectId + "/repository/archive"; System.out.println(url); Map<String,String> map = new HashMap<String, String>(); map.put("private_token",privateToken); map.put("sha",branchName); HttpDownUtil.download("http://git.jd.com/api/v4/projects/30186/repository/archive",map,path); System.out.println("下載完成 開始解壓"); // unCompressArchiveGz(path); //解壓 File project = new File(path); // decompress(project, sourceFolder); try { unTarGz(project,sourceFolder); System.out.println("解壓完成"); //獲取解壓後的文件夾名(項目文件夾名) File[] unZipDirectoryArray = new File(sourceFolder).listFiles(); for (int i = 0; i < unZipDirectoryArray.length; i++){ if (unZipDirectoryArray[i].isDirectory()){ projectDir = unZipDirectoryArray[i].getAbsolutePath(); System.out.println(projectDir); } } //進入項目目錄執行 mvn 構建 System.out.println("開始執行mvn構建"); //projectDir = "D:\\project\\hello\\HelloTest-master-2c25a3cb8bd88d90838cf07eff20bc652fffbe40"; String commond = "cmd /c d: && cd " + projectDir +" && mvn clean package -Dmaven.test.skip=true"; //String commond = "cmd /c d: && cd d:\\project\\hello\\HelloTest-master-8d0c483bf0837bf0bc66914e9d746e1d9af6bad8 && start mvn clean package -Dmaven.test.skip=true"; System.out.println(commond); Process process = Runtime.getRuntime().exec(commond); //讀取標準輸出內容 防止輸出緩衝區阻塞程序執行 String str; BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); while ( (str = bufferedReader.readLine()) !=null) { System.out.println(str); } process.waitFor(); System.out.println("package finish"); } catch (Exception e) { e.printStackTrace(); } } public static void unTarGz(File file,String outputDir) throws IOException{ TarInputStream tarIn = null; try{ tarIn = new TarInputStream(new GZIPInputStream( new BufferedInputStream(new FileInputStream(file))), 1024 * 2); createDirectory(outputDir,null);//建立輸出目錄 TarEntry entry = null; while( (entry = tarIn.getNextEntry()) != null ){ if(entry.isDirectory()){//是目錄 entry.getName(); createDirectory(outputDir,entry.getName());//建立空目錄 }else{//是文件 File tmpFile = new File(outputDir + "/" + entry.getName()); createDirectory(tmpFile.getParent() + "/",null);//建立輸出目錄 OutputStream out = null; try{ out = new FileOutputStream(tmpFile); int length = 0; byte[] b = new byte[2048]; while((length = tarIn.read(b)) != -1){ out.write(b, 0, length); } }catch(IOException ex){ throw ex; }finally{ if(out!=null) out.close(); } } } }catch(IOException ex){ throw new IOException("解壓歸檔文件出現異常",ex); } finally{ try{ if(tarIn != null){ tarIn.close(); } }catch(IOException ex){ throw new IOException("關閉tarFile出現異常",ex); } } } public static void createDirectory(String outputDir,String subDir){ File file = new File(outputDir); if(!(subDir == null || subDir.trim().equals(""))){//子目錄不爲空 file = new File(outputDir + "/" + subDir); } if(!file.exists()){ if(!file.getParentFile().exists()) file.getParentFile().mkdirs(); file.mkdirs(); } } }
生成腳本文件 會增長磁盤I/O 不推薦