1. 創(chuàng)建一個根模塊
Paste_Image.png
打包方式必須為pom京革,這樣代表一個根模塊
Paste_Image.png
創(chuàng)建完畢后刪除根模塊的src目錄庸毫,因為沒有必要
2. 創(chuàng)建子模塊
根模塊右建->maven model
Paste_Image.png
這里不使用archetype骨架創(chuàng)建項目
Paste_Image.png
子模塊要打包成jar包供其他模塊使用
Paste_Image.png
同理創(chuàng)建:helloweb-entity helloweb-dao
3. 創(chuàng)建web模塊
- 不使用architype骨架來創(chuàng)建web項目
Paste_Image.png
Paste_Image.png
如果不使用architype骨架的話挡爵,那么需要手動添加內(nèi)容到webapp下
Paste_Image.png
-
使用architype骨架來創(chuàng)建web項目
Paste_Image.png
Paste_Image.png
4. pom配置
創(chuàng)建完成后,結(jié)構(gòu)如下
Paste_Image.png
根模塊的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxjqr</groupId>
<artifactId>helloweb-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 跟子模塊的關(guān)聯(lián) -->
<modules>
<module>helloweb-core</module>
<module>helloweb-entity</module>
<module>helloweb-dao</module>
<module>helloweb-web</module>
</modules>
<!-- 用dependencyManagement來管理依賴版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>servlet</artifactId>
<version>4.1.36</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Maven 使用dependencyManagement 元素來提供了一種管理依賴版本號的方式零酪。通常會在一個組織或者項目的最頂層的父POM
中看到dependencyManagement 元素。使用pom.xml 中的dependencyManagement 元素能讓
所有在子項目中引用一個依賴而不用顯式的列出版本號拇勃。Maven 會沿著父子層次向上走四苇,直到找到一個擁有dependencyManagement
元素的項目,然后它就會使用在這個dependencyManagement 元素中指定的版本號方咆。
這樣做的好處就是:如果有多個子項目都引用同一樣依賴月腋,則可以避免在每個使用的子項目里都聲明一個版本號,這樣當(dāng)想升級或切換到另一個版本時瓣赂,只需要在頂層父容器里更新榆骚,而不需要一個一個子項目的修改
;另外如果某個子項目需要另外的一個版本钩述,只需要聲明version就可寨躁。
dependencyManagement里只是聲明依賴,并不實現(xiàn)引入牙勘,因此子項目需要顯式的聲明需要用的依賴职恳。 -->
</project>
entity子模塊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>
<!-- 跟根模塊的關(guān)聯(lián) -->
<parent>
<groupId>com.xxjqr</groupId>
<artifactId>helloweb-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>helloweb-entity</artifactId>
<!-- 為什么子模塊中不用寫<groupId></groupId>和<version></version>?
因為子模塊的pom會繼承自根模塊的pom,而根模塊的pom中已經(jīng)寫了<groupId>和<version>
且這些在所以的模塊中都是一樣的方面,所以就不用在子模塊中寫了
-->
</project>
web子模塊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>
<!-- 跟根模塊的關(guān)聯(lián) -->
<parent>
<groupId>com.xxjqr</groupId>
<artifactId>helloweb-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.xxjqr</groupId>
<artifactId>helloweb-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>helloweb-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 依賴管理(不再需要指定版本號和域放钦;除非我們想覆蓋)-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>servlet</artifactId>
</dependency>
</dependencies>
<build>
<finalName>helloweb-web</finalName>
</build>
</project>
5. 最后操作
選中全部模塊,右鍵->maven->update project
選中web模塊部署到tomcat中運行