一、获取个人Token
这里是添加一个个人Token,输出Token name,过期时间,使用范围。
注意:该token只会展示一次,注意保存。
注意:不是下面的Feed token。
二、使用脚本
package org.example.gitlab;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StreamUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Iterator;
/**
* gitlab 一次性克隆多个项目脚本
*/
public class Main {
/**
* gitlab用户名
*/
private static final String USERNAME = "username";
/**
* gitlab密码
*/
private static final String PASSWORD = "password";
/**
* 配置克隆到的地址
*/
private static final String TO_DIR = "/Users/xxx/xxx/xxx/xxx";
/**
* gitlab地址
*/
private static final String GITLAB_URL = "http://xxx:xxx";
/**
* 你的private token
*/
private static final String GITLAB_PRIVATE_TOKEN = "xxx";
public static void main(String[] args) throws IOException {
//获取项目的json
JSONArray projects = getProjectJson();
//指定下载后输出的地址
File execDir = new File(TO_DIR);
for (Iterator iterator = projects.iterator(); iterator.hasNext(); ) {
JSONObject prj = (JSONObject) iterator.next();
//项目地址
String httpUrlToRepo = prj.getString("http_url_to_repo");
StringBuilder builder = new StringBuilder(httpUrlToRepo);
String usernameAndPassword = USERNAME + ":" + PASSWORD + "@";
builder.insert(7, usernameAndPassword);
httpUrlToRepo = builder.toString();
//项目路径
String pathWithNamespace = prj.getString("path_with_namespace");
gitClone(httpUrlToRepo, pathWithNamespace, execDir);
}
}
private static JSONArray getProjectJson() throws IOException {
//这里的per_page 是指一页返回多少个项目
URL url = new URL(String.format("%s/api/v4/projects?per_page=100", GITLAB_URL));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("PRIVATE-TOKEN", GITLAB_PRIVATE_TOKEN);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
}
reader.close();
System.out.println("res:" + responseBuilder);
return JSONObject.parseArray(responseBuilder.toString());
}
static void gitClone(String httpUrlToRepo, String pathWithNamespace, File execDir) {
String command = String.format("git clone %s %s", httpUrlToRepo, pathWithNamespace);
try {
System.out.println("===========start==================");
System.out.println("start exec command : " + command);
//执行命令行
Process exec = Runtime.getRuntime().exec(command, null, execDir);
exec.waitFor();
String successResult = StreamUtils.copyToString(exec.getInputStream(), Charset.forName("UTF-8"));
String errorResult = StreamUtils.copyToString(exec.getErrorStream(), Charset.forName("UTF-8"));
System.out.println("successResult: " + successResult);
System.out.println("errorResult: " + errorResult);
System.out.println("============end================");
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置项:
- USERNAME:GitLab用户名
- PASSWORD:GitLab密码
- TO_DIR:代码需要克隆到的路径
- GITLAB_URL:GitLab地址及端口
- GITLAB_PRIVATE_TOKEN:第一步获取都的token
配置完后,IDE中运行即可。