maven插件maven-assembly-plugin打包歸納文件zip/tar使用
前言
java項目運行的文件需要jar或者war格式,同時還需要使用Java命令。同時還可能包含一些外部的資源文件。因此要運行一個Java項目,需要多個文件配合才能啟動。因此再發(fā)布的時候,我們可以使用maven-assembly-plugin插件,將我們項目需要的所有資源打包成一個zip或者gz的壓縮包,這樣我們發(fā)布出去的項目就是一整套運行環(huán)境,我們常用的一些中間件,比如Tomcat、zookeeper等都這樣發(fā)布。下面給大家詳細(xì)介紹通過maven-assembly-plugin來制作自己的發(fā)布包。
一、使用方式
和其他插件一樣,要使用maven-assembly-plugin插件,我們在pom.xml中進(jìn)行如下配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.4.2</version> <configuration> <descriptors> <descriptor>assembly/package.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
二、assembly配置文件詳解
在上面的配置中descriptor標(biāo)簽引用了一個package.xml文件,這個文件也是assembly插件的解析文件,package.xml的內(nèi)容如下面所示:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>win64.vm-${build.version}</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>plugins</directory> <outputDirectory>\plugins</outputDirectory> <includes> <include>plugin.test.dll</include> </includes> </fileSet> <fileSet> <directory>prod/jvm</directory> <outputDirectory>\</outputDirectory> <includes> <include>ServiceUpdate.bat</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>\</outputDirectory> <includes> <include>RaspberryAgent.exe</include> </includes> </fileSet> </fileSets> </assembly>
下面對assembly配置進(jìn)行詳細(xì)說明
1.id
id標(biāo)簽可以指定生成的歸納文件名稱,但如果我們在插件的configuration標(biāo)簽里面加入:
<configuration> <appendAssemblyId>false</appendAssemblyId> <finalName>${build.finalName}</finalName> <descriptors> <descriptor>assembly/package.xml</descriptor> </descriptors> </configuration>
這樣ID標(biāo)簽就無效,生成的歸納文件名稱由finalName來指定,當(dāng)我們?nèi)サ鬭ppendAssemblyId和finalName,那么生成的文件就由id標(biāo)簽來指定,同時descriptors標(biāo)簽下可以添加多個配置文件,因此我們可以在一次構(gòu)建時打包成多個歸納文件,這個時候就需要使用ID來指定歸納文件的名稱。
2.formats
指定歸納文件的格式,支持的格式包含:
- zip
- tar
- tar.gz (or tgz)
- tar.bz2 (or tbz2)
- tar.snappy
- tar.xz (or txz)
- tar.zst (or tzst)
- jar
- dir
- war
3.includeBaseDirectory
表示生成的歸納文件是否包含根目錄,如果包含解壓時會生成和文件名相同的根目錄
4.fileSet
這里是需要將哪些資源文件打包到歸納文件里面,具體使用如下:
<fileSet> <directory>plugins</directory> <outputDirectory>\plugins</outputDirectory> <includes> <include>*.dll</include> </includes> </fileSet>
directory
表示源文件目錄
outputDirectory
表示打包到歸納文件后的目錄
includes、excludes
過濾作用,includes可以指定需要的文件。excludes可以排除不需要的文件。可以使用正則表達(dá)式來進(jìn)行篩選配置。
5.dependencySets
這個配置可以將所有的jar文件提取出來,常見配置如下:
<dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> <excludes> <exclude> ${groupId}:${artifactId} </exclude> </excludes> </dependencySet> </dependencySets>
這樣會在歸納文件中生成一個lib目錄,里面包含了項目所依賴的所有jar文件。
useProjectArtifact
是否把本項目添加到依賴文件夾下。
outputDirectory
提取的jar依賴存放的文件夾,當(dāng)打包成歸納文件時,歸納文件就會生成一個lib的文件夾
excludes
表示要排除的依賴,上面的配置我們是排除了本項目的,這樣就不會把本項目的jar文件打入到lib目錄下,當(dāng)然我們也可以不配置excludes,而配置false也能達(dá)到同樣的效果。如果要排除其他的內(nèi)庫,配置格式: g r o u p I d : {groupId}: groupId:{artifactId}。如下配置:
<dependencySets> <dependencySet> .... <excludes> <exclude>org.apache.commons:commons-lang3</exclude> <exclude>org.apache.logging.log4j:log4j-1.2-api</exclude> </excludes> </dependencySet> .... </dependencySets>
這樣就會把commons-lang3和log4j-1.2-api的依賴去掉
三、使用assembly打包jar文件
在assembly的配置中可以選擇jar和war格式的文件。因此assembly也可以用來進(jìn)行jar打包,能實現(xiàn)maven-jar-plugin插件的功能。
如果我們只打包一個內(nèi)庫的jar文件,可以添加如下配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.4.2</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
另外我還可以使用assembly打包一個可執(zhí)行的jar文件,配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.4.2</version> <configuration> <archive> <manifest> <mainClass>com.example.testmvnpkgexespringboot.TestMvnPkgExeSpringbootApplication</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
注意這兩種配置方式都會將項目依賴的所有依賴都會打包到這個jar文件中,我們可以解壓打包的jar文件,內(nèi)容如下:
同樣,如果我要打包可執(zhí)行的jar文件時,manifest的配置和maven-jar-plugin的配置一樣,大家可以參照我的上一篇文章【maven插件maven-jar-plugin構(gòu)建jar文件詳細(xì)使用】
總結(jié)
使用assembly來打包可執(zhí)行的jar文件,目前貌似只能用于普通的Java項目,如果要打包springboot項目還有一些問題,如果要定制化springboot項目的jar文件,還是需要借助maven-jar-plugin插件,有興趣的同學(xué)可參照我的另外一篇文章:【使用maven對springboot項目進(jìn)行瘦身分離jar的多種處理方案】
到此這篇關(guān)于maven插件maven-assembly-plugin打包歸納文件zip/tar使用的文章就介紹到這了,更多相關(guān)maven-assembly-plugin打包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Cloud?Eureka基礎(chǔ)應(yīng)用及原理
這篇文章主要介紹了Spring?Cloud?Eureka基礎(chǔ)應(yīng)用,Eureka?Client中內(nèi)置一個負(fù)載均衡器,用來進(jìn)行基本的負(fù)載均衡,下面我們將通過搭建一個簡單的Eureka例子來了解Eureka的運作原理,感興趣的朋友一起看看吧2022-05-05MyBatis的通俗理解:SqlSession.getMapper()源碼解讀
這篇文章主要介紹了MyBatis的通俗理解:SqlSession.getMapper()源碼解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java發(fā)送form-data請求實現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Java發(fā)送form-data請求實現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06基于servlet實現(xiàn)統(tǒng)計網(wǎng)頁訪問次數(shù)
這篇文章主要為大家詳細(xì)介紹了基于servlet實現(xiàn)統(tǒng)計網(wǎng)頁訪問次數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Java Scanner的使用和hasNextXXX()的用法說明
這篇文章主要介紹了Java Scanner的使用和hasNextXXX()的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10slf4j與jul、log4j1、log4j2、logback的集成原理
這篇文章主要介紹了slf4j與jul、log4j1、log4j2、logback的集成原理,以及通用日志框架與具體日志實現(xiàn)系統(tǒng)的機(jī)制機(jī)制介紹,包括依賴的jar包,jar沖突處理等2022-03-03