Maven項目管理工具 (五) 分模塊構建工程

l 分析

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-jpHJOQaR-1593164108662)(../img-folder/Maven/1576484075873.png)]

l 分模塊開發細節注意

注意:表現層工程web.xml去加載Spring applicationContext開頭的配置文件時,我們要使用classpath*

l 理解繼承和聚合

通常繼承和聚合同時使用

父工程的特徵:packing: pom , (jar/war)

n 何爲繼承?

繼承是爲了消除重複,如果將dao、service、web分開創建獨立的工程,則每個工程的pom.xml文件中內容存在重複,比如設置編譯版本、鎖定spring的版本等,可以將這些重複的內容提取出放在父工程的pom.xml中定義

l 何爲聚合?

項目開發通常分模塊/分工程開發,每個模塊開發完成要運行整個工程需要將每個模塊聚合在一起,比如:dao、service、web三個工程最終打成一個war包運行

l 依賴範圍對傳遞依賴的影響(瞭解)

service層依賴於dao層,有一個scope(runtime)

假如dao層依賴於一個jar包A,也有一個scope(test)配置

service能否使用A這個jar?看兩個scope的值

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-lchcjrnG-1593164108674)(../img-folder/Maven/1576484108114.png)]

(一)搭建分模塊工程

基於上邊的三個工程分析

繼承:創建一個 parent 工程將所需的依賴都配置在 pom 中

聚合:聚合多個模塊運行。

1.需求

需求描述——將 SSM 工程拆分爲多個模塊開發:

ssm_dao ssm_service ssm_web

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-qWRiKZRH-1593164108681)(../img-folder/Maven/wps56.jpg)]

2.創建父工程maven-parent

(1)選擇骨架創建父工程

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-uJbcZaDW-1593164108690)(../img-folder/Maven/wps57.jpg)]

注意代碼所在的路徑

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-3CO34WSv-1593164108693)(../img-folder/Maven/wps58.jpg)]

添加項目的打包方式

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-nXGR926y-1593164108695)(../img-folder/Maven/wps59.jpg)]

定義 pom.xml

在父工程的 pom.xml 中抽取一些重複的配置的,比如:鎖定 jar 包的版本、設置編譯版本等。

<properties>

<spring.version>5.0.2.RELEASE</spring.version>

<springmvc.version>5.0.4.RELEASE</springmvc.version> <mybatis.version>3.4.5</mybatis.version>

</properties>



<dependencyManagement>

<dependencies>

<!-- Mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version> </dependency>

<!-- springMVC -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springmvc.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version>

</dependency>

<!-- spring -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId><artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId> <version>${spring.version}</version>

</dependency>



<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version>

</dependency>



<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>
</dependencies>

</dependencyManagement>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<target>1.8</target>

<source>1.8</source>

</configuration>

</plugin>

</plugins>

</build>

將父工程發佈至倉庫

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-U47sfKgl-1593164108696)(../img-folder/Maven/wps60.jpg)]

(2)ssm_dao 子模塊

在父工程上右擊創建 maven 模塊:

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-UfRvvL7q-1593164108698)(../img-folder/Maven/wps61.jpg)]

選擇“跳過骨架選擇”

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-RKm1jvSA-1593164108699)(../img-folder/Maven/wps62.jpg)]

填寫模塊名稱

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-uSMGfeDh-1593164108701)(../img-folder/Maven/wps63.jpg)]

下一步,確定項目的目錄

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-h1vm0adw-1593164108703)(../img-folder/Maven/wps64.jpg)]

添加打包方式是 jar

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Dv0i6obK-1593164108704)(../img-folder/Maven/wps65.jpg)]

定義 pom.xml

<dependencies>

<!-- Mybatis 和 mybatis 與 spring 的整合 --> <dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId> <version>1.3.1</version>

</dependency>

<!-- MySql 驅動 -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId> <version>5.1.32</version>

</dependency>
<!-- druid 數據庫連接池 -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.0.9</version>

</dependency>

<!-- spring 相關 -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>

</dependency>

</dependencies>

只添加到層的 pom,mybatis 和 spring 的整合相關依賴

dao 模塊代碼編寫(省略)

