- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章批量将现有Jar包上传到Maven私服由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
基本信息 。
适用环境:内网环境下的 maven 私服,无法连接外网(或者需要翻墙),需要通过其他手段下载完依赖后导入到内网私服的情况.
功能描述:
单个依赖包含的pom,jar等文件应该在一个单独的目录中,可以指定下面的路径,上传 gson 到私服.
还可以指定到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
|
<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>
|
上传命令 。
使用下面的命令可以上传依赖到私服.
1
|
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
|
/**
* 上传依赖到 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 是基础的命令部分.
后面的date_pattern主要是存在快照版时,忽略日期形式的版本,只保留snapshot形式的.
再后面获取了一个cmd和一个线程池.
继续代码.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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
|
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
|
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
){
//当有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.导入项目直接运行 main 方法.
2.使用 javac 编译为class后运行,由于代码存在中文,java代码需要使用utf8格式保存,编译时通过-encoding utf8参数指定.
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持。如果你想了解更多相关内容请查看下面相关链接 。
原文链接:https://blog.csdn.net/isea533/article/details/77197017 。
最后此篇关于批量将现有Jar包上传到Maven私服的文章就讲到这里了,如果你想了解更多关于批量将现有Jar包上传到Maven私服的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
目录 私服仓库的配置 1、 下载sonatype Nexus来搭建私服 2 安装nexus服务 3、创建私服仓库 4、配
我是一名优秀的程序员,十分优秀!