1. 程式人生 > >使用Maven合併依賴jar包

使用Maven合併依賴jar包

如果你的專案使用maven構建的話,當專案要上線,部署到伺服器上去的時候或許會碰見這樣的問題。問題就是,伺服器上沒有maven的環境,也就是說,專案所依賴到的那些倉庫(repository)中的jar包你需要單獨提取出來上傳到伺服器中去。

     我知道pom型別如果是war的話,在使用mvn package 的命令就能自動將專案依賴的jar包打到web-inf 下的lib資料夾中。但是,如果pom型別為jar的話,當你呼叫mvn package命令,執行過程中不會將依賴的第三方包提取出來。

     以前,我的做法是,等到專案要上線的時候將pom型別改成war,然後執行一下mvn package命令,這樣先把所以依賴到的包提取出來,然後再把pom型別改成jar,這樣雖然能完成任務,但是,總感覺這樣的做法比較拙劣。

     所以最近尋找了一下有沒有更好的辦法,其實很簡單,就是使用 maven的 assembly外掛,在pom.xml 新增如下外掛:

  1. <project>
  2.   [...]  
  3.   <build>
  4.     [...]  
  5.     <plugins>
  6.       <plugin>
  7.         <!-- NOTE: We don't need a groupId specification because the group is  
  8.              org.apache.maven.plugins ...which is assumed by default.  
  9.          -->
  10.         <artifactId>maven-assembly-plugin</artifactId>
  11.         <version>2.2-beta-5</version>
  12.         <configuration>
  13.           <descriptorRefs>
  14.             <descriptorRef>jar-with-dependencies</descriptorRef>
  15.           </descriptorRefs>
  16.         </configuration>
  17.         [...]  
  18. </project>

然後在命令列上輸入:

mvn assembly:assembly

你會在${project}/target 資料夾下發現新生成的  {artifactId}-jar-with-dependencies.jar  這個檔案

在上面的這個 命令執行的過程中,maven會將jar包所依賴的包匯出,並且解壓(unpackage),一併放在

這個{artifactId}-jar-with-dependencies.jar 包中,這樣對於程式的部署人員來說很方便,哪怕你的專案依賴了再多的第三方包,在部署的時候都會合併到一個assembly中。

但是問題又來了,在部署的過程中我們往往還是希望,將各個第三方包單獨部署,比如ibatis歸ibatis包,spring包歸spring包,這樣以後單獨為某個第三方包升級也比較方便。

其實也有解決辦法:

之前使用

  1. <descriptorRefs>
  2.             <descriptorRef>jar-with-dependencies</descriptorRef>
  3.           </descriptorRefs>

這個jar-with-dependencies是assembly預先寫好的一個,組裝描述引用(assembly descriptor)我們來看一下這個定義這個組裝描述(assemly descriptor)的xml檔案

  1. <assemblyxmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  2.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  4.   <id>jar-with-dependencies</id>
  5.   <formats>
  6.     <format>jar</format>
  7.   </formats>
  8.   <includeBaseDirectory>false</includeBaseDirectory>
  9.   <dependencySets>
  10.     <dependencySet>
  11.       <unpack>true</unpack>
  12.       <scope>runtime</scope>
  13.     </dependencySet>
  14.   </dependencySets>
  15.   <fileSets>
  16.     <fileSet>
  17.       <directory>${project.build.outputDirectory}</directory>
  18.     </fileSet>
  19.   </fileSets>
  20. </assembly>

其實只要將上面這個xml檔案中的

  1. <dependencySet>
  2.       <unpack>true</unpack>
  3.       <scope>runtime</scope>
  4. </dependencySet>

改成:

  1. <dependencySet>
  2. <unpack>false</unpack>
  3. <scope>runtime</scope>
  4. </dependencySet>

在 main/assembly 下建立 src.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>jar-with-dependencies</id>
  6.     <formats>
  7.         <format>jar</format>
  8.     </formats>
  9.     <includeBaseDirectory>false</includeBaseDirectory>
  10.     <dependencySets>
  11.         <dependencySet>
  12.             <unpack>false</unpack>
  13.             <scope>runtime</scope>
  14.         </dependencySet>
  15.     </dependencySets>
  16.     <fileSets>
  17.         <fileSet>
  18.             <directory>${project.build.outputDirectory}</directory>
  19.         </fileSet>
  20.     </fileSets>
  21. </assembly>

將之前pom.xml 中的plugin改成如下:

  1. <plugin>
  2.                 <artifactId>maven-assembly-plugin</artifactId>
  3.                 <configuration>
  4.                     <descriptors>
  5.                         <descriptor>src/main/assembly/src.xml</descriptor>
  6.                     </descriptors>
  7.                 </configuration>
  8.             </plugin>

再執行

mvn assembly:assembly
這樣在target資料夾中就會看見新創建出來的{artifactId}-jar-with-dependencies.jar  這個jar包

裡面會將專案所依賴的所有第三方包全部打包在裡面,如下圖: