最近在做一個項目,這個項目有個特點,含有衆多業務模塊,各業務模塊間關聯內容很少,且公司領導又想達到各模塊可快速單獨拆分使用的效果(說白了就是公司沒錢,想把這個項目做完後,做下拆分,拆分成各個小產品單獨銷售),於是乎我想到是否可使用Maven來進行模塊化開發,將各個業務模塊做成一個個小的Jar包,到用的時候需要那個加載那個。
一,構想:
把系統分成以下幾個模塊(項目名稱proj):
1,proj模塊,作爲父模塊用於聚合所有子模塊,打包方式pom
2,proj-web模塊,用於發佈最終的war包,打包方式war
3,proj-core模塊,作爲項目核心模塊,可以把用戶管理、權限管理等基礎管理模塊放入其 中,打包方式jar
4,proj-util模塊,用於項目中共用的工具類,打包方式jar
5,proj-jslib模塊,用於項目中共用的JS類庫,打包方式jar
6,proj-common模塊,用戶項目中各業務模塊間的調用,自己不需要就沒加,打包方式jar
7,proj-businessModuleA,業務模塊A,打包方式jar
8,proj-businessModuleB,業務模塊B,打包方式jar
二,依賴關係:
三,創建項目
創建Proj Maven項目:
右鍵點擊proj項目,創建proj-web Maven項目:
其他模塊與web模塊相似,只是打包方式變爲了jar,maven原型可以不用選擇webapp而已。
四,POM文件:
proj項目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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>proj</artifactId> <packaging>pom</packaging> <version>0.0.1</version> <name>Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>proj</finalName> </build> <modules> <module>proj-web</module> <module>proj-moduleA</module> <module>proj-util</module> <module>proj-core</module> <module>proj-jslib</module> <module>proj-moduleb</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.4.RELEASE</spring.version> <jackson.version>2.5.0</jackson.version> <activiti.version>5.16</activiti.version> <cxf.version>3.0.4</cxf.version> <shiro.version>1.2.2</shiro.version> <mq.version>5.7.0</mq.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>
web模塊pom文件:
<?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>test</groupId> <artifactId>proj</artifactId> <version>0.0.1</version> </parent> <artifactId>proj-web</artifactId> <packaging>war</packaging> <name>proj-web Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>test</groupId> <artifactId>proj-moduleA</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>test</groupId> <artifactId>proj-moduleB</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>test</groupId> <artifactId>proj-core</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>proj-web</finalName> </build> </project>
core模塊POM文件:
<?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>test</groupId> <artifactId>proj</artifactId> <version>0.0.1</version> </parent> <artifactId>proj-core</artifactId> <name>proj-core</name> <packaging>jar</packaging> <build> <finalName>proj-core</finalName> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>test</groupId> <artifactId>proj-util</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>test</groupId> <artifactId>proj-jslib</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
其他模塊和Core模塊POM文件類似不在描述,只需添加各自所依賴的項目即可。
五,文件存放規則
依據servlet3.0,可直接訪問jar包中位於src\main\resources\META-INF\resources中的文件,
因此各模塊(jar包)中文件存放規則如下:
遇到的問題:
在項目運行過程中發現jsp中引入jar包中的js文件無法獲取,http請求404,經過多次嘗試,發現本人在web.xml中配置的springmvc DispatcherServlet的url-pattern爲“/”,即匹配所有請求,這些js以及圖片文件經過DispatcherServlet處理後獲取不到。
解決方式:修改 url-pattern 爲“*.jspx”,即讓DispatcherServlet只處理後綴爲“jspx”的http請求。
<servlet> <description>spring mvc servlet</description> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:springmvc.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.jspx</url-pattern> </servlet-mapping>