【Maven學習】maven-assembly-plugin的使用

轉自http://liugang594.iteye.com/blog/2093607

maven-assembly-plugin使用描述(拷自 maven-assembly-plugin 主頁)

The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.

目前它只有一個有意義的goal, 詳細的請看(http://maven.apache.org/plugins/maven-assembly-plugin/plugin-info.html):

assembly:single

single操作有很多可配置的參數,詳細的請看(http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html)。

簡單的說,maven-assembly-plugin 就是用來幫助打包用的,比如說打出一個什么類型的包,包里包括哪些內容等等。
目前至少支持以下打包類型:

  • zip
  • tar
  • tar.gz
  • tar.bz2
  • jar
  • dir
  • war

默認情況下,打jar包時,只有在類路徑上的文件資源會被打包到jar中,并且文件名是${artifactId}-${version}.jar,下面看看怎么用maven-assembly-plugin插件來定制化打包。

首先需要添加插件聲明:

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-assembly-plugin</artifactId>  
    <version>2.4</version>  
    <executions>  
        <execution>  
            <phase>package</phase>  
            <goals>  
                <goal>single</goal>  
            </goals>  
        </execution>  
    </executions>  
</plugin>  

使用內置的Assembly Descriptor

要使用maven-assembly-plugin,需要指定至少一個要使用的assembly descriptor 文件,對于當前使用的版本(2.4)對應的assembly descriptor的schema定義為:Assembly Schema ,其中assembly descriptor中又可以包括 component 的定義 (component 可以很方便的用于多個assembly descriptor之間共享),component 的schema 定義在:Component Schema 。 關于assembly descriptor的component descriptor的更詳細的說明,請見:Component DescriptorAssembly Descriptor

默認情況下,maven-assembly-plugin內置了幾個可以用的assembly descriptor:

  • bin : 類似于默認打包,會將bin目錄下的文件打到包中
  • jar-with-dependencies : 會將所有依賴都解壓打包到生成物中
  • src :只將源碼目錄下的文件打包
  • project : 將整個project資源打包

要查看它們的詳細定義,可以到maven-assembly-plugin-2.4.jar里去看,例如對應 bin 的assembly descriptor 如下:

<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>bin</id>  
  <formats>  
    <format>tar.gz</format>  
    <format>tar.bz2</format>  
    <format>zip</format>  
  </formats>  
  <fileSets>  
    <fileSet>  
      <directory>${project.basedir}</directory>  
      <outputDirectory>/</outputDirectory>  
      <includes>  
        <include>README*</include>  
        <include>LICENSE*</include>  
        <include>NOTICE*</include>  
      </includes>  
    </fileSet>  
    <fileSet>  
      <directory>${project.build.directory}</directory>  
      <outputDirectory>/</outputDirectory>  
      <includes>  
        <include>*.jar</include>  
      </includes>  
    </fileSet>  
    <fileSet>  
      <directory>${project.build.directory}/site</directory>  
      <outputDirectory>docs</outputDirectory>  
    </fileSet>  
  </fileSets>  
</assembly>

自定義Assembly Descriptor

一般來說,內置的assembly descriptor都不滿足需求,這個時候就需要寫自己的assembly descriptor的實現了。先從一個最簡單的定義開始:

<?xml version='1.0' encoding='UTF-8'?>  
<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>demo</id>  
    <formats>  
        <format>jar</format>  
    </formats>  
    <includeBaseDirectory>false</includeBaseDirectory>  
    <fileSets>  
        <fileSet>  
            <directory>${project.build.directory}/classes</directory>  
            <outputDirectory>/</outputDirectory>  
        </fileSet>  
    </fileSets>  
</assembly>

這個定義很簡單:

  • format:指定打包類型
  • includeBaseDirectory:指定是否包含打包層目錄(比如finalName是output,當值為true,所有文件被放在output目錄下,否則直接放在包的根目錄下)
  • fileSets:指定要包含的文件集,可以定義多個fileSet
  • directory:指定要包含的目錄
  • outputDirectory:指定當前要包含的目錄的目的地
    要使用這個assembly descriptor,需要如下配置:
<configuration>  
    <finalName>demo</finalName>  
    <descriptors>  
        <descriptor>assemblies/demo.xml</descriptor>  
    </descriptors>  
    <outputDirectory>output</outputDirectory>  
</configuration> 

最后會生成一個demo-demo.jar 文件在目錄 output 下,其中前一個demo來自finalName,后一個demo來自assembly descriptor中的id,其中的內容和默認的打包出來的jar類似。

如果只想有finalName,則增加配置:

<appendAssemblyId>false</appendAssemblyId>  

添加文件

上面演示了添加所有編譯后的資源,同樣的可以增加其他資源,例如想添加當前工程目錄下的某個文件 b.txt ,在assembly descriptor的assembly結點下增加

<files>  
    <file>  
        <source>b.txt</source>  
        <outputDirectory>/</outputDirectory>  
    </file>  
</files>

這里用到了 files 元素類型,可以想象 fileSets 下的結點都是針對文件夾的;files 下的結點都是針對文件的。

也可以改變打包后的文件名,例如上面的 b.txt ,希望打包后的名字為 b.txt.bak, 只需要在file 里添加以下配置 :

<destName>b.txt.bak</destName>  

排除文件

在fileSet里可以使用includes 和 excludes來更精確的控制哪些文件要添加,哪些文件要排除。
例如要排除某個目錄下所有的txt文件:

<fileSet>  
    <directory>${project.build.directory}/classes</directory>  
    <outputDirectory>/</outputDirectory>  
    <excludes>  
        <exclude>**/*.txt</exclude>  
    </excludes>  
</fileSet>

或者某個目錄下只想 .class 文件:

<fileSet>  
    <directory>${project.build.directory}/classes</directory>  
    <outputDirectory>/</outputDirectory>  
    <includes>  
        <include>**/*.class</include>  
    </includes>  
</fileSet>

添加依賴

如果想把一些依賴庫打到包里,可以用 dependencySets 元素,例如最簡單的,把當前工程的所有依賴都添加到包里:

<dependencySets>  
    <dependencySet>  
        <outputDirectory>/</outputDirectory>  
    </dependencySet>  
</dependencySets>

在assembly下添加以上配置,則當前工程的依賴和工程本身生成的jar都會被打包進來。

如果要排除工程自身生成的jar,則可以添加

<useProjectArtifact>false</useProjectArtifact>

unpack參數可以控制依賴包是否在打包進來時是否解開,例如解開所有包,添加以下配置:

<unpack>true</unpack>  

和 fileSet 一樣,可以使用 excludes 和 includes 來更詳細的控制哪些依賴需要打包進來;另外 useProjectAttachments,useTransitiveDependencies,useTransitiveFiltering等參數可以對間接依賴、傳遞依賴進行控制。

其他選項

  • moduleSets:當有子模塊時候用
  • repositories:想包含庫的時候用
  • containerDescriptorHandlers:可以進行一些合并,定義ArtifactHandler之類的時候可以用,(可以參考:說明
  • componentDescriptors:如上所述,可以包含一些componentDescriptor定義,這些定義可以被多個assembly共享

Assembly Plugin更多配置

上面已經看到了一些Assembly Plugin本身的配置,例如 finalName, outputDirectory, appendAssemblyId和descriptors等,除了這些還有其他的一些可配置參數,參見:single,其中某些參數會覆蓋在assembly descriptor中的參數。有一個比較有用的參數是: archive,它的詳細配置在:archive
下面介紹一些archive的用法。

指定Main-Class

archive的一個重要用處就是配置生成的MANIFEST.MF文件。默認會生成一個MANIFEST.MF文件,不過這個文件默認值沒什么意義。如果想指定生成jar的Main-Class,可以如下配置:

<archive>  
    <manifest>  
        <mainClass>demo.DemoMain</mainClass>  
    </manifest>  
</archive>

添加MANIFEST項

除了可以指定Main-Class外,還可以添加任意項。比如在OSGI bundle的MANIFEST.MF定義里就有很多用來定義bundle的屬性的項,如Import-Package,Export-Package等等。要添加項,可以使用如下配置:

<archive>  
    <manifestEntries>  
        <Import-Package>javax.xml.ws.*</Import-Package>  
    </manifestEntries>  
</archive> 

指定MANIFEST.MF文件

還可以直接指定MANIFEST.MF文件。如下:

<archive>  
    <manifestFile>META-INF/MANIFEST.MF</manifestFile>  
</archive>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,237評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,957評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,248評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,356評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,081評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,485評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,534評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,720評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,263評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,025評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,204評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,787評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,461評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,874評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,105評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,945評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,205評論 2 375

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,806評論 18 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,900評論 6 342
  • 使用指導 如何添加外部依賴jar包 在Maven工程中添加依賴jar包,很簡單,只要在POM文件中引入對應的<de...
    靜默虛空閱讀 2,814評論 0 13
  • 轉自:http://www.cnblogs.com/crazy-fox/archive/2012/02/09/23...
    晴天哥_王志閱讀 2,259評論 2 27
  • 我們都知道Maven本質上是一個插件框架,它的核心并不執行任何具體的構建任務,所有這些任務都交給插件來完成,例如編...
    付鵬丶閱讀 1,637評論 0 15