1. 程式人生 > >一個多maven專案聚合的例項

一個多maven專案聚合的例項

原文: http://my.oschina.net/xuqiang/blog/99854

本文介紹一個多maven專案的例項demo,展示了聚合、繼承、工程依賴、單元測試、多war聚合、cargo釋出等場景   一、工程介紹   該專案由5個maven專案組成      task-aggregator是父工程,同時承擔聚合模組和父模組的作用,沒有實際程式碼和資原始檔  task-common是基礎工程,裡面是公共的程式碼  task-sla是某一個業務子模組,不包含web內容  task-sla-web是某一個web子模組  task-web-dist是最外圍的web工程,聚合多個web工程,形成最終的war包   依賴關係是:task-common <-- task-sla <-- task-sla-web <-- task-web-dist   二、task-aggregator  

    這個工程是起到聚合作用,並充當parent pom,所以沒有任何實際程式碼和資原始檔。我這裡選擇了平行結構,另外一種方式是樹形結構,我個人感覺平行結構看起來更舒服一點   下面是pom,有所簡化: 

Xml程式碼            收藏程式碼  

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <!-- 定義公共變數 -->  
  4.     <properties>  
  5.         <spring.version>3.1.0.RELEASE</spring.version>  
  6.         <struts2.version>2.3.1</struts2.version>  
  7.         <hibernate.version>3.2.7.ga</hibernate.version>  
  8.     </properties>  
  9.     <modelVersion>4.0.0</modelVersion>  
  10.     <groupId>com.huawei.inoc.wfm.task</groupId>  
  11.     <artifactId>task-aggregator</artifactId>  
  12.     <version>0.0.1-SNAPSHOT</version>  
  13.     <packaging>pom</packaging>  
  14.         <!-- 待聚合模組 -->  
  15.     <modules>  
  16.         <module>../task-common</module>  
  17.         <module>../task-sla</module>  
  18.         <module>../task-sla-web</module>  
  19.         <module>../task-web-dist</module>  
  20.     </modules>  
  21.     <!-- 配置部署的遠端倉庫 -->  
  22.     <distributionManagement>  
  23.         <snapshotRepository>  
  24.             <id>nexus-snapshots</id>  
  25.             <name>nexus distribution snapshot repository</name>  
  26.             <url>http://10.78.68.122:9090/nexus-2.1.1/content/repositories/snapshots/</url>  
  27.         </snapshotRepository>  
  28.     </distributionManagement>  
  29.     <build>  
  30.         <pluginManagement>  
  31.             <plugins>  
  32.                 <plugin>  
  33.                     <groupId>org.apache.maven.plugins</groupId>  
  34.                     <artifactId>maven-resources-plugin</artifactId>  
  35.                     <version>2.6</version>  
  36.                     <configuration>  
  37.                         <encoding>UTF-8</encoding>  
  38.                     </configuration>  
  39.                 </plugin>  
  40.                 <plugin>  
  41.                     <groupId>org.apache.maven.plugins</groupId>  
  42.                     <artifactId>maven-compiler-plugin</artifactId>  
  43.                     <version>2.5.1</version>  
  44.                     <configuration>  
  45.                         <encoding>UTF-8</encoding>  
  46.                     </configuration>  
  47.                 </plugin>  
  48.             </plugins>  
  49.         </pluginManagement>  
  50.     </build>  
  51.     <dependencyManagement>  
  52.         <dependencies>  
  53.             <dependency>  
  54.                 <groupId>com.sun</groupId>  
  55.                 <artifactId>tools</artifactId>  
  56.                 <version>1.6.0</version>  
  57.                 <scope>system</scope>  
  58.                 <systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>  
  59.             </dependency>  
  60.         </dependencies>  
  61.     </dependencyManagement>  
  62. </project>  

基本上是一目瞭然,只是有幾點注意下:       1、這裡配置了<distributionManagement>,這樣子專案就不需要重複配置了       2、通過<pluginManagement>,對一些外掛進行了公共的配置,這裡主要是為了消除構建時的告警       3、配置tools,是因為實際中發現,其他開發人員從svn上check out工程以後,有的人會報錯,找不到tools.jar,這樣配置以後就好了   三、task-common   該工程是公共工程,提供了專案中的公共程式碼,這裡只包括了通用的DAO元件,作為示例。   該工程不依賴任何其他工程      該工程裡有幾點要點:       1、在程式碼內部用了Spring的註解 

Java程式碼            收藏程式碼  

  1. public abstract class GenericDAO<T> implements IGenericDAO<T> {  
  2.     private Class<T> entityClass;  
  3.     public GenericDAO(Class<T> clazz) {  
  4.         this.entityClass = clazz;  
  5.     }  
  6.     @Autowired  
  7.     private HibernateTemplate hibernateTemplate;  
  8. }  

