目的:爲了在開發過程當中更清晰、更有效地複用模塊進行開發子系統web
使用maven將模塊工程化開發,將每一個模塊建立爲一個maven工程spring
(缺點,模塊過多,管理起來麻煩)apache
如今將系統分爲三個模塊/工程:編程
系統主工程模塊:project (編程模塊)tomcat
系統工具類模塊:util (引入所須要的工具類)mybatis
技術架構模塊:springmybatis(不進行編程,只須要在pom.xml中引入項目框架所依賴的jar包)架構
說明:主工程projec依賴於工具模塊util和技術架構模塊springmybatiapp
工程聚合:(將各個工程聚合起來打包成war包,方便測試)框架
parent工程(不進行編碼)eclipse
①在parent工程的pom.xml中配置(聚合工程)
<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> <!--聚合工程,將子工程聚合起來,方便打包成war包時進行測試 --> <groupId>yycg</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <!-- 設定子模塊的目錄 ,目錄要有pom.xml,使用相對路徑 --> <module>../project</module> <module>../util</module> <module>../springmybatis</module> </modules> </project>
②在project、util和springmybatis工程的pom.xml中都配置如下代碼
<!--繼承父模塊(聚合工程) --> <parent> <!-- 相對路徑 --> <relativePath>../parent</relativePath> <groupId>yycg</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
③在主工程project的pom.xml中配置插件
<!-- war包生成插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> </configuration> </plugin> <!-- tomcat運行插件 --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.2.3</version> <configuration> <container> <containerId>tomcat7x</containerId> <home>D:\tomcat_install</home> <!--本身的tomcat安裝路徑--> </container> <configuration> <type>existing</type> <home>D:\tomcat_install</home> </configuration> </configuration> <executions> <execution> <id>cargo-run</id> <phase>install</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
④在cmd命令行執行工程聚合,達到一步構建(編譯、運行、打包war、啓動tomcat)
一、 cmd進入parent工程目錄(例:cd /d D:\myeclipse\文件保存處\parent)
二、在parent工程目錄下運行mvn install
三、運行mvn install以後 ,檢查本地倉庫,是否三個工程jar發佈到本地倉庫