栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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)


 4.0.0
 com.hd
 test-hd-parent
 0.0.1-SNAPSHOT
 pom
 
  test-hd-api
  test-hd-service
  test-hd-resource
  test-hd-foundation
  test-hd-modules
 


 
 

  
   
   
    com.hd
    test-hd-api
    0.0.1-SNAPSHOT
   

   
    com.hd
    test-hd-service
    0.0.1-SNAPSHOT
   

   
    com.hd
    test-hd-resource
    0.0.1-SNAPSHOT
   

   
    com.hd
    test-hd-foundation
    0.0.1-SNAPSHOT
   

   
   
    javax.servlet
    javax.servlet-api
    3.0.1
    provided
   
   
    javax.servlet.jsp
    jsp-api
    2.2
    provided
   

   
   
    javax.servlet
    jstl
    1.2
   

   
    taglibs
    standard
    1.1.2
   

   
    junit
    junit
    3.8.1
    test
   

  
 



test-hd-foundation中的依赖



 4.0.0
 
  com.hd
  test-hd-parent
  0.0.1-SNAPSHOT
 
 test-hd-foundation

 

  
  
   javax.servlet
   jstl
  

  
   taglibs
   standard
  

  
   junit
   junit
  
 

 
  
   
   
    org.apache.maven.plugins
    maven-compiler-plugin
    2.3.2
    
     1.7
     1.7
    
   
  
 


test-hd-api中的依赖关系



 4.0.0
 
  com.hd
  test-hd-parent
  0.0.1-SNAPSHOT
 
 test-hd-api
 

  
   com.hd
   test-hd-foundation
  

  
  
   javax.servlet
   jstl
  

  
   taglibs
   standard
  

  
   junit
   junit
  
 
 
  
   
   
    org.apache.maven.plugins
    maven-compiler-plugin
    2.3.2
    
     1.7
     1.7
    
   
  
  test-hd-api
 


test-hd-resource中的依赖关系



 4.0.0
 
  com.hd
  test-hd-parent
  0.0.1-SNAPSHOT
 
 test-hd-resource
 

  
   junit
   junit
  
 

 
  
   
   
    org.apache.maven.plugins
    maven-compiler-plugin
    2.3.2
    
     1.7
     1.7
    
   
  
 


test-hd-service中的依赖关系



 4.0.0
 
  com.hd
  test-hd-parent
  0.0.1-SNAPSHOT
 
 test-hd-service
 

  
   com.hd
   test-hd-foundation
  

  
   com.hd
   test-hd-api
  

  
  
   javax.servlet
   jstl
  

  
   taglibs
   standard
  

  
   junit
   junit
  
 


 
  
   
   
    org.apache.maven.plugins
    maven-compiler-plugin
    2.3.2
    
     1.7
     1.7
    
   
  
  test-hd-service
 


test-hd-module中的依赖关系 



 4.0.0
 
  com.hd
  test-hd-parent
  0.0.1-SNAPSHOT
 

 test-hd-modules
 pom

 
  test-hd-www
  test-hd-admin
 

 

  
   com.hd
   test-hd-foundation
  

  
   com.hd
   test-hd-service
  
  
   com.hd
   test-hd-api
  

  
   com.hd
   test-hd-resource
  

  
  
   javax.servlet
   jstl
  

  
   taglibs
   standard
  

  
   junit
   junit
  

 


 test-hd-www中的依赖关系 

 

 4.0.0
 
  com.hd
  test-hd-modules
  0.0.1-SNAPSHOT
 
 test-hd-www
 war

 
  
   
   
    org.apache.maven.plugins
    maven-compiler-plugin
    2.3.2
    
     1.7
     1.7
    
   
  
  test-hd-www
 



最后使用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包

    

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144038.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号