1.配置pom.xml文件,添加build節點
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
|
< build > <!-- 輸出的包名 --> < finalName >p2p</ finalName > < sourceDirectory >src/main/java</ sourceDirectory > < resources > <!-- 控制資源文件的拷貝(默認復制到classes目錄,最后打進jar包) --> < resource > < directory >src/main/resources</ directory > < includes > < include >**/*.properties</ include > < include >**/*.xml</ include > </ includes > <!-- 排除外置的配置文件(運行時注釋上使IDE能讀取到配置文件;打包時放開注釋讓配置文件外置方便修改) --> < excludes > < exclude >config.properties</ exclude > </ excludes > </ resource > <!-- 配置文件外置的資源(存放到config目錄,也是classpath路徑,下面會配置) --> < resource > < directory >src/main/resources</ directory > < includes > < include >config.properties</ include > </ includes > < targetPath >${project.build.directory}/config</ targetPath > </ resource > </ resources > < plugins > <!-- 設置編譯版本 --> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-compiler-plugin</ artifactId > < version >3.1</ version > < configuration > < source >1.7</ source > < target >1.7</ target > < encoding >UTF-8</ encoding > </ configuration > </ plugin > <!-- 打包jar文件時,配置manifest文件,加入lib包的jar依賴 --> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-jar-plugin</ artifactId > < configuration > < archive > <!-- 清單文件,設置入口類和classpath --> < manifest > < mainClass >com.hdwang.Application</ mainClass > < addClasspath >true</ addClasspath > < classpathPrefix >lib/</ classpathPrefix > </ manifest > <!-- 給清單文件添加鍵值對,增加classpath路徑,這里將config目錄也設置為classpath路徑 --> < manifestEntries > < Class-Path >config/</ Class-Path > </ manifestEntries > </ archive > < classesDirectory > </ classesDirectory > </ configuration > </ plugin > <!-- 拷貝依賴的jar包到lib目錄 --> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-dependency-plugin</ artifactId > < executions > < execution > < id >copy</ id > < phase >package</ phase > < goals > < goal >copy-dependencies</ goal > </ goals > < configuration > < outputDirectory > ${project.build.directory}/lib </ outputDirectory > </ configuration > </ execution > </ executions > </ plugin > <!-- 解決資源文件的編碼問題 --> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-resources-plugin</ artifactId > < version >2.5</ version > < configuration > < encoding >UTF-8</ encoding > </ configuration > </ plugin > <!-- 自定義打zip包 --> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-assembly-plugin</ artifactId > < version >2.2.1</ version > < configuration > < descriptors > < descriptor >src/main/assembly/assembly.xml</ descriptor > </ descriptors > </ configuration > < executions > < execution > < id >make-assembly</ id > < phase >package</ phase > < goals > < goal >single</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > |
這個pom配置文件中注意紅色字體部分,這是實現配置文件外置的關鍵配置,思路就是配置文件不打進jar包,放置到外面,且將此文件夾設置為classpath,這樣子程序便可以通過根據classloader很方便地讀取到配置文件了。下面給出讀取配置文件的java代碼,在IDE運行時和打包后,代碼都不用修改,因為配置文件總能從classpath路徑中找到!!!
工具包的maven信息
1
2
3
4
5
|
< dependency > < groupId >commons-configuration</ groupId > < artifactId >commons-configuration</ artifactId > < version >1.10</ version > </ dependency > |
2.新建maven-assembly-plugin插件的配置文件assembly.xml,內容如下
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
|
< assembly > < id >bin</ id > < formats > < format >zip</ format > </ formats > <!-- 使用assembly拷貝依賴包 --> <!--<dependencySets>--> <!--<dependencySet>--> <!--<!– 是否包含自己(將項目生成的jar包也輸出到lib目錄) –>--> <!--<useProjectArtifact>false</useProjectArtifact>--> <!--<outputDirectory>lib</outputDirectory>--> <!--</dependencySet>--> <!--</dependencySets>--> < fileSets > <!-- 從目標目錄拷貝文件去壓縮 --> < fileSet > < directory >target</ directory > < includes > < include >*.jar</ include > </ includes > < outputDirectory >/</ outputDirectory > </ fileSet > < fileSet > < directory >target/lib</ directory > < outputDirectory >/lib</ outputDirectory > </ fileSet > < fileSet > < directory >target/config</ directory > < outputDirectory >/config</ outputDirectory > </ fileSet > <!-- 從源目錄拷貝文件去壓縮 --> < fileSet > < directory >src/main/run</ directory > < includes > < include >*.sh</ include > < include >*.cmd</ include > </ includes > < outputDirectory >/</ outputDirectory > </ fileSet > < fileSet > < directory >src/main</ directory > < includes > < include >ReadMe.txt</ include > </ includes > < outputDirectory >/</ outputDirectory > </ fileSet > </ fileSets > </ assembly > |
這個插件在package生命周期中運行,執行mvn package或者mvn install便可觸發此插件的執行。這里我注釋掉了拷貝依賴包的代碼,是因為在pom.xml文件中已經配置了maven-dependency-plugin執行這樣的操作,無須重復配置。fileSet可以配置需要拷貝壓縮的文件,directory路徑相對于項目根目錄,outputDirectory路徑相對于輸出目錄target,includes可以對拷貝的文件進行篩選。這里可以拷貝壓縮輸出目錄的文件,應該就是因為此插件運行在程序編譯打包之后,這樣子就達到了我們自定義打包的要求:編譯->拷貝資源文件->項目打包->拷貝依賴的jar包-> assembly進行拷貝壓縮。然后使用打出的zip包就可以去部署發布了,解壓后就能運行。
3.程序打包流程示意圖
以上這篇java application maven項目打自定義zip包實例(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。