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

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

转自: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>

转载于:https://www.cnblogs.com/sharpest/p/5969191.html

你可能感兴趣的文章
Sitecore标准模板字段
查看>>
poj 1088 滑雪
查看>>
《内向者的优势》读书笔记
查看>>
卡方检验(Chi-square test/Chi-Square Goodness-of-Fit Test)
查看>>
单例模式的七种写法
查看>>
CommonJS
查看>>
.iOS APP Project or Mac APP Project编译错误提示: My Mac 64-bit is not valid for Running the scheme...
查看>>
深入Django ORM的继承关系
查看>>
初始化ArrayList的两种方法
查看>>
想要装逼,全靠3D
查看>>
为什么说我们需要软件架构图?
查看>>
NIKE旗下品牌JORDAN发力新零售 ,瞄准了天猫小黑盒
查看>>
智能汽车真的来了!百度福田超级卡车亮相百度云智峰会
查看>>
OPPO R17|R17 Pro新年版首销火爆,2019最应景的新年礼物
查看>>
华为消费者业务公布2017上半年智能手机收入暴涨
查看>>
福州三江口大桥主线通车
查看>>
湖南益阳办牡丹文化艺术节 游人新春可观牡丹盛开
查看>>
业界:通用航空机场的修建不能只是民航局的事
查看>>
陕西榆林“精准施策”提升民众生活质量 发展中改善民生
查看>>
春运期间国航将加飞进出成都航班406班次 增座超十万个
查看>>