Maven--多模塊依賴實例解析(五)

Maven--搭建開發環境(一)

Maven--構建企業級倉庫(二)

Maven—幾個需要補充的問題(三)

Maven—生命週期和插件(四)

Maven--多模塊依賴實例解析(五)

 


這是最後一篇,做一個多模塊依賴的正式例子,簡單的講一下單元測試、覆蓋率以及發佈和站點的部署。只想講講和Maven相關的東西,因此,注重看pom文件,裏面具體的代碼就不實現了,下面是我項目骨架:


首先創建一個MavenProject,命名爲demo-parent,此爲父模塊,重要的是如下兩步:


所選的的模板是quickstart


pom文件中的配置如下:

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project 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/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.      <groupId>com.tgb.demo</groupId>  
  6.      <artifactId>demo-parent</artifactId>  
  7.      <version>0.0.1-SNAPSHOT</version>  
  8.      <packaging>pom</packaging>  
  9.       
  10.      <name>demo-parent</name>  
  11.      <url>http://maven.apache.org</url>  
  12.   
  13.      <properties>  
  14.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.        <springframework.version>3.1.1.RELEASE</springframework.version>  
  16.      </properties>  
  17.   
  18.      <modules>  
  19.         <module>demo-core</module>  
  20.         <module>demo-persistent</module>  
  21.         <module>demo-service</module>  
  22.        <module>demo-web</module>  
  23.      </modules>  
  24.     
  25.     <dependencyManagement>  
  26.         <dependencies>  
  27.             <dependency>  
  28.                 <groupId>org.springframework</groupId>  
  29.                 <artifactId>spring-context-support</artifactId>  
  30.                 <version>${springframework.version}</version>  
  31.             </dependency>  
  32.             <dependency>  
  33.                 <groupId>junit</groupId>  
  34.                 <artifactid>junit</artifactId>  
  35.                 <version>4.8.2</version>  
  36.                 <scope>test</scope>  
  37.              </dependency>  
  38.         </dependencies>  
  39.      </dependencyManagement>  
  40.     
  41.     <build>  
  42.         <pluginManagement>  
  43.             <plugins>  
  44.                 <plugin>  
  45.                     <groupId>org.apache.maven.plugins</groupId>  
  46.                     <artifactId>maven-compiler-plugin</artifactId>  
  47.                     <version>2.3.2</version>  
  48.                     <configuration>  
  49.                       <source>1.5</source>  
  50.                       <target>1.5</target>  
  51.                       <encoding>UTF-8</encoding>  
  52.                     </configuration>  
  53.                 </plugin>  
  54.                   
  55.                 <plugin>    
  56.                    <groupId>org.apache.maven.plugins</groupId>    
  57.                    <artifactId>maven-resources-plugin</artifactId>    
  58.                    <configuration>    
  59.                           <encoding>UTF-8</encoding>    
  60.                    </configuration>    
  61.                 </plugin>  
  62.   
  63.             </plugins>  
  64.         </pluginManagement>  
  65.     </build>  
  66. </project>  

後面我們會一一講解上面這個pom文件,接下來,繼續創建MavenProject或者MavenModule,Module在創建時需要選擇父模塊,而Project創建完成之後需要在pom中添加依賴的父模塊,沒什麼區別,將此模塊命名爲demo-core,之後依次創建demo-persistent、demo-service、demo-web,值得注意的是,創建demo-web時選擇的模板必須是webapp,demo-web的pom配置如下,其餘的都和demo-web差不多,讀者可仿照配置:

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0"?>  
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  4.   <modelVersion>4.0.0</modelVersion>  
  5.   <parent>  
  6.     <groupId>com.tgb.demo</groupId>  
  7.     <artifactId>demo-parent</artifactId>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.   </parent>  
  10.   <groupId>com.tgb.demo</groupId>  
  11.   <artifactId>demo-web</artifactId>  
  12.   <version>0.0.1-SNAPSHOT</version>  
  13.   <packaging>war</packaging>  
  14.   <name>demo-web Maven Webapp</name>  
  15.   <url>http://maven.apache.org</url>  
  16.   
  17.   <dependencies>  
  18.         <dependency>  
  19.             <groupId>${project.groupId}</groupId>  
  20.             <artifactId>student-service</artifactId>  
  21.             <version>0.0.1-SNAPSHOT</version>  
  22.         </dependency>  
  23.         <dependency>  
  24.             <groupId>org.springframework</groupId>  
  25.             <artifactId>spring-webmvc</artifactId>  
  26.         </dependency>  
  27.     </dependencies>  
  28.     <build>  
  29.         <finalName>demo-web</finalName>  
  30.         <plugins>  
  31.             <plugin>  
  32.                 <groupId>org.mortbay.jetty</groupId>  
  33.                 <!-- 使用的是jetty-maven-plugin的插件 -->  
  34.                 <artifactId>jetty-maven-plugin</artifactId>  
  35.                 <configuration>  
  36.                     <scanIntervalSeconds>2000</scanIntervalSeconds>  
  37.                     <webApp>  
  38.                     <!-- 上下文路徑 -->  
  39.                         <contextPath>/stu</contextPath>  
  40.                     </webApp>  
  41.                     <connectors>  
  42.                         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
  43.                         <!-- 端口號 -->  
  44.                             <port>8787</port>  
  45.                             <maxIdleTime>500000</maxIdleTime>  
  46.                         </connector>  
  47.                     </connectors>  
  48.                 </configuration>  
  49.             </plugin>  
  50.             <!--   
  51.             <plugin>  
  52.                 <groupId>org.codehaus.mojo</groupId>  
  53.                 <artifactId>tomcat-maven-plugin</artifactId>  
  54.                 <version>1.1</version>  
  55.                 <configuration>  
  56.                       
  57.                     <port>8080</port>  
  58.                     <contextFile>src/main/resources/context.xml</contextFile>  
  59.                       
  60.                 </configuration>  
  61.             </plugin> -->  
  62.         </plugins>  
  63.     </build>  
  64. </project>  

