1. pom 中添加assembly 插件
要使用assembly 進項編譯打包, 首先主要在pom 中的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
|
< build > < finalName >${project.artifactId}</ finalName > < sourceDirectory >src/main/java</ sourceDirectory > < resources > < resource > < directory >src/main/resources</ directory > < filtering >true</ filtering > < includes > < include >**/*.xml</ include > < include >**/*.properties</ include > </ includes > </ resource > < resource > < directory >${profile.dir}</ directory > < filtering >true</ filtering > </ resource > </ resources > < plugins > <!-- compiler插件參數設置,指定編碼 --> < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >3.1</ version > < configuration > < source >1.8</ source > < target >1.8</ target > < encoding >utf-8</ encoding > </ configuration > </ plugin > <!-- 這個插件是關鍵 --> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-assembly-plugin</ artifactId > < configuration > <!-- 這個是assembly 所在位置 --> < descriptor >src/main/assembly/assembly.xml</ descriptor > </ configuration > < executions > < execution > < id >make-assembly</ id > < phase >package</ phase > < goals > < goal >single</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > |
2. 創建assembly文件夾和assembly.xml文件
創建assembly文件夾和assembly.xml文件, 這個樣子創建主要是規范。
在pom 中已經介紹assembly.xml 位置。
1
2
|
<!-- 這個是assembly 所在位置 --> < descriptor >src/main/assembly/assembly.xml</ descriptor > |
創建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
|
< assembly > < formats > <!--支持 zip,tar,tar.gz,tar.bz2,jar,dir,war 等 --> < format >tar.gz</ format > < format >zip</ format > < format >dir</ format > </ formats > < includeBaseDirectory >false</ includeBaseDirectory > < fileSets > < fileSet > < directory >src/main/resources</ directory > < outputDirectory >conf</ outputDirectory > < fileMode >0644</ fileMode > </ fileSet > < fileSet > < directory >${profile.dir}</ directory > < outputDirectory >conf</ outputDirectory > <!-- 表示的是包含下面格式的資源文件 --> < includes > < include >*.xml</ include > < include >*.properties</ include > < include >**/*.xml</ include > < include >**/*.properties</ include > </ includes > < fileMode >0644</ fileMode > </ fileSet > < fileSet > < directory >src/main/assembly/bin</ directory > < outputDirectory >bin</ outputDirectory > < fileMode >0755</ fileMode > </ fileSet > </ fileSets > < dependencySets > < dependencySet > < outputDirectory >lib</ outputDirectory > </ dependencySet > </ dependencySets > </ assembly > |
fileMode 官方解釋:
Similar to a UNIX permission, sets the file mode of the files included. THIS IS AN OCTAL VALUE. Format: (User)(Group)(Other) where each component is a sum of Read = 4, Write = 2, and Execute = 1. For example, the value 0644 translates to User read-write, Group and Other
上述的三個fileSet 分別是將resource 下的資源打包到config 目錄下, 將assembly下的bin 啟動相關腳本打包到bin 目錄下, 將maven項目依賴的所有jar 包, 打包到lib 中。
具體結構如下圖所示:
參考地址:
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
zip.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
|
< assembly xmlns = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd" > < id >release</ id > < formats > < format >zip</ format > </ formats > < fileSets > < fileSet > < directory >${project.basedir}\src\main\config</ directory > <!-- 過濾 --> < excludes > < exclude >*.xml</ exclude > </ excludes > < outputDirectory >\</ outputDirectory > </ fileSet > </ fileSets > < dependencySets > < dependencySet > < useProjectArtifact >true</ useProjectArtifact > < outputDirectory >lib</ outputDirectory > <!-- 將scope為runtime的依賴包打包到lib目錄下。 --> < scope >runtime</ scope > </ dependencySet > </ dependencySets > </ assembly > |
例:
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
|
< assembly > < id >assembly</ id > < formats > < format >tar.gz</ format > </ formats > < includeBaseDirectory >true</ includeBaseDirectory > < fileSets > < fileSet > < directory >${project.build.directory}/resources</ directory > < outputDirectory >resources</ outputDirectory > < fileMode >0755</ fileMode > </ fileSet > < fileSet > < directory >${project.build.directory}/config</ directory > < outputDirectory >config</ outputDirectory > < fileMode >0644</ fileMode > </ fileSet > < fileSet > < directory >${project.build.directory}/bin</ directory > < outputDirectory >bin</ outputDirectory > < fileMode >0755</ fileMode > </ fileSet > </ fileSets > < dependencySets > < dependencySet > < outputDirectory >lib</ outputDirectory > </ dependencySet > </ dependencySets > </ assembly > |
到此這篇關于maven 使用assembly 進行打包的方法的文章就介紹到這了,更多相關maven assembly打包內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/zhongzunfa/article/details/82465939