這裡用到了@Autowired註解,所以最終形成的war包,必須在spring配置檔案中宣告HibernateTemplate型別的bean,否則會報錯   我這裡用的maven環境是maven3.0.4,這個版本打出的jar包,帶有Directory Entries資訊,所以spring的註解即使在jar包中也可生效,如果是比較老的版本,spring的註解在jar包中不好用,關於這個問題的詳細描述,見另外一篇部落格:http://kyfxbl.iteye.com/blog/1675368       2、單元測試的寫法 

Java程式碼            收藏程式碼  

  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = "classpath:spring-test.xml")  
  3. @Transactional  
  4. public class GenericDAOTest {  
  5.     @Autowired  
  6.     private IBookDAO bookDAO;  
  7.     @Test   
  8.     public void testInsert() {  
  9.         Book book = new Book();  
  10.         book.setName("thinking in java");  
  11.         book.setIsbn("111");  
  12.         bookDAO.insert(book);  
  13.     }  
  14. }  

這裡用到了幾個註解,@RunWith是為了在spring容器環境下跑這個單元測試類,以支援依賴注入。@ContextConfiguration是宣告spring配置檔案的位置。@Transactional註解之後,在單元測試方法中的事務會自動回滾,這個比較方便,這樣在前面執行的方法,不會對後面的方法造成影響   這個單元測試類,可以直接在maven裡跑起來,讓我比較驚喜。之前這樣寫,在ant裡跑沒有成功,可能是我沒有找到合適的外掛的原因       3、除了測試的java程式碼之外,還有3個資原始檔,都是放在src/test/resources下,這些資原始檔只在test階段生效,package階段不會被打包,也就是專門供測試階段使用   這個各有利弊,優點是測試的配置檔案與開發的配置檔案隔離,互不干擾。缺點是配置檔案似乎缺少了集中放置的地點,這樣如果多個maven工程都需要跑單元測試,要共享測試用配置檔案,比較麻煩一點   不過從我個人來看,也算是利大於弊。只是在每個maven專案下,都需要獨立的測試相關資原始檔,其實也有利於分別修改   另外,可以看到這裡的hibernate對映檔案,不是和model類放在一個package下,而是放在resources目錄下的,這樣做可以避免一些潛在的問題,也有利於後續的聚合       4、pom檔案沒有什麼特別的,只是要引入<scope>為test的junit和spring-test   四、task-sla   該工程依賴task-common(因為用到了GenericDAO),是某一個業務模組的邏輯部分,包含了資料庫訪問層和業務邏輯層,但是不包括web相關的部分      這裡沒有什麼特別要注意的,目錄結構和task-common基本一樣。比較特別的是可以看到Maven Dependencies裡,有一個task-common工程,所以task-common裡的任何修改,都可以第一時間在這個工程裡體現出來,是比較方便的   關於這個問題,見另外一篇部落格:http://kyfxbl.iteye.com/blog/1679806   另外就是前面說過的,hibernate的對映檔案,應該放在src/main/resources下,而不是與Model類放在一起   五、task-sla-web   這個工程是上述task-sla工程的web層,依賴於task-sla,由於task-sla又依賴task-common,所以這個工程最終會同時依賴task-common和task-sla      然後這個工程裡包含了web層的東西,包括Action類、jsp、圖片、struts2的配置檔案等,這些東西放在web工程裡是最合適的      這裡需要注意2點:       1、這個工程的packaging型別是war,而不是jar。但是最終它不會獨立打出war包來,其src/main/webapp裡的所有檔案,都會被最外圍的task-web-dist工程聚合成一個總的war       2、這個工程的WEB-INF目錄下,沒有web.xml(有也沒用,最終會被覆蓋)。預設情況下,packaging型別為war的專案,如果沒有web.xml,則構建會失敗,因此需要在pom裡做一個配置   該專案的pom如下,省略了依賴部分: 

Xml程式碼            收藏程式碼  

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <parent>  
  4.         <groupId>com.huawei.inoc.wfm.task</groupId>  
  5.         <artifactId>task-aggregator</artifactId>  
  6.         <version>0.0.1-SNAPSHOT</version>  
  7.         <relativePath>../task-aggregator</relativePath>  
  8.     </parent>  
  9.     <modelVersion>4.0.0</modelVersion>  
  10.     <artifactId>task-sla-web</artifactId>  
  11.     <packaging>war</packaging>  
  12.     <build>  
  13.         <plugins>  
  14.             <plugin>  
  15.                 <groupId>org.apache.maven.plugins</groupId>  
  16.                 <artifactId>maven-war-plugin</artifactId>  
  17.                 <configuration>  
  18.                     <failOnMissingWebXml>false</failOnMissingWebXml>  
  19.                 </configuration>  
  20.             </plugin>  
  21.         </plugins>  
  22.     </build>  
  23.     <!-- 配置依賴 -->  
  24.     <dependencies>  
  25.         <dependency>  
  26.             <groupId>org.springframework</groupId>  
  27.             <artifactId>spring-beans</artifactId>  
  28.         </dependency>  
  29.     </dependencies>  
  30. </project>  

