因為今天一個朋友學(xué)習(xí)過程中用到了maven項目編寫代碼,到了最后打包階段打的包不能運行,一時我也沒想起來具體操作步驟,后來我百度學(xué)習(xí)了一下,特此記錄下,以便后續(xù)自己查閱。
maven項目中不可避免的需要用到依賴jar,實際使用中有的能從maven倉庫找到,有的找不到,所以存在使用本地jar的情況,下面將對使用maven倉庫中jar,以及使用本地jar不同情況下打包可運行jar進行介紹。
情景一:使用maven依賴,所有的依賴都從maven倉庫查找下載,最終打包成可執(zhí)行jar,需要修改pom文件如下。
1
2
3
4
5
6
7
|
<!--使用maven依賴查找 json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version> 2.4 </version> <classifier>jdk15</classifier> </dependency> |
對應(yīng)修改pom文件,需指定程序入口,不然會報錯 xxxx-1.0-SNAPSHOT.jar中沒有主清單屬性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!--情景一,制定程序入口即可。--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version> 1.2 . 1 </version> <executions> <execution> <phase> package </phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation= "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > <mainClass>com.study.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> |
打包之后,即可通過 java -jar xxx.jar 運行,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.study; import net.sf.json.JSONObject; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); JSONObject json = new JSONObject(); json.put( "name" , "子落" ); System.out.println(json.toString()); } } |
運行效果如下,
通過jar可以看出,是將引用的jar中對應(yīng)的文件解壓編譯到打包的jar中
最終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
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.study</ groupId > < artifactId >mvnstudy</ artifactId > < version >1.0-SNAPSHOT</ version > < packaging >jar</ packaging > < name >mvnstudy</ name > < url >http://www.example.com</ url > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >1.7</ maven.compiler.source > < maven.compiler.target >1.7</ maven.compiler.target > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > <!--使用maven依賴查找 json-lib --> < dependency > < groupId >net.sf.json-lib</ groupId > < artifactId >json-lib</ artifactId > < version >2.4</ version > < classifier >jdk15</ classifier > </ dependency > </ dependencies > < build > < plugins > <!--情景一,制定程序入口即可。--> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-shade-plugin</ artifactId > < version >1.2.1</ version > < executions > < execution > < phase >package</ phase > < goals > < goal >shade</ goal > </ goals > < configuration > < transformers > < transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > < mainClass >com.study.App</ mainClass > </ transformer > </ transformers > </ configuration > </ execution > </ executions > </ plugin > </ plugins > </ build > </ project > |
情景二,本地jar文件及maven依賴混用時打包。
首先介紹下,接下來使用的 testJar-1.0.jar,這個為我隨便編寫的一個jar文件,模擬本地jar使用場景。
1
2
3
4
5
6
7
8
|
package com.company; public class Mp { public static void println(String str){ System.out.println( "Mp println = [" + str + "]" ); } } |
項目中使用
pom文件中引用
1
2
3
4
5
6
7
8
|
<!--使用本地jar--> <dependency> <groupId>testJar</groupId> <artifactId>junit</artifactId> <version> 1.0 </version> <scope>system</scope> <systemPath>${project.basedir}/lib/testJar- 1.0 .jar</systemPath> </dependency> |
App代碼修改,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.study; import com.company.Mp; import net.sf.json.JSONObject; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); JSONObject json = new JSONObject(); json.put( "name" , "子落" ); System.out.println(json.toString()); Mp.println( "子落." ); } } |
這里打包時采用將所有本地jar也一起打包到一個可執(zhí)行jar中,這樣可以直接通過 java - jar xxx.jar運行。也可以通過先將本地jar注冊到maven倉庫,然后再打包,或者將本地jar復(fù)制到lib文件夾,然后通過在Manifest文件class-path中進行引用,這里主要講,將所有依賴打包到一個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
|
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.study.App</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>com.jolira</groupId> <artifactId>onejar-maven-plugin</artifactId> <version> 1.4 . 4 </version> <executions> <execution> <configuration> <attachToBuild> true </attachToBuild> <classifier>onejar</classifier> </configuration> <goals> <goal>one-jar</goal> </goals> </execution> </executions> </plugin> |
最終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
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.study</ groupId > < artifactId >mvnstudy</ artifactId > < version >1.0-SNAPSHOT</ version > < packaging >jar</ packaging > < name >mvnstudy</ name > < url >http://www.example.com</ url > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >1.7</ maven.compiler.source > < maven.compiler.target >1.7</ maven.compiler.target > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > <!--使用maven依賴查找 json-lib --> < dependency > < groupId >net.sf.json-lib</ groupId > < artifactId >json-lib</ artifactId > < version >2.4</ version > < classifier >jdk15</ classifier > </ dependency > <!--使用本地jar--> < dependency > < groupId >testJar</ groupId > < artifactId >junit</ artifactId > < version >1.0</ version > < scope >system</ scope > < systemPath >${project.basedir}/lib/testJar-1.0.jar</ systemPath > </ dependency > </ dependencies > < build > < plugins > <!--情景一,制定程序入口即可。--> <!--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.study.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>--> <!--情景二,使打包的jar包含lib文件夾中的本地jar--> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-jar-plugin</ artifactId > < configuration > < archive > < manifest > < mainClass >com.study.App</ mainClass > </ manifest > </ archive > </ configuration > </ plugin > < plugin > < groupId >com.jolira</ groupId > < artifactId >onejar-maven-plugin</ artifactId > < version >1.4.4</ version > < executions > < execution > < configuration > < attachToBuild >true</ attachToBuild > < classifier >onejar</ classifier > </ configuration > < goals > < goal >one-jar</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > </ project > |
運行效果如下,
最終編譯的文件如下
其中 mvnstudy-1.0-SNAPSHOT.one-jar.jar 既可直接運行的jar,里面包括之前引用的本地 testJar-1.0.jar 如下圖,
可以看到以上兩種方式,最終編譯的jar內(nèi)部文件是不一樣的,第一種是將引用依賴中jar對應(yīng)的文件解壓,然后編譯到最終的jar中,第二種是將引用的jar文件直接拿過來放入lib文件夾中,然后以套一層殼的方式運行,并不會對原jar文件進行解壓,具體使用哪種方式,還要看實際使用情況。
以上這篇用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/01x2v3/p/9000292.html