博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Maven构建多模块项目
阅读量:6860 次
发布时间:2019-06-26

本文共 32971 字,大约阅读时间需要 109 分钟。

转自:http://www.cnblogs.com/h--d/p/6001366.html

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项目,参照:
                  •   同理可以配置其他的web子模块   test-hd-admin(web模块2)

                      

                   

                  配置个模块的依赖

                  •   在parent项目pom.xml中建立依赖管理(dependencyManagement)
                    1 
                    3
                    4.0.0
                    4
                    com.hd
                    5
                    test-hd-parent
                    6
                    0.0.1-SNAPSHOT
                    7
                    pom
                    8
                    9
                    test-hd-api
                    10
                    test-hd-service
                    11
                    test-hd-resource
                    12
                    test-hd-foundation
                    13
                    test-hd-modules
                    14
                    15 16 17
                    18
                    19 20
                    21
                    22
                    23
                    com.hd
                    24
                    test-hd-api
                    25
                    0.0.1-SNAPSHOT
                    26
                    27 28
                    29
                    com.hd
                    30
                    test-hd-service
                    31
                    0.0.1-SNAPSHOT
                    32
                    33 34
                    35
                    com.hd
                    36
                    test-hd-resource
                    37
                    0.0.1-SNAPSHOT
                    38
                    39 40
                    41
                    com.hd
                    42
                    test-hd-foundation
                    43
                    0.0.1-SNAPSHOT
                    44
                    45 46
                    47
                    48
                    javax.servlet
                    49
                    javax.servlet-api
                    50
                    3.0.1
                    51
                    provided
                    52
                    53
                    54
                    javax.servlet.jsp
                    55
                    jsp-api
                    56
                    2.2
                    57
                    provided
                    58
                    59 60
                    61
                    62
                    javax.servlet
                    63
                    jstl
                    64
                    1.2
                    65
                    66 67
                    68
                    taglibs
                    69
                    standard
                    70
                    1.1.2
                    71
                    72 73
                    74
                    junit
                    75
                    junit
                    76
                    3.8.1
                    77
                    test
                    78
                    79 80
                    81
                    82 83

                     

                  •     test-hd-foundation中的依赖
                    1 
                    2
                    5
                    4.0.0
                    6
                    7
                    com.hd
                    8
                    test-hd-parent
                    9
                    0.0.1-SNAPSHOT
                    10
                    11
                    test-hd-foundation
                    12 13
                    14 15
                    16
                    17
                    javax.servlet
                    18
                    jstl
                    19
                    20 21
                    22
                    taglibs
                    23
                    standard
                    24
                    25 26
                    27
                    junit
                    28
                    junit
                    29
                    30
                    31 32
                    33
                    34
                    35
                    36
                    org.apache.maven.plugins
                    37
                    maven-compiler-plugin
                    38
                    2.3.2
                    39
                    40
                    1.741
                    1.7
                    42
                    43
                    44
                    45
                    46

                     

                  •     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>

                    1 
                    2
                    5
                    4.0.0
                    6
                    7
                    com.hd
                    8
                    test-hd-parent
                    9
                    0.0.1-SNAPSHOT
                    10
                    11
                    test-hd-resource
                    12
                    13 14
                    15
                    junit
                    16
                    junit
                    17
                    18
                    19 20
                    21
                    22
                    23
                    24
                    org.apache.maven.plugins
                    25
                    maven-compiler-plugin
                    26
                    2.3.2
                    27
                    28
                    1.729
                    1.7
                    30
                    31
                    32
                    33
                    34

                     

                  •     test-hd-service中的依赖关系
                    1 
                    2
                    5
                    4.0.0
                    6
                    7
                    com.hd
                    8
                    test-hd-parent
                    9
                    0.0.1-SNAPSHOT
                    10
                    11
                    test-hd-service
                    12
                    13 14
                    15
                    com.hd
                    16
                    test-hd-foundation
                    17
                    18 19
                    20
                    com.hd
                    21
                    test-hd-api
                    22
                    23 24
                    25
                    26
                    javax.servlet
                    27
                    jstl
                    28
                    29 30
                    31
                    taglibs
                    32
                    standard
                    33
                    34 35
                    36
                    junit
                    37
                    junit
                    38
                    39
                    40 41 42
                    43
                    44
                    45
                    46
                    org.apache.maven.plugins
                    47
                    maven-compiler-plugin
                    48
                    2.3.2
                    49
                    50
                    1.751
                    1.7
                    52
                    53
                    54
                    55
                    test-hd-service
                    56
                    57

                     

                    1 
                    2
                    5
                    4.0.0
                    6
                    7
                    com.hd
                    8
                    test-hd-parent
                    9
                    0.0.1-SNAPSHOT
                    10
                    11
                    test-hd-service
                    12
                    13 14
                    15
                    com.hd
                    16
                    test-hd-foundation
                    17
                    18 19
                    20
                    com.hd
                    21
                    test-hd-api
                    22
                    23 24
                    25
                    26
                    javax.servlet
                    27
                    jstl
                    28
                    29 30
                    31
                    taglibs
                    32
                    standard
                    33
                    34 35
                    36
                    junit
                    37
                    junit
                    38
                    39
                    40 41 42
                    43
                    44
                    45
                    46
                    org.apache.maven.plugins
                    47
                    maven-compiler-plugin
                    48
                    2.3.2
                    49
                    50
                    1.751
                    1.7
                    52
                    53
                    54
                    55
                    test-hd-service
                    56
                    57

                     

                  •   test-hd-module中的依赖关系
                    1 
                    2
                    4
                    4.0.0
                    5
                    6
                    com.hd
                    7
                    test-hd-parent
                    8
                    0.0.1-SNAPSHOT
                    9
                    10 11
                    test-hd-modules
                    12
                    pom
                    13 14
                    15
                    test-hd-www
                    16
                    test-hd-admin
                    17
                    18 19
                    20 21
                    22
                    com.hd
                    23
                    test-hd-foundation
                    24
                    25 26
                    27
                    com.hd
                    28
                    test-hd-service
                    29
                    30
                    31
                    com.hd
                    32
                    test-hd-api
                    33
                    34 35
                    36
                    com.hd
                    37
                    test-hd-resource
                    38
                    39 40
                    41
                    42
                    javax.servlet
                    43
                    jstl
                    44
                    45 46
                    47
                    taglibs
                    48
                    standard
                    49
                    50 51
                    52
                    junit
                    53
                    junit
                    54
                    55 56
                    57

                     

                    1 
                    2
                    4
                    4.0.0
                    5
                    6
                    com.hd
                    7
                    test-hd-parent
                    8
                    0.0.1-SNAPSHOT
                    9
                    10 11
                    test-hd-modules
                    12
                    pom
                    13 14
                    15
                    test-hd-www
                    16
                    test-hd-admin
                    17
                    18 19
                    20 21
                    22
                    com.hd
                    23
                    test-hd-foundation
                    24
                    25 26
                    27
                    com.hd
                    28
                    test-hd-service
                    29
                    30
                    31
                    com.hd
                    32
                    test-hd-api
                    33
                    34 35
                    36
                    com.hd
                    37
                    test-hd-resource
                    38
                    39 40
                    41
                    42
                    javax.servlet
                    43
                    jstl
                    44
                    45 46
                    47
                    taglibs
                    48
                    standard
                    49
                    50 51
                    52
                    junit
                    53
                    junit
                    54
                    55 56
                    57

                     

                  •     test-hd-www中的依赖关系
                    1 
                    2
                    5
                    4.0.0
                    6
                    7
                    com.hd
                    8
                    test-hd-modules
                    9
                    0.0.1-SNAPSHOT
                    10
                    11
                    test-hd-www
                    12
                    war
                    13 14
                    15
                    16
                    17
                    18
                    org.apache.maven.plugins
                    19
                    maven-compiler-plugin
                    20
                    2.3.2
                    21
                    22
                    1.723
                    1.7
                    24
                    25
                    26
                    27
                    test-hd-www
                    28
                    29 30

                     

                    1 
                    2
                    5
                    4.0.0
                    6
                    7
                    com.hd
                    8
                    test-hd-modules
                    9
                    0.0.1-SNAPSHOT
                    10
                    11
                    test-hd-www
                    12
                    war
                    13 14
                    15
                    16
                    17
                    18
                    org.apache.maven.plugins
                    19
                    maven-compiler-plugin
                    20
                    2.3.2
                    21
                    22
                    1.723
                    1.7
                    24
                    25
                    26
                    27
                    test-hd-www
                    28
                    29 30

                     

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

                      

<?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>

你可能感兴趣的文章
手把手教你使用腾讯的热修复框架-Tinker
查看>>
《当程序员的那些狗日日子》(三十一)特殊任务
查看>>
9.10---堆箱子问题(CC150)
查看>>
Spark技术内幕:究竟什么是RDD
查看>>
新功能!从 Dropbox 部署到 Windows Azure 网站
查看>>
指尖上的电商---(10)SolrAdmin中加入多核
查看>>
CCEditBox/CCEditBoxImplAndroid
查看>>
TCP/IP协议栈--IP首部选项字段的分析
查看>>
Kubuntu 初始配置
查看>>
python中列表和元组的操作(结尾格式化输出小福利)
查看>>
用过的一些服务器集成软件
查看>>
一键拨打
查看>>
20120522:ERROR - ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
查看>>
Maven构建war项目添加版本号
查看>>
更新 手淘 flexible 布局 rem 单位适配问题
查看>>
第三次作业
查看>>
新浪微博登录接口实例
查看>>
wcf技术剖析_会话
查看>>
AngularJS 指令的 Scope (作用域)
查看>>
gitlab的使用
查看>>