详解使用Maven构建多模块项目(图文)

发布时间 - 2026-01-11 03:25:00    点击率:

Maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理。尤其是一些开源框架,也是采用多模块的方式,提供插件集成,用户可以根据需要配置指定的模块。

项目结构如下:

     test-hd-parent   (父级)
       ---pom.xml
       ---test-hd-api     (第三方接口层)
          ----pom.xml 
       ---test-hd-foundation  (基础工具层)
          ----pom.xml
       ---test-hd-resource  (资源层) 
          ----pom.xml
       ---test-hd-service    (逻辑业务层)
          ----pom.xml
       ---test-hd-modules    (web层)
          ----pom.xml
            ---test-hd-www      (web模块1)
              ----pom.xml
            ---test-hd-admin      (web模块2)
              ----pom.xml  

创建一个父maven工程

新建一个maven项目,选择存储位置,并选择创建一个简单的maven工程

输入Group Id、Artifact Id、Packaging,packaging选择pom包

    

生成父工程,pom.xml如下

   

删除工程中的src 目录

    

创建子模块

右击父工程名---》New---》Project,然后选择新建一个maven module工程

    

设置子工程名以及父工程,再设置快速创建模式

    

得到子工程(test-hd-api,第三方接口层),设置编译的jdk

    

同理设置,子模块:test-hd-foundation(基础工具层)、test-hd-resource(资源层) 、test-hd-service(逻辑业务层)
新建test-hd-modules (web层),选择创建一个a simple project,输入Group Id、Artifact Id、Packaging,packaging选择pom包

        

创建web子模块

web子模块在建在test-hd-modules (web层)里面,右击test-hd-modules 工程名---》New---》Project,然后选择新建一个maven module工程,设置子工程名以及父工程,选择新建web项目

    

配置maven web项目,参照:【Maven】Eclipse 使用Maven创建Java Web项目

同理可以配置其他的web子模块   test-hd-admin(web模块2)

    

配置个模块的依赖

在parent项目pom.xml中建立依赖管理(dependencyManagement)

<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.hd</groupId>
 <artifactId>test-hd-parent</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>pom</packaging>
 <modules>
  <module>test-hd-api</module>
  <module>test-hd-service</module>
  <module>test-hd-resource</module>
  <module>test-hd-foundation</module>
  <module>test-hd-modules</module>
 </modules>


 <!-- maven依赖 -->
 <dependencyManagement>

  <dependencies>
   <!-- hd -->
   <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
   </dependency>

   <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
   </dependency>

   <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-resource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
   </dependency>

   <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-foundation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
   </dependency>

   <!-- Servlet -->
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
   </dependency>
   <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
   </dependency>

   <!-- jstl -->
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
   </dependency>

   <dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
   </dependency>

   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
   </dependency>

  </dependencies>
 </dependencyManagement>

</project>

test-hd-foundation中的依赖

<?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.hd</groupId>
  <artifactId>test-hd-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <artifactId>test-hd-foundation</artifactId>

 <dependencies>

  <!-- servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>

  <dependency>
   <groupId>taglibs</groupId>
   <artifactId>standard</artifactId>
  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

test-hd-api中的依赖关系

<?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.hd</groupId>
  <artifactId>test-hd-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <artifactId>test-hd-api</artifactId>
 <dependencies>

  <dependency>
   <groupId>com.hd</groupId>
   <artifactId>test-hd-foundation</artifactId>
  </dependency>

  <!-- servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>

  <dependency>
   <groupId>taglibs</groupId>
   <artifactId>standard</artifactId>
  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
  <finalName>test-hd-api</finalName>
 </build>
</project>

test-hd-resource中的依赖关系

<?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.hd</groupId>
  <artifactId>test-hd-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <artifactId>test-hd-resource</artifactId>
 <dependencies>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

test-hd-service中的依赖关系

<?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.hd</groupId>
  <artifactId>test-hd-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <artifactId>test-hd-service</artifactId>
 <dependencies>

  <dependency>
   <groupId>com.hd</groupId>
   <artifactId>test-hd-foundation</artifactId>
  </dependency>

  <dependency>
   <groupId>com.hd</groupId>
   <artifactId>test-hd-api</artifactId>
  </dependency>

  <!-- servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>

  <dependency>
   <groupId>taglibs</groupId>
   <artifactId>standard</artifactId>
  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
 </dependencies>


 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
  <finalName>test-hd-service</finalName>
 </build>
</project>

test-hd-module中的依赖关系 

