批量上传Jar包到Maven私服的工具的方法

批量上传Jar包到Maven私服的⼯具的⽅法
基本信息
适⽤环境:内⽹环境下的 Maven 私服,⽆法连接外⽹(或者需要),需要通过其他⼿段下载完依赖后导⼊到内⽹私服的情况。
功能描述:
单个依赖包含的pom,jar等⽂件应该在⼀个单独的⽬录中,可以指定下⾯的路径,上传 gson 到私服。
水玻璃铸造
还可以指定到f:\\.m2\\repository\\Gson\\gson,上传 gson 的多个版本。
也可以直接f:\\.m2\\repository,将整个仓库下⾯的所有 jar 包的所有版本都上传到私服。
注意:上传前,如果允许重复上传到私服,就需要在私服配置,允许 redeploy,否则已经存在的会报错。
下载 Jar 包
如果是针对项⽬,可以先配置⼀个新的本地仓库路径(避免和已有jar搅和⼀起不好区分)。
为了可以下载source和javadoc,在 l 中增加下⾯的配置:
<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
在项⽬下⾯执⾏:mvn clean install命令。
执⾏完成后,再次执⾏:mvn dependency:sources下载源码。
如果需要 javadoc ,可以执⾏命令:mvn dependency:resolve -Dclassifier=javadoc
需要在 l 中设置好账号密码,参考如下。
<server>
<id>thirdpart</id>
<username>admin</username>
<password>123456</password>
</server>
上传命令
使⽤下⾯的命令可以上传依赖到私服。
复制代码代码如下:
mvn deploy:deploy-file -Durl=file:///home/me/m2-repo -po.id -Dfile=./path/to/artifact-name-1.0.jar -DpomFile=./path/l -Dsources=./path/to/artifact-name-1.0-sources.jar -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
so.csdn/api/v3/search?p=1&t=all&q=⾃动化
⼿动使⽤这个命令上传时,还不如直接通过nexus的前台进⾏上传,为了可以⾃动批量上传,我们可以写个⼩程序来利⽤这个命令进⾏批量操作。
当写⼀个可以批量上传依赖的程序时,还需要考虑如果packaging=pom或者packaging=bundle时,需要特殊处理。pom时,Dfile DpomFile两个参数都指定为pom⽂件即可,bundle时,需要指定-Dpackaging=jar,由于jar时这个参数也没问题,所以⽆论bundle还
是jar都带上这个命令。
下⾯开始代码。
/**
* 上传依赖到 Maven 私服
*
* @author liuzenghui
* @since 2017/7/31.
*/
public class Deploy {
/**
* mvn -s F:\.l
* deploy:deploy-file
* -Durl=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\\l " +
"deploy:deploy-file " +
"-Durl=IP:PORT/nexus/content/repositories/thirdpart " +
"-DrepositoryId=thirdpart " +
"-DgeneratePom=false";
public static final Pattern DATE_PATTERN = Patternpile("-[\\d]{8}\\.[\\d]{6}-");
public static final Runtime CMD = Runtime();
public static final Writer ERROR;
public static final ExecutorService EXECUTOR_SERVICE = wFixedThreadPool(10);
先看第⼀部分,BASE_CMD 是基础的命令部分。
cmd /c可以保证使⽤ Java 的runtime 执⾏命令时,可以到命令。
-s F:\\.m2\\l参数指定了配置⽂件的路径(避免多个配置的时候不知道配置那个)。
deploy:deploy-file是上传⽂件的命令。
-Durl=xxx指定了上传的位置,从nexus中可以到这个地址。
-DrepositoryId=thirdpart必须和上⾯指定的地址⼀致,从nexus仓库配置可以看到这个id,另外上⾯提到的l中的⽤户密码要和这个id匹配。
-DgeneratePom=false因为我们会传pom⽂件,所以禁⽤⾃动⽣成。
后⾯的DATE_PATTERN主要是存在快照版时,忽略⽇期形式的版本,只保留SNAPSHOT形式的。
再后⾯获取了⼀个CMD和⼀个线程池。
继续代码。
static {
Writer err = null;
try {
err = new OutputStreamWriter(new FileOutputStream("deploy-error.log"), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
ERROR = err;
}
public static void error(String error){
try {
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 (!ists()) {
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 = adLine()) != null){
im().indexOf("<packaging>pom</packaging>")!=-1){
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try{reader.close();}catch(Exception e){}
}
return false;
}
当为pom类型时,只需要上传pom。
public static void deployPom(final File pom) {
ute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").Name());
cmd.append(" -Dfile=").Name());
try {
final Process proc = (String(), null, ParentFile());
热镀锌合金InputStream inputStream = InputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);          BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n=====================================\n");          while((line = adLine()) != 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("上传失败:" + AbsolutePath());
}
} catch (IOException e) {
error("上传失败:" + AbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上传失败:" + AbsolutePath());
e.printStackTrace();
}
}
});
}
注意DpomFile和Dfile都指定的pom⽂件。
当上传的⽂件包含 jar 时,使⽤下⾯的⽅式。
public static void deploy(
final File pom, final File jar, final File source, final File javadoc) {
ute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").Name());
if(jar != null){
//当有bundle类型时,下⾯的配置可以保证上传的jar包后缀为.jar
cmd.append(" -Dpackaging=jar -Dfile=").Name());
} else {
cmd.append(" -Dfile=").Name());
}
if(source != null){
cmd.append(" -Dsources=").Name());
}
if(javadoc != null){
cmd.append(" -Djavadoc=").Name());
}
try {
final Process proc = (String(), null, ParentFile());
InputStream inputStream = InputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);          BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n=====================================\n");          while((line = adLine()) != 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("上传失败:" + AbsolutePath());
}
} catch (IOException e) {
error("上传失败:" + AbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上传失败:" + AbsolutePath());
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 = Name();
if(DATE_PATTERN.matcher(name).find()){
//skip
} else if (dsWith(".pom")) {
pom = file;
} else if (dsWith("-javadoc.jar")) {
javadoc = file;
} else if (dsWith("-sources.jar")) {
source = file;
} else if (dsWith(".jar")) {
jar = file;
}
}
if(pom != null){
if(jar != null){
deploy(pom, jar, source, javadoc);
} else if(packingIsPom(pom)){
deployPom(pom);
}
}
}
}
在main⽅法中,有两种调⽤⽅式。
u形管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\ xx\\1.0.0\\").listFiles());
如果想通过命令⾏调⽤时指定⽬录,可以⽤下⾯的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参数为-Durl=E:\\.m2\\repository,还可以打包到本地仓库

本文发布于:2024-09-22 02:06:29,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/179216.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:上传   配置   私服
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议