使用maven-assembly-plugin將第三方依賴jar包 打包入jar

1、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.  (maven-assembly-plugin 主頁html

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

Goal Description:apache

assembly:single Assemble an application bundle or distribution from an assembly descriptor. This goal is suitable either for binding to the lifecycle or calling directly from the command line (provided all required files are available before the build starts, or are produced by another goal specified before this one on the command line).

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

簡單的說,maven-assembly-plugin 就是用來幫助打包用的,好比說打出一個什麼類型的包,包裏包括哪些內容等等。maven

目前至少支持如下打包類型:ide

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

默認狀況下,打jar包時,只有在類路徑上的文件資源會被打包到jar中,而且文件名是${artifactId}-${version}.jarui

2、怎麼用maven-assembly-plugin插件來定製化打包

 

1.首先須要添加插件聲明:this

<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 Descriptorspa

要使用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 Descriptor 和 Assembly Descriptor 。插件

默認狀況下,maven-assembly-plugin內置了幾個能夠用的assembly descriptor:

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

例如:使用 其默認jar-with-dependencies的配置 只需在pom的plugin配置中添加

<configuration>  
       <descriptorRefs>  
           <descriptorRef>jar-with-dependencies</descriptorRef>  
       </descriptorRefs>  
  </configuration>

要查看它們的詳細定義,能夠到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>

 

3.自定義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>

 

4.添加文件

上面演示了添加全部編譯後的資源,一樣的能夠增長其餘資源,例如想添加當前工程目錄下的某個文件 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>

 

5.排除文件

在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>

 

6.添加依賴

若是想把一些依賴庫打到包裏,能夠用 dependencySets 元素,例如最簡單的,把當前工程的全部依賴都添加到包裏:

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

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

若是要排除工程自身生成的jar,則能夠添加:

<useProjectArtifact>false</useProjectArtifact>

unpack參數能夠控制依賴包是否在打包進來時是否解開,例如解開全部包,添加如下配置:

<unpack>true</unpack>

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

7.其餘選項

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

 

3、 Assembly Plugin更多配置

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

下面介紹一些archive的用法。

1.指定Main-Class

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

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

 

2.添加MANIFEST項

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

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

 

3.指定MANIFEST.MF文件

還能夠直接指定MANIFEST.MF文件。以下:

<archive>  
    <manifestFile>META-INF/MANIFEST.MF</manifestFile>  
</archive>  
相關文章
相關標籤/搜索