Sky's blog

          我和我追逐的夢(mèng)

          導(dǎo)航

          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(8)

          隨筆分類(lèi)

          隨筆檔案

          閱讀排行榜

          評(píng)論排行榜

          常用鏈接

          統(tǒng)計(jì)

          其他鏈接

          友情鏈接

          最新評(píng)論

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

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

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

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

              類(lèi)似的建立java項(xiàng)目,文件結(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

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

              assembly plugin的使用方式比較簡(jiǎn)單,主要有:

          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是這個(gè)插件的標(biāo)準(zhǔn)命名,在maven2.0.*中帶的默認(rèn)版本是

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

              execution的設(shè)置是為了將maven-assembly-plugin繼承到標(biāo)準(zhǔn)的maven打包過(guò)程中,這樣在運(yùn)行maven-package時(shí)就會(huì)執(zhí)行maven-assembly-plugin的操作,從而實(shí)現(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>

             
              詳細(xì)的語(yǔ)法不介紹了,請(qǐng)參考官方指南,有非常詳盡的說(shuō)明:Assembly Descriptor Format reference

              簡(jiǎn)單解釋一下:

              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目錄下就會(huì)出現(xiàn)***.zip文件,里面的格式和要求的完全一致。

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

          posted on 2009-01-16 18:22 sky ao 閱讀(49319) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): project building

          評(píng)論

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

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

          主站蜘蛛池模板: 班玛县| 阿拉善左旗| 五指山市| 海兴县| 澄迈县| 乌兰县| 永宁县| 会泽县| 随州市| 梁平县| 汤阴县| 鹰潭市| 运城市| 新和县| 平乐县| 许昌县| 吉木乃县| 县级市| 马鞍山市| 乌鲁木齐市| 山丹县| 苏尼特左旗| 手机| 绍兴县| 金川县| 高台县| 衡水市| 青田县| 渝中区| 绵竹市| 巴林左旗| 旌德县| 广德县| 秭归县| 岑溪市| 龙游县| 乡宁县| 大兴区| 镇雄县| 栾川县| 江都市|