先看效果(源碼Github地址:https://github.com/qudengr/minitool)
上傳后效果
沒(méi)有任何的第三方依賴(lài)扮休,直接通過(guò)java命令編譯運(yùn)行即可刨疼。
javac NexusComponentsUpload.java
java NexusComponentsUpload
curl命令調(diào)用API上傳
curl -v -u admin:admin123 -X POST 'http://192.168.0.8:8081/service/rest/v1/components?repository=appname-hosted' -F maven2.generate-pom=true -F maven2.groupId=com.xxx.xxx.thirdlib -F maven2.artifactId=ojdbc8 -F maven2.version=12.2.0.1 -F maven2.asset1=@/home/appname/repo/thirdlib/ojdbc8/12.2.0.1/ojdbc8-12.2.0.1.jar -F maven2.asset1.extension=jar
沒(méi)有任何三方依賴(lài),需要在linux借助curl命令上傳。
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class NexusComponentsUpload {
public static final String USAGE =
"Expected Parameters: [options] -U username -P password -h host -p port -r repositoryName -d repositoryDirectory\n" +
"\t-U - nexus username\n" +
"\t-P - nexus password\n" +
"\t-h - nexus host\n" +
"\t-p - nexus port\n" +
"\t-r - nexus repository name\n" +
"\t-d - local repository directory\n";
public static void main(String[] args) {
NexusComponentsUpload nexusComponentsUpload = new NexusComponentsUpload();
String repositoryHost = "localhost";
String repositoryPort = "8081";
String repositoryName = "maven-hosted";
String repositoryDirectory = "~/repo/";
String username = "admin";
String password = "admin123";
int base = 0;
for (base = 0; base < args.length; base++) {
if (args[base].equals("-h")) {
repositoryHost = args[++base];
} else if (args[base].equals("-p")) {
repositoryPort = args[++base];
} else if (args[base].equals("-r")) {
repositoryName = args[++base];
} else if (args[base].equals("-d")) {
repositoryDirectory = args[++base];
} else if (args[base].equals("-U")) {
username = args[++base];
} else if (args[base].equals("-P")) {
password = args[++base];
} else {
break;
}
}
int minParams = 1;
int remain = args.length;
if (remain < minParams) {
if (args.length > 0) {
System.err.println("Actual Parameters: " + Arrays.toString(args));
}
System.err.println(USAGE);
System.exit(1);
}
System.out.println("Repository:" + repositoryName);
System.out.println("Repository:" + repositoryName);
// 獲取所有的pom文件
ArrayList<String> files = getFileLists(repositoryDirectory, ".pom");
// 一個(gè)個(gè)的解析并處理POM文件及對(duì)應(yīng)的jar包
for (String file : files) {
try {
String fileNameWithoutExtension = file.substring(0, file.lastIndexOf("."));
// 解析POM文件
GAVP gavp = nexusComponentsUpload.parsePom(file);
//如果是POM組件,直接上傳
if (gavp.packaging.equals("pom")) {
String cmd = getApiCmd(repositoryHost, repositoryPort, username, password, repositoryName, gavp.groupId, gavp.artifactId, gavp.version, "pom", file);
exec(cmd);
} else {
//如果是jar包組件,判斷一下jar包是存在
String jarFilePath = fileNameWithoutExtension + ".jar";
File jarFile = new File(jarFilePath);
if (jarFile.exists()) {
String cmd = getApiCmd(repositoryHost, repositoryPort, username, password, repositoryName, gavp.groupId, gavp.artifactId, gavp.version, "jar", jarFilePath);
exec(cmd);
}
}
} catch (Exception e) {
System.err.println("Error:"+e);
}
}
}
/**
* 獲取待執(zhí)行的命令字符串
* @param file
* @return
*/
public static String getApiCmd(String host, String port, String username, String password, String repository, String groupId, String artifactId, String version, String extension, String file) {
String apiUri = "/service/rest/v1/components?repository=";
String cmd = "curl -v -u " + username + ":" + password
+ " -X POST \'http://" + host + ":" + port + apiUri + repository + "\'"
+ " -F maven2.generate-pom=true -F maven2.groupId=" + groupId + " -F maven2.artifactId=" + artifactId + " -F maven2.version=" + version
+ " -F maven2.asset1=@" + file + " -F maven2.asset1.extension=" + extension;
return cmd;
}
/**
* 解析POM文件
* @param file
* @return
*/
public GAVP parsePom(String file) {
String fileContent = readFileString(file);
//groupId
String groupId = fileContent.substring(fileContent.indexOf("<groupId>")+9, fileContent.indexOf("</groupId>"));
//artifactId
String artifactId = fileContent.substring(fileContent.indexOf("<artifactId>")+12, fileContent.indexOf("</artifactId>"));
//version
String version = fileContent.substring(fileContent.indexOf("<version>")+9, fileContent.indexOf("</version>"));
//packaging
String packaging = "jar";
if (fileContent.indexOf("<packaging>")>0) {
packaging = fileContent.substring(fileContent.indexOf("<packaging>")+11, fileContent.indexOf("</packaging>"));
}
return new GAVP(groupId, artifactId, version, packaging);
}
/**
* 執(zhí)行命令
* @param cmd
*/
public static void exec(String cmd) {
Process process = null;
try {
System.out.println("============exec cmd============");
System.out.println(cmd);
process = Runtime.getRuntime().exec("/bin/sh");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
bw.write(cmd, 0, cmd.length());
bw.newLine();
bw.write("exit", 0, 4);
bw.newLine();
bw.flush();
try {
process.waitFor();
} catch (InterruptedException e) {
System.err.println(cmd+"\n"+e);
}
InputStream errIs = process.getErrorStream();
InputStream stdIs = process.getInputStream();
int len = errIs.available();
byte[] buf = null;
if (len != 0) {
buf = new byte[len];
System.err.println("============stderr msg============");
errIs.read(buf);
System.err.println(new String(buf, 0, len));
}
len = stdIs.available();
if (len != 0) {
buf = new byte[len];
System.out.println("============stdout msg===========");
stdIs.read(buf);
System.out.println(new String(buf, 0, len));
}
} catch (IOException e) {
System.err.println(e);
} finally {
if (process != null) {
process.destroy();
}
}
}
/**
* 判斷目錄不存在
* @param filePath
* @return
*/
public static boolean directoryExists(String filePath) {
File file = new File(filePath);
return file.isDirectory() && file.exists();
}
/**
* 判斷目錄存在
* @param filePath
* @return
*/
public static boolean directoryNotExists(String filePath) {
return !directoryExists(filePath);
}
/**
* 判斷文件存在
* @param filePath
* @return
*/
public static boolean fileExists(String filePath) {
File file = new File(filePath);
return file.isFile() && file.exists();
}
/**
* 判斷文件不存在
* @param filePath
* @return
*/
public static boolean fileNotExists(String filePath) {
return !fileExists(filePath);
}
/**
* 列出目錄下所有的文件 */
public static ArrayList<String> getFileLists(String directory, String extension) {
ArrayList<String> fileLists = new ArrayList<>();
if (directoryNotExists(directory)) {
return fileLists;
}
File rootDirectory = new File(directory);
File[] files = rootDirectory.listFiles();
for (File file : files) {
if (file.isFile()) {
// 文件
if (file.getAbsolutePath().endsWith(extension) || extension == null || extension.length() == 0) {
fileLists.add(file.getAbsolutePath());
}
} else {
// 子目錄
fileLists.addAll(getFileLists(file.getAbsolutePath(), extension));
}
}
return fileLists;
}
public static String readFileString(String filePath) {
StringBuffer fileString = new StringBuffer("");
if (fileNotExists(filePath)) {
return fileString.toString();
}
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(filePath);
bufferedReader = new BufferedReader(fileReader);
String str;
// read by line
while ((str = bufferedReader.readLine()) != null) {
fileString.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
}
}
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
}
}
}
return fileString.toString();
}
/**
* 存放POM信息
*/
public class GAVP {
public String groupId;
public String artifactId;
public String version;
public String packaging;
GAVP(String groupId,
String artifactId,
String version,
String packaging) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.packaging = packaging;
}
@Override
public String toString() {
return "GAVP{" +
"groupId='" + groupId + '\'' +
", artifactId='" + artifactId + '\'' +
", version='" + version + '\'' +
", packaging='" + packaging + '\'' +
'}';
}
}
}