把 dao 模塊 install 到本地倉庫(省略)

(3)ssm_service 子模塊(參考dao)

定義 pom.xml

ssm_service 模塊的 pom.xml 文件中需要繼承父模塊,ssm_service 依賴 ssm_dao 模塊,添加spring 相關的依賴:

<dependencies>

<!-- spring 相關 -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>

</dependency>

<!--	dao 層的依賴 -->

<dependency>

<groupId>com.mavenTest.ssm_mavenTestt</groupId>

<artifactId>ssm_dao</artifactId>

<version>1.0-SNAPSHOT</version></dependency>

</dependencies>

把 service 模塊 install 到本地倉庫(省略)

(4)ssm_web 子模塊

選擇骨架創建 web 子模塊

|      |                                       || ---- | ------------------------------------- ||      | [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-5WFA3rMX-1593164108706)(../img-folder/Maven/wps66.jpg)] |

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-38YBCPGj-1593164108708)(../img-folder/Maven/wps67.jpg)]

定義模塊名稱,下一步確認使用自己的本地倉庫

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-qZSs41oD-1593164108709)(../img-folder/Maven/wps68.jpg)]

使用骨架創建 web 項目會花費些時間,請耐心等待

創建 java 和 resources 文件夾,轉成 source root

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-rfdxdkXz-1593164108711)(../img-folder/Maven/wps69.jpg)]

定義 pom.xml

ssm_web 模塊的 pom.xml 文件中需要繼承父模塊,ssm_web 依賴 ssm_service 模塊,和springmvc 的依賴

<dependencies>

<!-- 依賴 service -->

<dependency>

<groupId>com.mavenTest.ssm_mavenTestt</groupId>

<artifactId>ssm_service</artifactId> <version>1.0-SNAPSHOT</version>

</dependency>

<!-- springMVC -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springmvc.version}</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>
<version>2.0</version>

<scope>provided</scope>

</dependency>

<!-- jstl -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

</dependencies>

controller代碼編寫和配置文件(省略)

注意 web.xml 中的配置:使用通配符加載

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-R5XZmdC2-1593164108712)(../img-folder/Maven/wps70.png)]

(5)運行調試

方法 1:在 ssm_web 工程的 pom.xml 中配置 tomcat 插件運行

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-xm6KdpRb-1593164108714)(../img-folder/Maven/wps71.png)]

運行 ssm_web 工程它會從本地倉庫下載依賴的 jar 包,所以當 ssm_web 依賴的 jar 包內容修改了必須及時發佈到本地倉庫,比如:ssm_web 依賴的 ssm_service 修改了,需要及時將ssm_service 發佈到本地倉庫。

方法 2:在父工程的 pom.xml 中配置 tomcat 插件運行,自動聚合並執行

推薦方法 2,如果子工程都在本地,採用方法 2 則不需要子工程修改就立即發佈到本地倉庫,父工程會自動聚合並使用最新代碼執行。

注意:如果子工程和父工程中都配置了 tomcat 插件,運行的端口和路徑以子工程爲準。

3.分模塊構建工程-依賴整合

每個模塊都需要 spring 或者 junit 的 jar,況且最終 package 打完包最後生成的項目中的

jar 就是各個模塊依賴的整合,所以我們可以把項目中所需的依賴都可以放到父工程中,模塊

中只留模塊和模塊之間的依賴,那父工程的 pom.xml 可以如下配置:

<properties>

<spring.version>5.0.2.RELEASE</spring.version>

<springmvc.version>5.0.2.RELEASE</springmvc.version>

<mybatis.version>3.4.5</mybatis.version>

</properties>



<dependencyManagement>

<dependencies>

<!-- Mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

<!-- springMVC -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${springmvc.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<!-- spring -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId> <version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId><groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId> <version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId></dependency>

<!-- spring 相關 -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

</dependency>
<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>

</dependency>

<!-- spring 相關 事務相關 -->

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>

</dependency>

<!-- junit 測試 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>

<version>2.0</version><scope>provided</scope>

</dependency>

<!-- jstl -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

</dependencies>


<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<target>1.8</target>

<source>1.8</source>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version>

</plugin>

</plugins>

</build>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章