基本信息
適用環境:內網環境下的 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 是基礎的命令部分。
- 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
|
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( "上傳失?。?quot; + pom.getabsolutepath()); } } catch (ioexception e) { error( "上傳失?。?quot; + pom.getabsolutepath()); e.printstacktrace(); } catch (interruptedexception e) { error( "上傳失?。?quot; + 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( "上傳失?。?quot; + pom.getabsolutepath()); e.printstacktrace(); } catch (interruptedexception e) { error( "上傳失?。?quot; + 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( "上傳失?。?quot; + pom.getabsolutepath()); } } catch (ioexception e) { error( "上傳失?。?quot; + pom.getabsolutepath()); e.printstacktrace(); } catch (interruptedexception e) { error( "上傳失?。?quot; + 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( "上傳失?。?quot; + pom.getabsolutepath()); } } catch (ioexception e) { error( "上傳失敗:" + pom.getabsolutepath()); e.printstacktrace(); } catch (interruptedexception e) { error( "上傳失?。?quot; + pom.getabsolutepath()); e.printstacktrace(); } } }); } } |
使用方式
1.導入項目直接運行 main 方法。
2.使用 javac 編譯為class后運行,由于代碼存在中文,java代碼需要使用utf8格式保存,編譯時通過-encoding utf8參數指定。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/isea533/article/details/77197017