批量將現(xiàn)有Jar包上傳到Maven私服
基本信息
適用環(huán)境:內(nèi)網(wǎng)環(huán)境下的 Maven 私服,無法連接外網(wǎng)(或者需要翻墻),需要通過其他手段下載完依賴后導(dǎo)入到內(nèi)網(wǎng)私服的情況。
功能描述:
單個依賴包含的pom,jar等文件應(yīng)該在一個單獨(dú)的目錄中,可以指定下面的路徑,上傳 gson 到私服。

還可以指定到f:\\.m2\\repository\\Gson\\gson,上傳gson的多個版本。
也可以直接f:\\.m2\\repository,將整個倉庫下面的所有jar包的所有版本都上傳到私服。
注意:上傳前,如果允許重復(fù)上傳到私服,就需要在私服配置,允許 redeploy,否則已經(jīng)存在的會報(bào)錯。
下載Jar 包
如果是下載單個的jar包,可以從 http://mvnrepository.com/ 搜素下載,下載的時候(根據(jù)連接打開一個地址,下載pom,jar,source,javadoc)。
如果是針對項(xiàng)目,可以先配置一個新的本地倉庫路徑(避免和已有jar攪和一起不好區(qū)分)。
為了可以下載source和javadoc,在 settings.xml 中增加下面的配置:
<profiles> <profile> <id>downloadSources</id> <properties> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </properties> </profile> </profiles> <activeProfiles> <activeProfile>downloadSources</activeProfile> </activeProfiles>
在項(xiàng)目下面執(zhí)行:mvn clean install命令。
執(zhí)行完成后,再次執(zhí)行:mvn dependency:sources下載源碼。
如果需要 javadoc ,可以執(zhí)行命令:mvn dependency:resolve -Dclassifier=javadoc
需要在 settings.xml 中設(shè)置好賬號密碼,參考如下。
<server> <id>thirdpart</id> <username>admin</username> <password>123456</password> </server>
上傳命令
使用下面的命令可以上傳依賴到私服。
mvn deploy:deploy-file -Durl=file:///home/me/m2-repo -DrepositoryId=some.repo.id -Dfile=./path/to/artifact-name-1.0.jar -DpomFile=./path/to/pom.xml -Dsources=./path/to/artifact-name-1.0-sources.jar -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
自動化
手動使用這個命令上傳時,還不如直接通過nexus的前臺進(jìn)行上傳,為了可以自動批量上傳,我們可以寫個小程序來利用這個命令進(jìn)行批量操作。
當(dāng)寫一個可以批量上傳依賴的程序時,還需要考慮如果packaging=pom或者packaging=bundle時,需要特殊處理。pom時,Dfile DpomFile兩個參數(shù)都指定為pom文件即可,bundle時,需要指定-Dpackaging=jar,由于jar時這個參數(shù)也沒問題,所以無論bundle還是jar都帶上這個命令。
下面開始代碼。
/**
* 上傳依賴到 Maven 私服
* @author
* @since
*/
public class Deploy {
/**
* mvn -s F:\.m2\settings.xml
* deploy:deploy-file
* -Durl=http://IP:PORT/nexus/content/repositories/thirdpart
* -DrepositoryId=thirdpart
* -Dfile=antlr-2.7.2.jar
* -DpomFile=antlr-2.7.2.pom
* -Dpackaging=jar
* -DgeneratePom=false
* -Dsources=./path/to/artifact-name-1.0-sources.jar
* -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
*/
public static final String BASE_CMD = "cmd /c mvn " +
"-s F:\\.m2\\settings.xml " +
"deploy:deploy-file " +
"-Durl=http://IP:PORT/nexus/content/repositories/thirdpart " +
"-DrepositoryId=thirdpart " +
"-DgeneratePom=false";
public static final Pattern DATE_PATTERN = Pattern.compile("-[\\d]{8}\\.[\\d]{6}-");
public static final Runtime CMD = Runtime.getRuntime();
public static final Writer ERROR;
public static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(10);
先看第一部分,BASE_CMD 是基礎(chǔ)的命令部分。
- cmd /c 可以保證使用 Java 的runtime 執(zhí)行命令時,可以找到命令。
- -s F:\\.m2\\settings.xml 參數(shù)指定了配置文件的路徑(避免多個配置的時候不知道配置那個)。
- deploy:deploy-file 是上傳文件的命令。
- -Durl=xxx指定了上傳的位置,從nexus中可以找到這個地址。
- -DrepositoryId=thirdpart必須和上面指定的地址一致,從nexus倉庫配置可以看到這個id,另外上面提到的settings.xml中的用戶密碼要和這個id匹配。
- -DgeneratePom=false因?yàn)槲覀儠鱬om文件,所以禁用自動生成。
后面的DATE_PATTERN主要是存在快照版時,忽略日期形式的版本,只保留SNAPSHOT形式的。
再后面獲取了一個CMD和一個線程池。
繼續(xù)代碼。
static {
Writer err = null;
try {
err = new OutputStreamWriter(new FileOutputStream("deploy-error.log"), "utf-8");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
ERROR = err;
}
public static void error(String error){
try {
System.err.println(error);
ERROR.write(error + "\n");
ERROR.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
創(chuàng)建了一個文件來記錄錯誤信息,并且提供了一個靜態(tài)方法方便使用。
下面是參數(shù)校驗(yàn)和提示信息。
public static boolean checkArgs(String[] args){
if (args.length != 1) {
System.out.println("用法如: java -jar Deploy D:\\some\\path\\");
return false;
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println(args[0] + " 目錄不存在!");
return false;
}
if (!file.isDirectory()) {
System.out.println("必須指定為目錄!");
return false;
}
return true;
}
下面方法判斷pom文件的packaging 是否為 pom:
public static boolean packingIsPom(File pom){
BufferedReader reader = null;
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
String line;
while((line = reader.readLine()) != null){
if(line.trim().indexOf("<packaging>pom</packaging>")!=-1){
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try{reader.close();}catch(Exception e){}
}
return false;
}
當(dāng)為pom類型時,只需要上傳pom。
public static void deployPom(final File pom) {
EXECUTOR_SERVICE.execute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").append(pom.getName());
cmd.append(" -Dfile=").append(pom.getName());
try {
final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n=====================================\n");
while((line = reader.readLine()) != null){
if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
logBuffer.append(
Thread.currentThread().getName() + " : " + line + "\n");
}
}
System.out.println(logBuffer);
int result = proc.waitFor();
if(result != 0){
error("上傳失?。? + pom.getAbsolutePath());
}
} catch (IOException e) {
error("上傳失?。? + pom.getAbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上傳失?。? + pom.getAbsolutePath());
e.printStackTrace();
}
}
});
}
注意DpomFile和Dfile都指定的pom文件。
當(dāng)上傳的文件包含 jar 時,使用下面的方式。
public static void deploy(
final File pom, final File jar, final File source, final File javadoc) {
EXECUTOR_SERVICE.execute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").append(pom.getName());
if(jar != null){
//當(dāng)有bundle類型時,下面的配置可以保證上傳的jar包后綴為.jar
cmd.append(" -Dpackaging=jar -Dfile=").append(jar.getName());
} else {
cmd.append(" -Dfile=").append(pom.getName());
}
if(source != null){
cmd.append(" -Dsources=").append(source.getName());
}
if(javadoc != null){
cmd.append(" -Djavadoc=").append(javadoc.getName());
}
try {
final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n=====================================\n");
while((line = reader.readLine()) != null){
if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
logBuffer.append(
Thread.currentThread().getName() + " : " + line + "\n");
}
}
System.out.println(logBuffer);
int result = proc.waitFor();
if(result != 0){
error("上傳失?。? + pom.getAbsolutePath());
}
} catch (IOException e) {
error("上傳失?。? + pom.getAbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上傳失?。? + pom.getAbsolutePath());
e.printStackTrace();
}
}
});
}
必須有pom和jar,source和javadoc可選。
下面是一個對上面代碼封裝后的方法,這個方法用于迭代查找包含pom,jar,source和javadoc的目錄和文件。
public static void deploy(File[] files) {
if (files.length == 0) {
//ignore
} else if (files[0].isDirectory()) {
for (File file : files) {
if (file.isDirectory()) {
deploy(file.listFiles());
}
}
} else if (files[0].isFile()) {
File pom = null;
File jar = null;
File source = null;
File javadoc = null;
//忽略日期快照版本,如 xxx-mySql-2.2.6-20170714.095105-1.jar
for (File file : files) {
String name = file.getName();
if(DATE_PATTERN.matcher(name).find()){
//skip
} else if (name.endsWith(".pom")) {
pom = file;
} else if (name.endsWith("-javadoc.jar")) {
javadoc = file;
} else if (name.endsWith("-sources.jar")) {
source = file;
} else if (name.endsWith(".jar")) {
jar = file;
}
}
if(pom != null){
if(jar != null){
deploy(pom, jar, source, javadoc);
} else if(packingIsPom(pom)){
deployPom(pom);
}
}
}
}
在main方法中,有兩種調(diào)用方式。
public static void main(String[] args) {
deploy(new File("F:\\.m2\\repository").listFiles());
EXECUTOR_SERVICE.shutdown();
try {
ERROR.close();
} catch (IOException e) {
e.printStackTrace();
}
}
直接指定一個倉庫的目錄即可。
還可以是更具體的目錄:
deploy(new File("F:\\.m2\\repository\\org\\apache\\tomcat\\xxx\\1.0.0\\").listFiles());
如果想通過命令行調(diào)用時指定目錄,可以用下面的main方法。
public static void main(String[] args) {
if(checkArgs(args)){
File file = new File(args[0]);
deploy(file.listFiles());
}
EXECUTOR_SERVICE.shutdown();
try {
ERROR.close();
} catch (IOException e) {
e.printStackTrace();
}
}
通過上面這種方式可以很輕易的將依賴傳到私服中。如果修改上面url參數(shù)為-Durl=E:\\.m2\\repository,還可以打包到本地倉庫中。
雖然內(nèi)網(wǎng)使用私服的情況不常見,如果遇到這種情況,使用這個代碼批量傳多少jar包都會變得很容易。
完整代碼
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
/**
* 上傳依賴到 Maven 私服
* @author
* @since
*/
public class Deploy {
/**
* mvn -s F:\.m2\settings.xml
* org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file
* -Durl=http://IP:PORT/nexus/content/repositories/thirdpart
* -DrepositoryId=thirdpart
* -Dfile=antlr-2.7.2.jar
* -DpomFile=antlr-2.7.2.pom
* -Dpackaging=jar
* -DgeneratePom=false
* -Dsources=./path/to/artifact-name-1.0-sources.jar
* -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
*/
public static final String BASE_CMD = "cmd /c mvn " +
"-s F:\\.m2\\settings.xml " +
"deploy:deploy-file " +
"-Durl=http://IP:PORT/nexus/content/repositories/thirdpart " +
"-DrepositoryId=thirdpart " +
"-DgeneratePom=false";
public static final Pattern DATE_PATTERN = Pattern.compile("-[\\d]{8}\\.[\\d]{6}-");
public static final Runtime CMD = Runtime.getRuntime();
public static final Writer ERROR;
public static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(10);
static {
Writer err = null;
try {
err = new OutputStreamWriter(new FileOutputStream("deploy-error.log"), "utf-8");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
ERROR = err;
}
public static void main(String[] args) {
deploy(new File("F:\\.m2\\repository").listFiles());
// if(checkArgs(args)){
// File file = new File(args[0]);
// deploy(file.listFiles());
// }
EXECUTOR_SERVICE.shutdown();
try {
ERROR.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void error(String error){
try {
System.err.println(error);
ERROR.write(error + "\n");
ERROR.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean checkArgs(String[] args){
if (args.length != 1) {
System.out.println("用法如: java -jar Deploy D:\\some\\path\\");
return false;
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println(args[0] + " 目錄不存在!");
return false;
}
if (!file.isDirectory()) {
System.out.println("必須指定為目錄!");
return false;
}
return true;
}
public static void deploy(File[] files) {
if (files.length == 0) {
//ignore
} else if (files[0].isDirectory()) {
for (File file : files) {
if (file.isDirectory()) {
deploy(file.listFiles());
}
}
} else if (files[0].isFile()) {
File pom = null;
File jar = null;
File source = null;
File javadoc = null;
//忽略日期快照版本,如 xxx-mySql-2.2.6-20170714.095105-1.jar
for (File file : files) {
String name = file.getName();
if(DATE_PATTERN.matcher(name).find()){
//skip
} else if (name.endsWith(".pom")) {
pom = file;
} else if (name.endsWith("-javadoc.jar")) {
javadoc = file;
} else if (name.endsWith("-sources.jar")) {
source = file;
} else if (name.endsWith(".jar")) {
jar = file;
}
}
if(pom != null){
if(jar != null){
deploy(pom, jar, source, javadoc);
} else if(packingIsPom(pom)){
deployPom(pom);
}
}
}
}
public static boolean packingIsPom(File pom){
BufferedReader reader = null;
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
String line;
while((line = reader.readLine()) != null){
if(line.trim().indexOf("<packaging>pom</packaging>")!=-1){
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try{reader.close();}catch(Exception e){}
}
return false;
}
public static void deployPom(final File pom) {
EXECUTOR_SERVICE.execute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").append(pom.getName());
cmd.append(" -Dfile=").append(pom.getName());
try {
final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n==================================\n");
while((line = reader.readLine()) != null){
if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
logBuffer.append(Thread.currentThread().getName() + " : " + line + "\n");
}
}
System.out.println(logBuffer);
int result = proc.waitFor();
if(result != 0){
error("上傳失?。? + pom.getAbsolutePath());
}
} catch (IOException e) {
error("上傳失敗:" + pom.getAbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上傳失?。? + pom.getAbsolutePath());
e.printStackTrace();
}
}
});
}
public static void deploy(final File pom, final File jar, final File source, final File javadoc) {
EXECUTOR_SERVICE.execute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").append(pom.getName());
if(jar != null){
//當(dāng)有bundle類型時,下面的配置可以保證上傳的jar包后綴為.jar
cmd.append(" -Dpackaging=jar -Dfile=").append(jar.getName());
} else {
cmd.append(" -Dfile=").append(pom.getName());
}
if(source != null){
cmd.append(" -Dsources=").append(source.getName());
}
if(javadoc != null){
cmd.append(" -Djavadoc=").append(javadoc.getName());
}
try {
final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n=======================================\n");
while((line = reader.readLine()) != null){
if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
logBuffer.append(Thread.currentThread().getName() + " : " + line + "\n");
}
}
System.out.println(logBuffer);
int result = proc.waitFor();
if(result != 0){
error("上傳失敗:" + pom.getAbsolutePath());
}
} catch (IOException e) {
error("上傳失?。? + pom.getAbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上傳失?。? + pom.getAbsolutePath());
e.printStackTrace();
}
}
});
}
}
使用方式
1.導(dǎo)入項(xiàng)目直接運(yùn)行 main 方法。
2.使用 javac 編譯為class后運(yùn)行,由于代碼存在中文,java代碼需要使用utf8格式保存,編譯時通過-encoding utf8參數(shù)指定。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
在deepin上如何使用Fleet開發(fā)SpringBoot?3.0.0項(xiàng)目
這篇文章主要介紹了在deepin上使用Fleet開發(fā)SpringBoot?3.0.0項(xiàng)目的過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
spring?cloud?gateway中配置uri三種方式
gateway?組件是SpringCloud?組件中的網(wǎng)關(guān)組件,主要是解決路由轉(zhuǎn)發(fā)的問題,跟nginx有點(diǎn)類似,區(qū)別是nginx多用在前端上,gateway用在后端上,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-08-08
java簡單實(shí)現(xiàn)八叉樹圖像處理代碼示例
這篇文章主要介紹了java簡單實(shí)現(xiàn)八叉樹圖像處理代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
深入解析堆排序的算法思想及Java代碼的實(shí)現(xiàn)演示
堆排序基于二叉堆結(jié)構(gòu)即完全二叉樹,可利用最大堆和最小堆的組建方式來進(jìn)行排序,這里就來深入解析堆排序的算法思想及Java代碼的實(shí)現(xiàn)演示2016-06-06
java.lang.Runtime.exec的左膀右臂:流輸入和流讀取詳解
這篇文章主要介紹了java.lang.Runtime.exec的左膀右臂:流輸入和流讀取詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

