Sky's blog

          我和我追逐的夢

          常用鏈接

          統(tǒng)計

          其他鏈接

          友情鏈接

          最新評論

          初學(xué)maven(5)-使用assembly plugin實現(xiàn)自定義打包

              在上一篇文章中,討論到在對maven的機制不熟悉的情況下,為了實現(xiàn)自己需要的打包格式而使用maven ant task以maven + ant的方式來實現(xiàn)非標(biāo)準(zhǔn)打包,而現(xiàn)在要介紹的是maven中針對打包任務(wù)而提供的標(biāo)準(zhǔn)插件:assembly plugin。

              依然以上文(初學(xué)maven(4)-使用maven ant task實現(xiàn)非標(biāo)準(zhǔn)打包)的項目為例,要打包的程序如下:

              demo1
              |____lib
              |_____demo1.jar
              |_____*****.jar
              |_____*****.jar
              |____config
              |_____*****.properties
              |_____*****.xml
              |____log
              |_____*****.log
              |____run.bat
              |____run.sh

              類似的建立java項目,文件結(jié)構(gòu)如下:

              demo1
              |____src/main/java
              |____src/main/config
              |____src/main/bin
              |____src/main/resources
              |____src/main/assemble
              |____src/test/java
              |____src/test/resources
              |____target
              |____pom.xml

              除開增加了src/main/assemble目錄和沒有ant的build文件外,其他內(nèi)容完全一樣:其中src/main/java下放java代碼;src/main/resources下放一個*.properties文件,這個資源文件是打包到 jar中,內(nèi)容打包之后不需要改變的。src/main/config下放一個標(biāo)準(zhǔn)的log4j.xml,這個是有在安裝運行前臨時修改的需要的。src /main/bin下放置可執(zhí)行文件。

              assembly plugin的使用方式比較簡單,主要有:

          1. 修改pom.xml

              pom.xml中設(shè)置如下:
             
          <build>
                  
          <plugins>
                      
          <plugin>
                          
          <artifactId>maven-assembly-plugin</artifactId>
                          
          <configuration>
                              
          <!-- not append assembly id in release file name -->
                              
          <appendAssemblyId>false</appendAssemblyId>
                              
          <descriptors>
                                  
          <descriptor>src/main/assemble/package.xml</descriptor>
                              
          </descriptors>
                          
          </configuration>
                          
          <executions>
                              
          <execution>
                                  
          <id>make-assembly</id>
                                  
          <phase>package</phase>
                                  
          <goals>
                                     
          <goal>single</goal>
                                  
          </goals>
                              
          </execution>
                          
          </executions>
                      
          </plugin>
                  
          </plugins>
              
          </build>

              其中<artifactId>maven-assembly-plugin</artifactId>的maven-assembly-plugin是這個插件的標(biāo)準(zhǔn)命名,在maven2.0.*中帶的默認版本是

              appendAssemblyId屬性控制是否在生成的打包文件的文件名中包含assembly id。
             
              descriptor屬性指定maven-assembly-plugin的配置文件,當(dāng)然我設(shè)置的是src/main/assemble/package.xml.容許使用多個,功能強大當(dāng)然用法也復(fù)雜,對于簡單情況一個足矣。

              execution的設(shè)置是為了將maven-assembly-plugin繼承到標(biāo)準(zhǔn)的maven打包過程中,這樣在運行maven-package時就會執(zhí)行maven-assembly-plugin的操作,從而實現(xiàn)我們需要的自定義打包。
          2. assemble descriptor file

              我的src/main/assemble/package.xml內(nèi)容如下:

          <assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">
              
          <id>package</id>
              
          <formats>
                  
          <format>zip</format>
              
          </formats>
              
          <includeBaseDirectory>true</includeBaseDirectory>
              
          <fileSets>
                  
          <fileSet>
                      
          <directory>src/main/bin</directory>
                      
          <outputDirectory>/</outputDirectory>
                  
          </fileSet>
                  
          <fileSet>
                      
          <directory>src/main/config</directory>
                      
          <outputDirectory>config</outputDirectory>
                  
          </fileSet>
              
          </fileSets>
              
          <dependencySets>
                  
          <dependencySet>
                      
          <outputDirectory>lib</outputDirectory>
                      
          <scope>runtime</scope>
                  
          </dependencySet>
              
          </dependencySets>
          </assembly>

             
              詳細的語法不介紹了,請參考官方指南,有非常詳盡的說明:Assembly Descriptor Format reference

              簡單解釋一下:

              1) format
              format=zip設(shè)置打包的最終文件格式為zip.
              支持的其他格式還有g(shù)z,tar,tar.gz,tar.bz2。

              2)  fileset
             
              <fileSet>
                      
          <directory>src/main/bin</directory>
                      
          <outputDirectory>/</outputDirectory>
              
          </fileSet> 
           
              將src/main/bin目錄下的文件打包到根目錄(/)下.

          <fileSet>
                      
          <directory>src/main/config</directory>
                      
          <outputDirectory>config</outputDirectory>
          </fileSet>

              將src/main/config目錄下的文件打包到config下.

              3) dependencySets

              <dependencySet>
                      
          <outputDirectory>lib</outputDirectory>
                      
          <scope>runtime</scope>
             
          </dependencySet>

              將scope為runtime的依賴包打包到lib目錄下。


              總結(jié)一下,pom.xml中引入maven-assembly-plugin,然后assemble descriptor file按需設(shè)置,最后在eclipse中執(zhí)行Run As -> Maven package,在target目錄下就會出現(xiàn)***.zip文件,里面的格式和要求的完全一致。

              夠簡單明了吧?感覺比使用maven ant task要輕快不少,看來maven還是很強大的,繼續(xù)學(xué)習(xí)......

          posted on 2009-01-16 18:22 sky ao 閱讀(49321) 評論(1)  編輯  收藏 所屬分類: project building

          評論

          # re: 初學(xué)maven(5)-使用assembly plugin實現(xiàn)自定義打包 2016-04-25 09:14 李金龍

          按照你這種配置,執(zhí)行mvn compile,config里面的文件夾不會復(fù)制到classes里面,所以在測試的時候也要打包才行?  回復(fù)  更多評論   

          主站蜘蛛池模板: 阿克陶县| 通州市| 什邡市| 景宁| 个旧市| 永靖县| 余江县| 乡城县| 舟曲县| 双鸭山市| 三台县| 喀喇| 台中县| 勃利县| 福建省| 淮阳县| 应城市| 桂阳县| 新兴县| 庐江县| 绥棱县| 平凉市| 内江市| 嘉定区| 内丘县| 仲巴县| 潢川县| 绥中县| 柯坪县| 阳东县| 东光县| 渝中区| 巴林右旗| 亚东县| 平遥县| 虞城县| 汕尾市| 雷波县| 柳江县| 崇左市| 隆回县|