Sky's blog

          我和我追逐的夢

          常用鏈接

          統(tǒng)計

          其他鏈接

          友情鏈接

          最新評論

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

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

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

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

              類似的建立java項目,文件結構如下:

              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文件外,其他內容完全一樣:其中src/main/java下放java代碼;src/main/resources下放一個*.properties文件,這個資源文件是打包到 jar中,內容打包之后不需要改變的。src/main/config下放一個標準的log4j.xml,這個是有在安裝運行前臨時修改的需要的。src /main/bin下放置可執(zhí)行文件。

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

          1. 修改pom.xml

              pom.xml中設置如下:
             
          <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是這個插件的標準命名,在maven2.0.*中帶的默認版本是

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

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

              我的src/main/assemble/package.xml內容如下:

          <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設置打包的最終文件格式為zip.
              支持的其他格式還有gz,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目錄下。


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

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

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

          評論

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

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

          主站蜘蛛池模板: 广州市| 寿光市| 鄢陵县| 青田县| 阿巴嘎旗| 宣武区| 广丰县| 和田县| 泗阳县| 长葛市| 手游| 乌兰察布市| 汉寿县| 思南县| 攀枝花市| 临漳县| 青铜峡市| 浑源县| 卓尼县| 平武县| 兴山县| 辽阳市| 涿鹿县| 通江县| 南乐县| 新竹县| 浠水县| 文水县| 江北区| 淮滨县| 皮山县| 宁海县| 化州市| 宜良县| 霍城县| 科技| 怀仁县| 巢湖市| 灌云县| 全南县| 安庆市|