服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - 批量上传Jar包到Maven私服的工具的方法

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

2021-06-22 13:40isea533 Java教程

这篇文章主要介绍了批量上传Jar包到Maven私服的工具的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

基本信息

适用环境:内网环境下的 maven 私服,无法连接外网(或者需要翻墙),需要通过其他手段下载完依赖后导入到内网私服的情况。

功能描述:

单个依赖包含的pom,jar等文件应该在一个单独的目录中,可以指定下面的路径,上传 gson 到私服。

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

还可以指定到 f:\\.m2\\repository\\gson\\gson,上传 gson 的多个版本。

也可以直接 f:\\.m2\\repository,将整个仓库下面的所有 jar 包的所有版本都上传到私服。

注意: 上传前,如果允许重复上传到私服,就需要在私服配置,允许 redeploy,否则已经存在的会报错。

下载 jar 包

如果是下载单个的jar包,可以从  http://mvnrepository.com/ 搜素下载,下载的时候(根据连接打开一个地址,下载pom,jar,source,javadoc)。

如果是针对项目,可以先配置一个新的本地仓库路径(避免和已有jar搅和一起不好区分)。

为了可以下载source和javadoc,在 settings.xml 中增加下面的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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

需要在 settings.xml 中设置好账号密码,参考如下。

?
1
2
3
4
5
<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的前台进行上传,为了可以自动批量上传,我们可以写个小程序来利用这个命令进行批量操作。

当写一个可以批量上传依赖的程序时,还需要考虑如果packaging=pom或者packaging=bundle时,需要特殊处理。pom时,dfile dpomfile两个参数都指定为pom文件即可,bundle时,需要指定-dpackaging=jar,由于jar时这个参数也没问题,所以无论bundle还是jar都带上这个命令。

下面开始代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * 上传依赖到 maven 私服
 *
 * @author liuzenghui
 * @since 2017/7/31.
 */
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 是基础的命令部分。

  • cmd /c 可以保证使用 java 的runtime 执行命令时,可以找到命令。
  • -s f:\\.m2\\settings.xml 参数指定了配置文件的路径(避免多个配置的时候不知道配置那个)。
  • deploy:deploy-file 是上传文件的命令。
  • -durl=xxx指定了上传的位置,从nexus中可以找到这个地址。
  • -drepositoryid=thirdpart必须和上面指定的地址一致,从nexus仓库配置可以看到这个id,另外上面提到的settings.xml中的用户密码要和这个id匹配。
  • -dgeneratepom=false因为我们会传pom文件,所以禁用自动生成。

后面的 date_pattern主要是存在快照版时,忽略日期形式的版本,只保留snapshot形式的。

再后面获取了一个 cmd 和一个线程池。

继续代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
  }
}

创建了一个文件来记录错误信息,并且提供了一个静态方法方便使用。

下面是参数校验和提示信息。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}

当为pom类型时,只需要上传pom。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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文件。

当上传的文件包含 jar 时,使用下面的方式。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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){
         //当有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的目录和文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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方法中,有两种调用方式。

?
1
2
3
4
5
6
7
8
9
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());

如果想通过命令行调用时指定目录,可以用下面的main方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
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,还可以打包到本地仓库中。

虽然内网使用私服的情况不常见,如果遇到这种情况,使用这个代码批量传多少jar包都会变得很容易。

完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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 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){
          //当有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();
        }
      }
    });
  }
}

使用方式

  • 导入项目直接运行 main 方法。
  • 使用 javac 编译为class后运行,由于代码存在中文,java代码需要使用utf8格式保存,编译时通过-encoding utf8参数指定。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/isea533/article/details/77197017

延伸 · 阅读

精彩推荐