声明:本文参考
https://blog.csdn.net/isea533/article/details/77197017
https://www.cnblogs.com/fengpingfan/p/5197608.html
https://www.cnblogs.com/yucy/p/7509561.html
【适用场景】:下载jar包、上传单个jar包、批量上传jar包到内网Maven 私服、发布自己的jar包到私服
【适用人群】:IT 懒人
一、下载jar包
下载单个jar包:http://mvnrepository.com/ 搜索下载 pom、jar、source、javadoc
下载多个jar包:配置一个新的本地仓库路径,在 settings.xml 中增加以下的配置(为了可以下载source和javadoc)
<server>
<id>thirdpart</id>
<username>admin</username>
<password>admin123</password>
</server>
<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
二、上传命令
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
当执行命令有报错时,可以在命令的结尾加上"-X"进行调试
三、批量上传程序
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
/**
* 上传依赖到 Maven 私服
*
* @author liuzenghui
* @since 2017/7/31.
*/
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