1. 程式人生 > >Maven assembly實現自定義打包

Maven assembly實現自定義打包

maven-assembly-plugin : 是maven中針對打包任務而提供的標準外掛

(1)、在pom.xml 檔案裡面的配置說明

  1. <plugin>
  2.     <artifactId>maven-assembly-plugin</artifactId>
  3.     <executions><!--執行器 mvn assembly:assembly-->
  4.         <execution>
  5.             <id>make-zip</id><!--名字任意 -->
  6.         <phase>package</phase><!-- 繫結到package生命週期階段上 -->
  7.         <goals>
  8.            <goal>single</goal><!-- 只執行一次 -->
  9.         </goals>
  10.             <configuration>
  11.                      <descriptors><!--描述檔案路徑-->
  12.                           <descriptor>src/main/resources/zip.xml</descriptor>
  13.                     </
    descriptors>
  14.             </configuration>
  15.         </execution>
  16.     </executions>
  17.  </plugin>



(2)、zip.xml 檔案配置如下

  1. <assembly
  2.     xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation
    ="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  5.     <id>release</id>
  6.     <formats>
  7.         <format>zip</format>
  8.     </formats>
  9.     <fileSets>
  10.         <fileSet>
  11.             <directory>${project.basedir}\src\main\config</directory>
  12.             <!-- 過濾 -->
  13.             <excludes>
  14.                 <exclude>*.xml</exclude>
  15.             </excludes>
  16.             <outputDirectory>\</outputDirectory>
  17.         </fileSet>
  18.     </fileSets>
  19.     <dependencySets>
  20.         <dependencySet>
  21.             <useProjectArtifact>true</useProjectArtifact>
  22.             <outputDirectory>lib</outputDirectory><!-- 將scope為runtime的依賴包打包到lib目錄下。 -->
  23.             <scope>runtime</scope>
  24.         </dependencySet>
  25.     </dependencySets>
  26. </assembly>

(3)、zip.xml 格式屬性說明

打包的檔案格式
可以有:tar.zip war zip
<formats>
 <format>zip</format>
</formats>

需要打包的路徑
<directory>${project.basedir}</directory>

打包後輸出的路徑
<outputDirectory>/</outputDirectory>

打包需要包含的檔案

 <excludes>
        <exclude>junit:junit</exclude>
        <exclude>commons-lang:commons-lang</exclude>
        <exclude>commons-logging:commons-logging</exclude>
</excludes>

當前專案構件是否包含在這個依賴集合裡。

<useProjectArtifact>true</useProjectArtifact>

依賴包打包到目錄下
<dependencySets>
  <dependencySet>
   <outputDirectory>lib</outputDirectory><!-- 將scope為runtime的依賴包打包到lib目錄下。 -->
   <useProjectArtifact>true</useProjectArtifact>
   <scope>runtime</scope>
  </dependencySet>
</dependencySets>