免费获取学习方案
ARTICLE DETAIL

资讯详情

深耕编程基础知识与建站技术分享的一线实战洞察。

SSM 聚合项目搭建:多 Maven 模块项目

SSM 聚合项目搭建:多 Maven 模块项目 一、什么是 Maven 聚合父子工程1.1 模块划分说明hami‑parent 作为父工程聚合工程本身不写业务代码只做两件事聚合所有子模块使用dependencyManagement统一管理 jar 包版本子模块分工模块名打包方式职责说明hami‑parentpom父工程版本管理、聚合子模块无业务代码hami‑corejar核心公共模块model、dao、service、mapper 映射文件、工具类、逆向工程、代码生成器被其他模块依赖hami‑consolewar后台管理系统只写 Controller、jsp 页面依赖 ham‑corehami‑portalwar前台用户门户系统播放器、歌曲检索只写 Controller、jsp 页面依赖 hami‑corehami‑filewar独立文件服务器专门处理图片、MP3 文件上传下载独立文件服务器专门处理图片、MP3 文件上传下载核心优势公共的 dao、service 只写一份放在 hami‑core后台、前台直接依赖 jar 复用避免重复拷贝代码。文件服务器独立部署实现静态资源和业务服务解耦。1.2 运行逻辑hami‑core 编译打包生成 jar 包hami‑console、hami‑portal、hami‑file 引入 hami‑core 依赖各个 war 模块分别部署 tomcat实现多服务分开运行。项目目录结构截图参考二、父工程 hami‑parent pom.xml 配置父工程 packaging 必须是pom使用modules标签配置需要聚合的子模块dependencyManagement只是声明版本不会实际引入依赖子模块需要使用才需要再写 dependency。?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdcn.tx.music/groupId artifactIdham-parent/artifactId version1.0-SNAPSHOT/version !--父工程打包类型必须pom-- packagingpom/packaging !--聚合子模块填写子模块文件夹名字-- modules moduleham-core/module moduleham-console/module moduleham-portal/module moduleham-file/module /modules !--统一管理版本号子模块不需要写version-- properties project.build.sourceEncodingUTF-8/project.build.sourceEncoding maven.compiler.source1.8/maven.compiler.source maven.compiler.target1.8/maven.compiler.target spring.version5.0.2.RELEASE/spring.version slf4j.version1.6.6/slf4j.version log4j.version1.2.12/log4j.version mysql.version5.1.6/mysql.version mybatis.version3.4.5/mybatis.version aspectj.version1.6.8/aspectj.version junit.version4.12/junit.version servlet.version2.5/servlet.version jsp.version2.0/jsp.version jstl.version1.2/jstl.version druid.version1.1.10/druid.version /properties !--版本托管只定义版本子模块按需引入不会全部下载jar包-- dependencyManagement dependencies dependency groupIdorg.springframework/groupId artifactIdspring-context/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-webmvc/artifactId version${spring.version}/version /dependency dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version${mybatis.version}/version /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version${mysql.version}/version /dependency !--子模块之间依赖core模块坐标war子模块引入该依赖即可拿到core里面所有类-- dependency groupIdcn.tx.music/groupId artifactIdham-core/artifactId version1.0-SNAPSHOT/version /dependency /dependencies /dependencyManagement /project重点区分dependencies直接引入 jar所有子模块全部继承全部下载。dependencyManagement仅做版本声明子模块需要手动写 dependency 才会真正引入推荐聚合项目使用。三、子模块 pom 编写3.1 hami‑corejar 公共模块packaging 默认 jar通过parent继承父工程直接写 dependencies 引入需要的包不需要写 version版本由父工程 dependencyManagement 管控。?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd !--继承父工程-- parent artifactIdham-parent/artifactId groupIdcn.tx.music/groupId version1.0-SNAPSHOT/version /parent modelVersion4.0.0/modelVersion artifactIdham-core/artifactId dependencies dependency groupIdorg.springframework/groupId artifactIdspring-context/artifactId /dependency dependency groupIdorg.springframework/groupId artifactIdspring-jdbc/artifactId /dependency dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId /dependency /dependencies /project3.2 hami‑console 后台war 模块打包方式设置war引入 hami‑core 依赖直接复用 core 中所有 model、service、dao 代码。?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd parent artifactIdham-parent/artifactId groupIdcn.tx.music/groupId version1.0-SNAPSHOT/version /parent modelVersion4.0.0/modelVersion artifactIdham-console/artifactId packagingwar/packaging dependencies !--依赖核心模块拿到dao/service/model-- dependency groupIdcn.tx.music/groupId artifactIdham-core/artifactId /dependency !--web相关依赖-- dependency groupIdorg.springframework/groupId artifactIdspring-webmvc/artifactId /dependency dependency groupIdjavax.servlet/groupId artifactIdservlet-api/artifactId scopeprovided/scope /dependency /dependencies /projecthami‑portal、hami‑file 配置和 hami‑console 几乎一致只需要修改 artifactId。打包方式区分pom父聚合工程用来管理版本、聚合子模块不能部署 tomcat。jar普通 Java 包hami‑core给其他模块依赖不能直接部署 tomcat。warweb 工程可以放到 tomcat 运行包含 webapp、jsp、web.xml。四、IDEA 环境配置聚合项目重点4.1 JDK 配置File → Project StructureProject SDK选择 JDK1.8Project language level84.2 Maven 配置File → Settings → Build,Execution,Deployment → Build Tools → MavenMaven home directory本地解压好的 maven3.5.3 路径User settings file勾选 Override指向自己本地仓库的 settings.xmlWorking directory 运行工作目录执行 hami‑core 下逆向工程、代码生成器 main 方法的时候运行配置的Working directory必须设置为hami‑parent 父工程文件夹否则读取 xml、tpl 模板文件报FileNotFoundException文件找不到。现象同样代码单模块正常多模块报找不到配置文件90% 都是工作目录没有指向父工程。五、Maven 聚合项目开发流程在 MySQL 执行 SQL 脚本创建数据库 ham 以及所有数据表编写 hami‑core 模块实体 model、dao、mapper.xml、service、通用 BaseDao、BaseService逆向工程、自定义代码生成器全部写在 core先编译父工程mvn clean install把 hami‑core 安装到本地 maven 仓库重要war 模块依赖 hami‑core必须 install 之后war 模块才能识别 core 里面的 Java 类否则报类找不到。开发 hami‑console 后台只写 Controller、jsp 页面service 直接注入 core 中定义好的 Service开发 hami‑portal 前台播放器模块同样依赖 corehami‑file 独立文件服务器配置 tomcat处理图片、mp3 上传分别部署 war 包到不同 tomcat实现多服务运行。六、高频踩坑汇总坑 1子模块找不到 hami‑core 里面的类没有执行mvn clean install父工程core 没有安装到本地仓库。父、子模块 groupId、artifactId、version 版本号不一致。core 模块编译报错打包失败install 不会生成可用 jar。坑 2执行 main 方法读取 xml/tpl 模板报 FileNotFoundException原因IDEA 运行配置 Working directory 不是父工程 ham‑parent。解决Edit Configurations → 修改 Working directory选择父工程根目录。坑 3版本冲突同一个 jar 出现多个版本使用 Maven Helper 插件查看依赖树排除冲突 jar全部版本交给父工程dependencyManagement统一管控子模块不写 version。坑 4war 模块 tomcat 启动报 servlet-api 类重复servlet-api scope 设置为provided代表编译使用tomcat 运行时由容器提供不要打入 war 包。dependency groupIdjavax.servlet/groupId artifactIdservlet-api/artifactId scopeprovided/scope /dependency坑 5父工程写 dependencies所有子模块全部引入大量无用 jar不要在父工程直接写dependencies优先使用dependencyManagement做版本托管子模块按需引入。坑 6聚合打包顺序异常执行 mvn package 父工程maven 会自动按照依赖顺序编译先 core再 console/portal/file。如果手动打包必须先打包 core。七、聚合项目优点公共业务dao/service/model只写一份core 模块复用减少重复代码后台、前台业务层直接复用。版本统一管理修改版本号只需要改父工程 properties子模块无需改动。业务解耦后台、前台、文件服务器拆分成独立 war可以分开部署单独重启。适合中型项目课程大作业、初级企业项目常用架构。
返回列表