eclipse上搭建maven多模組Java Web專案
1、模組化需求及專案模組說明
手頭上有個已上線的系統,但因老闆的特殊要求,系統需要不斷的修改。還有就是公司市場部不定期地在舉行一些微信活動,每一個活動都是週期性的,活動完了這個功能就要在系統中移除。
系統中就有三種模組:已經在系統中正常執行不需要再變更的模組、經常性變更的模組、用完就要移除的活動模組。
所以,我們需要把專案分成了下面幾個模組。
簡單說明一下:
- timetable-common是常用工具包存放的模組。
- wechat-api是微信介面模組,此模組用到了timetable-common包下的內容。
- timetable-system是系統管理相關的內容,主要是使用者相關的邏輯和介面,此模組用到了timetable-common及wechat-api。
- timetable-common、wechat-api、timetable-system都是不需要再變更的模組。
- timetable-main是一個web模組,也是整個專案的入口。主模組用到了timetable-system、timetable-common、wechat-api模組。
- 每一次微信活動都可以作為一個web模組,如timetable-activity,活動中可能會用到其它模組(除timetable-main以外)的內容。在系統釋出的時候,可以將活動模組引入到主模組中一起釋出。當活動結束後,我們可以快速地將活動模組移除。
2、準備工作
eclipse版本:
Eclipse Java EE IDE for Web Developers.
Version : Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
由於涉及到maven專案在eclipse上進行tomcat的整合除錯,如果除錯異常的朋友可以考慮更換版本比較高的eclipse版本。
3、模組化過程
3.1 主專案設定
maven多模組專案需要一個主專案來聚合各個子模組,不過其實就是需要一個父pom.xml。
這個pom.xml主要有兩個內容:
- packaging方式為pom。所有帶有子模組的專案的packaging都為pom。packaging如果不進行配置,它的預設值是jar,代表Maven會將專案打成一個jar包。
<packaging>pom</packaging>
- 使用module標籤引入各個子模組。如果通過Maven build app-parent的時候,它會根據子模組的相互依賴關係整理一個build順序,然後依次build。其實此配置不需要手動輸寫,通過eclipse建立maven module時,eclipse會自動加入到此配置檔案中。
<modules>
<module>timetable-common</module>
<module>wechat-api</module>
<module>timetable-system</module>
<module>timetable-activity</module>
<module>timetable-main</module>
</modules>
完整的timetable/pom.xml為:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<packaging>pom</packaging>
<version>2.0.0</version>
<name>timetable</name>
<url>http://maven.apache.org</url>
<properties>
<project.version>2.0.0</project.version>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>timetable-common</module>
<module>wechat-api</module>
<module>timetable-activity</module>
<module>timetable-main</module>
<module>timetable-system</module>
</modules>
</project>
3.2 獨立的maven簡單模組
通過eclipse,新建Maven Module.
新建一個模組名為timetable-common的simple project。
建立完成後的模組類似一個簡單的java專案:
timetable-common/pom.xml:
<?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.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-common</artifactId>
<name>timetable-common</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 設定變數:用於統一管理Library的版本 -->
<javax.version>7.0</javax.version>
<javax.servlet.version>3.1.0</javax.servlet.version>
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.16</log4j.version>
<org.aspectj.version>1.7.3</org.aspectj.version>
<net.sf.ehcache.version>2.3.2</net.sf.ehcache.version>
<org.slf4j.version>1.6.1</org.slf4j.version>
<mysql.jdbc.version>5.1.29</mysql.jdbc.version>
<org.hibernate.entitymanager.version>3.6.3.Final</org.hibernate.entitymanager.version>
<org.springframework.version>4.1.1.RELEASE</org.springframework.version>
<org.codehaus.jackson.version>1.7.4</org.codehaus.jackson.version>
<net.sf.json-lib.version>2.4</net.sf.json-lib.version>
<proxool.version>0.8.3</proxool.version>
<javamail.version>1.4.1</javamail.version>
<commons-codec.version>1.9</commons-codec.version>
<commons-io.version>1.3.2</commons-io.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<xstream.version>1.4.3</xstream.version>
<org.apache.jcs.version>1.3</org.apache.jcs.version>
<joda-time.version>2.3</joda-time.version>
<org.apache.poi.version>3.8</org.apache.poi.version>
<com.fasterxml.jackson.core.version>2.3.3</com.fasterxml.jackson.core.version>
<commons-httpclient.version>3.1</commons-httpclient.version>
<jaxen.version>1.1.6</jaxen.version>
<com.drewnoakes.version>2.8.1</com.drewnoakes.version>
<com.google.zxing.version>3.0.0</com.google.zxing.version>
<io.netty.version>5.0.0.Alpha2</io.netty.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>${javax.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${org.hibernate.entitymanager.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${net.sf.ehcache.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${javax.servlet.jstl.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj.version}</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.jdbc.version}</version>
</dependency>
<!-- proxool -->
<dependency>
<groupId>proxool</groupId>
<artifactId>proxool</artifactId>
<version>${proxool.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<!-- spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<!-- joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${net.sf.json-lib.version}</version>
<classifier>jdk15</classifier>
</dependency>
<!-- mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javamail.version}</version>
</dependency>
<!-- Jcscache -->
<dependency>
<groupId>org.apache.jcs</groupId>
<artifactId>jcs</artifactId>
<version>${org.apache.jcs.version}</version>
</dependency>
<!-- commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!-- 微信介面使用到 -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>${xstream.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>
<!-- commons-httpclient 支付寶介面使用 -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${commons-httpclient.version}</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>${jaxen.version}</version>
</dependency>
<!-- 獲取圖片資訊 -->
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>${com.drewnoakes.version}</version>
</dependency>
<!-- google二維碼工具 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${com.google.zxing.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${com.google.zxing.version}</version>
</dependency>
<!-- netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${io.netty.version}</version>
</dependency>
</dependencies>
</project>
配置比較簡單:
- timetable-common模組繼承了timetable父模組,因此這個pom的一開始就聲明瞭對timetable的引用,該引用是通過Maven座標GAV實現的。而關於專案timetable-common本身,它卻沒有宣告完整GAV,這裡我們只看到了artifactId。這個pom並沒有錯,groupId和version預設從父模組繼承了。實際上子模組從父模組繼承一切東西,包括依賴,外掛配置等等。
<parent>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-common</artifactId>
<name>timetable-common</name>
<url>http://maven.apache.org</url>
- 如果packaging不寫,那麼預設值是jar。我們也可以寫上此標籤。
<packaging>jar</packaging>
- 比較特殊的是timetable-common模組是專案中最底層的模組,其它的模組都會依賴到此模組的內容。所以我在這裡把專案中用到的所以依賴都放在這裡配置。你也可以在使用到某些依賴的時候再在當前模組裡配置。但統一配置的好處是能夠方便的檢視到專案中用到的依賴。當然,如果是隻用於用完就要移除的活動模組中的依賴還是在本身的pom中配置。因為這些依賴隨著模組的移除也用不上了。
3.3 依賴其它模組的maven簡單模組
使用a中相同的方法新建wechat-api模組。
wechat-api/pom.xml:
<?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.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>wechat-api</artifactId>
<name>wechat-api</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
- wechat-api模組依賴timetable-common模組的內容
<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
timetable-system模組的新建與模組與wechat-api模組的方式與配置雷同。只是timetable-system模組依賴的是wechat-api模組。當然,通過依賴wechat-api,同時也依賴了timetable-common。
3.4 依賴其它模組的maven web模組
與使用eclipse建立一個獨立的maven web project一樣,建立一個maven web module。
注意:我們不是建立簡單的模組
通過Filter過濾後,選擇maven-archetype-webapp。
建立完成後的模組類似一個的java web專案:
在此模組下,我們和正常的java web開發一下,在src/main/java下寫java程式碼,src/main/resources下放配置檔案,src/main/webapp下寫web相關的程式碼及資源。
timetable-activity/pom.xml:
<?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.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-activity</artifactId>
<packaging>war</packaging>
<name>timetable-activity Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-system</artifactId>
<