OK,幾個模板創建完成後,我想你的模塊有可能在報錯,那麼接下來需要設置編譯、發佈等等,我廢話不多說,右擊你的模塊,然後打開Properties窗口,按照下面的圖依次設置:

選擇javabuild path:


再到javacompiler,把編譯jdk給改了:


現在來設置WTP:



以後設置wtp編譯目錄:



到此爲止我們的大概環境算是配置完成了,那麼接下來就需要分析很重要的pom文件了,一些基礎的東西我不會講,大家可上網查,先來分析demo-parent的pom文件,在demo-parent的pom值得注意的有以下幾點:

1.            <packaging>pom</packaging>,必須改成pom,不是jar,也不是war,因爲他是parent,大家可以想想接口的作用;

2.            在<modules></modules>必須要配上你所有相關的模塊,當eclipse導入項目時,就是根據這個標籤把相關的模塊一起導入進來的;

3.            消除重複,在<properties></properties>中,配置了<springframework.version>3.1.1.RELEASE</springframework.version>,下面依賴配置中就可以直接引用此配置了,如果將來改變版本,既改此一處;

4.            消除多模塊依賴配置重複,我們知道依賴是可以繼承的,既父模塊中定義的依賴,子模塊可自動繼承,但是有些子模塊需要,有些不需要的情況下該怎麼辦呢?那就必須用<dependencyManagement>標籤了,子模塊的pom中就可以 <dependency><groupId>junit</groupId><artifactid>junit</artifactId> </dependency>,這樣引用,沒有引用的就不會自動繼承;

5.            消除多模塊插件配置重複,與dependencyManagement類似的,我們也可以使用pluginManagement元素管理插件。一個常見的用法就是我們希望項目所有模塊的使用MavenCompiler Plugin的時候,都使用Java 1.6,以及指定Java源文件編碼爲UTF-8,如父模塊的POM中的配置;這段配置會被應用到所有子模塊的maven-compiler-plugin中,由於Maven內置了maven-compiler-plugin與生命週期的綁定,因此子模塊就不再需要任何maven-compiler-plugin的配置了。與依賴配置不同的是,通常所有項目對於任意一個依賴的配置都應該是統一的,但插件卻不是這樣,例如你可以希望模塊A運行所有單元測試,模塊B要跳過一些測試,這時就需要配置maven-surefire-plugin來實現,那樣兩個模塊的插件配置就不一致了。這也就是說,簡單的把插件配置提取到父POM的pluginManagement中往往不適合所有情況,那我們在使用的時候就需要注意了,只有那些普適的插件配置才應該使用pluginManagement提取到父POM中。

完成了環境的配置,下面就是單元測試、覆蓋率報告和站點的發佈了,其實這些都是對插件的應用,單元測試可以用maven-surefire-plugin,覆蓋率Maven自帶了一個插件,叫做cobertura,可以執行目標cobertura:cobertura,web的發佈,有jetty和tomcat插件,具體用法我已經在上面給出來了,站點的發佈有maven-project-info-reports-plugin插件,使用mvnsite可以完成站點的生成,具體用法如下


還有很多插件,三天三夜都說不完,掌握再多也是徒勞,極爲重要的是,自己需要什麼隨時去查,會查,然後能迅速掌握插件的用法,關於Maven到此結束了,這篇寫的很粗糙,大家有問題隨時留言或郵件。

關於本文實例源碼弄丟了,因此上傳了一個別人寫的例子,很全面,可參考:http://download.csdn.net/detail/stubbornpotatoes/7071803

發佈了16 篇原創文章 · 獲贊 13 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章