提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Maven模块概述
使⽤maven 提供的多模块构建的特性完成maven 环境下多个模块的项⽬的管理与构建
这⾥以四个模块为例来搭建项⽬,以达到通俗易懂的初衷
模块 maven_parent —– 基模块,就是常说的parent (pom)
模块 maven_dao —– 数据库的访问层,例如jdbc操作(jar) 模
块 maven_service —– 项⽬的业务逻辑层 (jar)
模块 maven_controller —– ⽤来接收请求,响应数据 (war)
项目模块之间的关系
- maven_parent是三个子模块的父模块 需要添加三个子模块的依赖
- maven_dao用于数据库访问之类的数据处理 需要提供给maven_service模块
- maven_service接收maven_dao对数据处理的结果 因此 需要添加maven_dao模块的依赖
- maven_controller需要接收请求并且与web网页进行连线 因此需要构建的是web工程 并且添加maven_service的依赖
一、创建maven-parent项目及子模块
-
目录结构:
-
修改父模块和三个子模块的配置——pom.xml文件
- maven_dao 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"> <parent> <artifactId>maven_parent</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>maven_dao</artifactId> <version>1.0-SNAPSHOT</version> <name>maven_dao</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> </build> </project>
- maven_service 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"> <parent> <artifactId>maven_parent</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>maven_service</artifactId> <version>1.0-SNAPSHOT</version> <name>maven_service</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--引入maven_dao模块的依赖--> <dependency> <groupId>org.example</groupId> <artifactId>maven_dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> </build> </project>
- maven_controller 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"> <parent> <artifactId>maven_parent</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>maven_controller</artifactId> <packaging>war</packaging> <name>maven_controller Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--引入maven_service模块的依赖--> <dependency> <groupId>org.example</groupId> <artifactId>maven_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 因为Controller层是web层 需要Servlet依赖 --> <!-- 引入Servlet依赖 --> <dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>maven_controller</finalName> <!-- 添加插件 --> <plugins> <!-- tomcat7插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <!-- <port>8080</port> --> <path>/web</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> </project>
- maven_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>org.example</groupId> <artifactId>maven_parent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>maven_dao</module> <module>maven_service</module> <module>maven_controller</module> </modules> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
二、多模块依赖连接测试代码
-
目录结构
-
maven_dao com.wedu.dao.UserDao
package com.wedu.dao; public class UserDao { public static void testDao(){ System.out.println("这是dao层的test"); } }
-
maven_service com.wedu.dao.UserService
package com.wedu.service; import com.wedu.dao.UserDao; public class UserService { public static void testService(){ System.out.println("这是service层 test"); //调用maven_dao模块的方法——这一步需要将dao模块的依赖引进 UserDao.testDao(); } }
-
maven_controller com.wedu.dao.UserServlet
package com.wedu.controller; import com.wedu.service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/user") public class UserServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse reps) throws ServletException, IOException{ System.out.println("UserServlet Test...."); //调用maven_service模块的方法 UserService.testService(); } }
-
添加运行配置
- 将各个模块都先打包——jar包/war包
因为进行打包后仓库中才有记录,之前配置的依赖才会有效
- 将各个模块都先打包——jar包/war包
-
tamcat7:run 启动
-
结果:
复制网址后再浏览器打开 在连接末尾 输入在controller模块设置的 “user”
控制台会显示调用各模块方法的打印结果
三、子模块打包错误!!
- 目录结构
问题:先打包UserDao —— build success
再打包UserService,此时出错!错误信息如下
Failed to execute goal on project maven_service: Could not resolve dependencies for project org.example:maven_service:jar:1.0-SNAPSHOT: Failed to collect dependencies at org.example:maven_dao:jar:1.0-SNAPSHOT
原因:可能是父模块没有instal,会导致子模块的依赖下载失败,无法找到jar包
解决:先install maven_parent 再install maven_Service