接上文内容,本节介绍Maven的聚合和继承。
从头阅读传送门
互联网时代,软件正在变得越来越复杂,开发人员通常会对软件划分模块,以获得清晰的设计、良好的分工及更高的可重用性。Maven的聚合特性能把多个模块聚合在一起构建,并促进各子模块通过继承父模块的pom配置来保持配置的一致。为了演示这些特性,本文的示例划分为数据持久层petstore-persist和petstore-web两个模块。
下面来介绍创建项目的过程,首先创建父模块perstore-parent。
File -> New -> Maven Project,简化一下流程,直接勾选创建简单项目,即跳过archetype的选择
下一步,填写Group Id和Artifact Id,注意作为父模块,打包类型(Packaging)必须选择pom。
完成后目录结构如下。
父模块除了构建文件pom.xml外没有实质内容,所以手动删除src目录,仅保留pom.xml文件,删除后目录结构如下:
接下来创建petstore-persist模块。在上图中petstore-parent上点击右键,选择New -> Project,在向导对话框中选择Maven Module,如下图:
下一步,这里仍然选择跳过archetype选项,模块名称填写petstore-persist
直接点击完成(Finish),完成后目录结构如下:
接下来创建Web子模块,重复“petstore-parent上点击右键,选择New -> Project,在向导对话框中选择Maven Module”步骤,但是不要勾选跳过archetype选择。填写模块名称后点击下一步。
archetype选择maven-archetype-webapp。
完成后目录结构如下:
在我的开发环境下,Maven的向导创建的Web模块版本是2.3,运行时会有一些兼容性问题,首先手动调整为3.0。
在项目上点击右键 -> Properties,并选择Project Facets菜单。现在选择3.0版本会提示错误,解决办法是先取消Dynamic Web Module选项,如下图:
点OK关闭对话框,修改petstore-web/src/main/webapps/WEB-INF/web.xml,如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>petstore-web</display-name> </web-app>从新打开Project Facets界面,勾选Dynamic Web Module选项,并选择版本号为3.0
然后点击Apply按钮保存设置。
然后切换到Java Build Path菜单,发现这里提示另一个错误,如下图:
解决办法是勾选JRE System Library[JavaSE-1.X],点击OK保存设置并关闭对话框,处理完成后项目中增加了一个WebContent目录,这对Maven项目来说是多余的,直接删除。
现在来解决Web模块上存在的红叉错误提示,查看详细信息,发现是“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”,说明是缺少编译的jar引用。在聚合结构中,通常在父模块的pom.xml中添加依赖,由其他子模块继承。这里有两种配置方法,第一种和普通项目相同,如下,子项目自动继承这些依赖。
petstore-parent/pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
但这种方式不够灵活,在中大型项目中,通常有非常多的依赖项,项目中即使一个功能非常简单的基础模块也会继承全部依赖,显然是不合理的。所以这里使用第二种配置方式,即使用dependencyManagement元素,它既可以规范各子模块使用的依赖版本,也能允许各子模块按需添加依赖,不过,配置文件代码会更多一些。
petstore-parent/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.example</groupId> <artifactId>petstore-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>petstore-persist</module> <module>petstore-web</module> </modules> <properties> <springframework.version>4.2.6.RELEASE</springframework.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> </dependencies> </dependencyManagement> </project>petstore-persist/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> <parent> <groupId>com.example</groupId> <artifactId>petstore-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>petstore-persist</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project>petstore-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> <parent> <groupId>com.example</groupId> <artifactId>petstore-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>petstore-web</artifactId> <packaging>war</packaging> <name>petstore-web Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>petstore-persist</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <finalName>petstore-web</finalName> </build> </project>在上面的配置中,子模块中的依赖不需要在添加<version>元素,与父模块中的设置保持一致。
compile 编译、测试、运行都有效
test 测试依赖范围, 只对于测试有效, 编译主代码和运行项目时无法使用此类依赖
provided 对于编译和测试有效, 运行时无效
runtime 测试和运行时有效, 编译主代码无效, 如JDBC驱动
system 与provided范围一致, 通过systemPath元素关联本机文件路径, 用于Maven库外的jar包
回到项目中,添加依赖后红色警告消失,但这里还有一个步骤要做,否则Web模块可能无法运行。在petstore-web上点击右键 -> Maven -> Update Project...。
到这里,项目就全部搭建完成了。我们来编写一些测试代码,来检查项目是否可以正常运行。最终目录结构如下图,红色方框圈中的是添加或修改了代码的文件,各文件的源码打包提供,就不再一一粘贴了。
运行结果如下:
本节代码下载
总结
本节中介绍了通过Maven的聚合和继承特性创建复杂的多模块项目,虽然看起来步骤有些繁琐,但掌握后操作并不复杂。下一节开始进入SpringMVC和Mybatis的功能和整合介绍,包括增删改查等功能。
从头开始基于Maven搭建SpringMVC+Mybatis项目(1)
从头开始基于Maven搭建SpringMVC+Mybatis项目(2)
从头开始基于Maven搭建SpringMVC+Mybatis项目(3)
从头开始基于Maven搭建SpringMVC+Mybatis项目(4)