這是最後一篇,做一個多模塊依賴的正式例子,簡單的講一下單元測試、覆蓋率以及發佈和站點的部署。只想講講和Maven相關的東西,因此,注重看pom文件,裏面具體的代碼就不實現了,下面是我項目骨架:
首先創建一個MavenProject,命名爲demo-parent,此爲父模塊,重要的是如下兩步:
所選的的模板是quickstart
pom文件中的配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.tgb.demo</groupId>
- <artifactId>demo-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>pom</packaging>
- <name>demo-parent</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <springframework.version>3.1.1.RELEASE</springframework.version>
- </properties>
- <modules>
- <module>demo-core</module>
- <module>demo-persistent</module>
- <module>demo-service</module>
- <module>demo-web</module>
- </modules>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactid>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <configuration>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
- </project>
後面我們會一一講解上面這個pom文件,接下來,繼續創建MavenProject或者MavenModule,Module在創建時需要選擇父模塊,而Project創建完成之後需要在pom中添加依賴的父模塊,沒什麼區別,將此模塊命名爲demo-core,之後依次創建demo-persistent、demo-service、demo-web,值得注意的是,創建demo-web時選擇的模板必須是webapp,demo-web的pom配置如下,其餘的都和demo-web差不多,讀者可仿照配置:
- <?xml version="1.0"?>
- <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"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.tgb.demo</groupId>
- <artifactId>demo-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.tgb.demo</groupId>
- <artifactId>demo-web</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>demo-web Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>student-service</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- </dependency>
- </dependencies>
- <build>
- <finalName>demo-web</finalName>
- <plugins>
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <!-- 使用的是jetty-maven-plugin的插件 -->
- <artifactId>jetty-maven-plugin</artifactId>
- <configuration>
- <scanIntervalSeconds>2000</scanIntervalSeconds>
- <webApp>
- <!-- 上下文路徑 -->
- <contextPath>/stu</contextPath>
- </webApp>
- <connectors>
- <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
- <!-- 端口號 -->
- <port>8787</port>
- <maxIdleTime>500000</maxIdleTime>
- </connector>
- </connectors>
- </configuration>
- </plugin>
- <!--
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>tomcat-maven-plugin</artifactId>
- <version>1.1</version>
- <configuration>
- <port>8080</port>
- <contextFile>src/main/resources/context.xml</contextFile>
- </configuration>
- </plugin> -->
- </plugins>
- </build>
- </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