<?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.hd</groupId>
  <artifactId>test-hd-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>

 <artifactId>test-hd-modules</artifactId>
 <packaging>pom</packaging>

 <modules>
  <module>test-hd-www</module>
  <module>test-hd-admin</module>
 </modules>

 <dependencies>

  <dependency>
   <groupId>com.hd</groupId>
   <artifactId>test-hd-foundation</artifactId>
  </dependency>

  <dependency>
   <groupId>com.hd</groupId>
   <artifactId>test-hd-service</artifactId>
  </dependency>
  <dependency>
   <groupId>com.hd</groupId>
   <artifactId>test-hd-api</artifactId>
  </dependency>

  <dependency>
   <groupId>com.hd</groupId>
   <artifactId>test-hd-resource</artifactId>
  </dependency>

  <!-- servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>

  <dependency>
   <groupId>taglibs</groupId>
   <artifactId>standard</artifactId>
  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>

 </dependencies>
</project>

 test-hd-www中的依赖关系 

 <?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.hd</groupId>
  <artifactId>test-hd-modules</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <artifactId>test-hd-www</artifactId>
 <packaging>war</packaging>

 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
  <finalName>test-hd-www</finalName>
 </build>

</project>

最后使用maven-update整个工程,右击父工程名--》Maven--》Update Project

    

打包和发布

打包,右击父工程名 test-hd-parent---->Run As--->Maven Install

 

打包web子工程,右击工程名test-hd-www--->Run As ---> Maven Build...---> Goals: clean package--->Run

  

            

右击工程名test-hd-www,进行刷新,找到war包,放到tomcat的webapps中,启动tomcat,即可访问工程http://localhost:8080/test-hd-www

    

可以去tomcat下面webapps》test-hd-www》WEB-INF》lib中,看到引用的jar包

    

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。 


# maven  # 构建多模块项目  # 多模块项目  # maven多模块项目搭建  # maven多模块工程打包部署的方法步骤  # SpringBoot+Maven 多模块项目的构建、运行、打包实战  # IntelliJ IDEA 构建maven多模块工程项目(详细多图)  # 基于maven中多个子模块的构建顺序  # 右击  # 创建一个  # 新建一个  # 第三方  # 多模  # 尤其是  # 其他的  # 适用于  # 比较大  # 可以根据  # 开源  # 大家多多  # 建在  # 复用  # jdk  # module  # simple  # nbsp  # project 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251811 】 【 AI营销90571


相关推荐: Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  如何正确下载安装西数主机建站助手?  矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?  详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点  JS弹性运动实现方法分析  Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程  车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?  Swift中switch语句区间和元组模式匹配  如何在不使用负向后查找的情况下匹配特定条件前的换行符  🚀拖拽式CMS建站能否实现高效与个性化并存?  Laravel如何实现数据导出到CSV文件_Laravel原生流式输出大数据量CSV【方案】  Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】  HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】  Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势  如何在建站之星绑定自定义域名?  Laravel怎么使用Intervention Image库处理图片上传和缩放  移动端脚本框架Hammer.js  Laravel如何创建自定义中间件?(Middleware代码示例)  三星、SK海力士获美批准:可向中国出口芯片制造设备  微信小程序 闭包写法详细介绍  制作电商网页,电商供应链怎么做?  Laravel Telescope怎么调试_使用Laravel Telescope进行应用监控与调试  Laravel怎么进行浏览器测试_Laravel Dusk自动化浏览器测试入门  详解jQuery停止动画——stop()方法的使用  Laravel如何为API生成Swagger或OpenAPI文档  php增删改查怎么学_零基础入门php数据库操作必知基础【教程】  Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  php485函数参数是什么意思_php485各参数详细说明【介绍】  Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】  微信小程序 配置文件详细介绍  php中::能调用final静态方法吗_final修饰静态方法调用规则【解答】  用v-html解决Vue.js渲染中html标签不被解析的问题  家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?  JavaScript如何实现类型判断_typeof和instanceof有什么区别  原生JS实现图片轮播切换效果  企业网站制作这些问题要关注  Laravel怎么做数据加密_Laravel内置Crypt门面的加密与解密功能  新三国志曹操传主线渭水交兵攻略  Python数据仓库与ETL构建实战_Airflow调度流程详解  Laravel如何生成API文档?(Swagger/OpenAPI教程)  怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?  Laravel如何处理CORS跨域问题_Laravel项目CORS配置与解决方案  Laravel如何自定义分页视图?(Pagination示例)  如何用免费手机建站系统零基础打造专业网站?  ChatGPT回答中断怎么办 引导AI继续输出完整内容的方法  如何快速搭建二级域名独立网站?  Laravel如何发送系统通知?(Notification渠道示例)  零服务器AI建站解决方案:快速部署与云端平台低成本实践  如何在橙子建站中快速调整背景颜色?