上面的<failOnMissingWebXml>,就是配置缺少web.xml也不使構建失敗   六、task-web-dist   這個工程是最外圍的web工程,起到聚合的作用,即把所有的web專案,打成最終的war包。同時,在這個工程裡,放置裡公共的配置檔案,比如struts.xml、ssoconfig.properties等      這個工程的聚合意圖十分明顯,比如struts.xml 

    Xml程式碼            收藏程式碼  

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">  
  3. <struts>  
  4.     <constant name="struts.objectFactory" value="spring" />  
  5.     <constant name="struts.ui.theme" value="simple" />  
  6.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  7.     <constant name="struts.action.extension" value="action" />  
  8.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  9.     <constant name="struts.devMode" value="true" />  
  10.     <include file="struts2/struts-sla.xml" />  
  11. </struts>  

提供了專案通用的配置,並把各子專案的struts2配置檔案聚合起來。war包中的web.xml也是在這裡提供的   下面是該工程的pom,也省略了依賴的配置: 

Xml程式碼            收藏程式碼  

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <parent>  
  4.         <groupId>com.huawei.inoc.wfm.task</groupId>  
  5.         <artifactId>task-aggregator</artifactId>  
  6.         <version>0.0.1-SNAPSHOT</version>  
  7.         <relativePath>../task-aggregator</relativePath>  
  8.     </parent>  
  9.     <modelVersion>4.0.0</modelVersion>  
  10.     <artifactId>task-web-dist</artifactId>  
  11.     <packaging>war</packaging>  
  12.     <build>  
  13.         <finalName>task</finalName>  
  14.         <plugins>  
  15.             <!-- 合併多個war -->  
  16.             <plugin>  
  17.                 <groupId>org.apache.maven.plugins</groupId>  
  18.                 <artifactId>maven-war-plugin</artifactId>  
  19.                 <configuration>  
  20.                     <packagingExcludes>WEB-INF/web.xml</packagingExcludes>    
  21.                     <overlays>  
  22.                         <overlay>  
  23.                             <groupId>com.huawei.inoc.wfm.task</groupId>  
  24.                             <artifactId>task-sla-web</artifactId>  
  25.                         </overlay>  
  26.                     </overlays>  
  27.                 </configuration>  
  28.             </plugin>  
  29.             <!-- 利用cargo啟動容器 -->  
  30.             <plugin>  
  31.                 <groupId>org.codehaus.cargo</groupId>  
  32.                 <artifactId>cargo-maven2-plugin</artifactId>  
  33.                 <version>1.2.3</version>  
  34.                 <configuration>  
  35.                     <container>  
  36.                         <containerId>tomcat7x</containerId>  
  37.                         <home>D:\apache-tomcat-7.0.29</home>  
  38.                     </container>  
  39.                     <configuration>  
  40.                         <type>standalone</type>  
  41.                         <home>${project.build.directory}/tomcat7.0.29</home>  
  42.                         <properties>  
  43.                             <cargo.jvmargs>  
  44.                                 -Xdebug  
  45.                                 -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787  
  46.                             </cargo.jvmargs>  
  47.                         </properties>  
  48.                     </configuration>  
  49.                 </configuration>  
  50.                 <executions>  
  51.                     <execution>  
  52.                         <id>cargo-run</id>  
  53.                         <phase>pre-integration-test</phase>  
  54.                         <goals>  
  55.                             <goal>run</goal>  
  56.                         </goals>  
  57.                     </execution>  
  58.                 </executions>  
  59.             </plugin>  
  60.         </plugins>  
  61.     </build>  
  62. </project>  

這裡主要是對maven-war-plugin和cargo-maven2-plugin這2個外掛進行了配置,以起到聚合war,以及通過cargo啟動容器的作用  關於多war聚合,以及cargo,見另外2篇部落格:http://kyfxbl.iteye.com/blog/1678121、http://kyfxbl.iteye.com/blog/1677608   七、啟動構建   在task-aggregator目錄下,執行mvn clean deploy或者mvn clean install,就可啟動整個構建過程,並將容器啟動起來,跑最終生成的war包