Maven多模块项目

我们为什么要进行模块化开发?

在多人使用Maven协作开发项目时,尤其是稍微上点规模的项目,每个RD的工作都细分到具体功能和模块,有些模块甚至还要单独部署。

我们假设有这样一个商城项目,包括以下几个模块:

  • 商城前台(shop)
  • 管理后台(admin)
  • 数据库交互模块(dao)
  • 通用业务模块(service)
  • 接口模块(api)
  • 通用工具(util)

其中shop和admin需要单独部署,dao、service、util你可能想要一些经验丰富的人来维护,如果使用一个应用来管理的话,所有的功能和模块都会耦合在一起,所有人都可以随意修改代码,这显然不是我们所期望的。

而且使用一个应用来管理的话,任何一个点的代码有变更,整个项目就需要重新build,使用模块化开发的另一个好处是如果dao的代码被修改,只需要重新build dao模块就可以了。web模块可以build成war,dao、service、util等可以build成jar,只需要配置好依赖关系,就可以实现模块间的解耦合。这样的设计才是遵循“高内聚,低耦合”设计原则的。

我们如何进行模块化开发呢?

我们使用上面的例子进行演示,先进行合理的优化,我们希望dao和service作为通用的底层工具来使用,把它们合并成一个核心模块(core),build成core.jar,简单的Maven模块化项目结构如下:

---------- mall         //顶级项目
   |------ pom.xml      //packaging = pom
   |------ mall-util    //通用工具
   |  |--- pom.xml      //packaging = jar
   |------ mall-core    //核心模块
   |  |--- pom.xml      //packaging = jar
   |------ mall-web-api //接口模块
   |  |--- pom.xml      //packaging = war
   |------ mall-web-admin//管理后台
   |  |--- pom.xml      //packaging = war
   |------ mall-web-shop//商城前台
   |  |--- pom.xml      //packaging = war
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这些模块中api、admin、shop均是可以单独部署的web应用,相互之间没有依赖关系,但都依赖于core模块,而core模块依赖于util模块。接下来我们按照上述确定的结构来搭建项目结构。

使用IDEA来创建Maven多模块项目

一、创建一个普通Maven项目

  1. New Project

image

  1. 填写基本信息,这里使用ipr作为项目描述文件

image

  1. 普通Maven项目不需要使用Maven模板搭建

image

二、给Maven项目添加模块

  1. New Module

image

  1. 填写基本信息,jar项目同样不需要使用Maven模板搭建

image

  1. 这个时候就可以看到,我们所添加的module已经被引入到parent的pom文件里了
<groupId>com.mall</groupId>
<artifactId>mall</artifactId>
<packaging>pom</packaging> //打包方式为pom
<version>1.0-SNAPSHOT</version>

<modules>
    <module>mall-util</module>
</modules>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. 变更util模块的构建方式为jar
<parent>
    <artifactId>mall</artifactId>
    <groupId>com.mall</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<packaging>jar</packaging> //打包方式为jar
<artifactId>mall-util</artifactId>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

三、给Maven项目添加web模块

  1. 创建一个module,并选中“Create from archetype”选项,同时maven模板选择webapp

image

  1. 接下来耐心的等待maven帮你创建好module,模块信息已经被添加
<modules>
    <module>mall-util</module>
    <module>mall-web-admin</module>
</modules>
  • 1
  • 2
  • 3
  • 4

目录结构如下:

image

pom:

<parent>
    <artifactId>mall</artifactId>
    <groupId>com.mall</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall-web-admin</artifactId>
<packaging>war</packaging>
<name>mall-web-admin</name>
<url>https://github.com/beiyoufx</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalName>mall-web-admin</finalName>
</build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

四、添加模块间的依赖关系

  1. 增加core与util的依赖

image

  1. 增加admin与core的依赖关系

image

admin与core、util的依赖链

image

多模块项目的构建与发布

打包

image

所有在root项目中进行的构建都会传递到模块中,例如root中的package会打包整个项目,当文件有变动时会进行重新聚合,其他命令同理。模块中的package只会打包当前模块。

使用source:jar命令会将源码打包。

发布

web模块可以单独部署也可聚合部署。

版权声明:本文为博主原创文章,转载请在显著位置注明出处并指明原文链接。 http://blog.csdn.net/u011404265/article/details/54891229
 
 
 
 
 
 
 

 一个多模块项目通过一个父POM 引用一个或多个子模块来定义。父项目,通过以下配置,将子项目关联。

 

[xhtml] view plain copy
 
  1. <packaging>pom</packaging>  
  2. <modules>  
  3.           <module>simple-weather</module>  
  4.           <module>simple-webapp</module>  
  5. </modules>  

 

     其中值得注意的是<packaging>pom</packaging>这个父项目不像之前的项目那样创建一个JAR 或者一个WAR,它仅仅是一个引用其它Maven 项目的POM。pom.xml 中下一部分列出了项目的子模块。这些模块在modules元素中定义,每个modules 元素对应了一个simple-parent/目录下的子目录。Maven知道去这些子目录寻找pom.xml 文件,并且,在构建的simp-parent 的时候,它会将这些子模块包含到要构建的项目中。

     当然,仅仅在父项目,配置子项目是不能够真正实现关联的,因为,这毕竟需要子项目的同意,故!子项目中需要配置:

 

[xhtml] view plain copy
 
  1. <parent>  
  2.         <groupId>org.sonatype.mavenbook.ch06</groupId>  
  3.         <artifactId>simple-parent</artifactId>  
  4.         <version>1.0</version>  
  5. </parent>  

 

     现在,通过父pom.xml将2个子项目进行了关联,那么我们需要从simple-parent 项目运行mvn clean install 命令,将2个子项目打包,编译为一个项目!

     当Maven 执行一个带有子模块的项目的时候,Maven 首先载入父POM,然后定位所有的子模块POM。Maven 然后将所有这些项目的POM 放入到一个称为Maven 反应堆(Reactor)的东西中,由它负责分析模块之间的依赖关系。这个反应堆处理组件的排序,以确保相互独立的模块能以适当的顺序被编译和安装。

     当,通过父pom.xml进行完成多个子项目的关联后,我们可以像前几章一样,分别进入某个单独的模块,进行运行,以移动程序!

 

 注意:子项目的文件,位于父项目pom.xml同级,也就是,子项目的pom.xml位于父pom.xml的下一级文件中!

 

 

 

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项目

    

    

 

配置个模块的依赖

  •   在parent项目pom.xml中建立依赖管理(dependencyManagement)
    复制代码
     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3     <modelVersion>4.0.0</modelVersion>
     4     <groupId>com.hd</groupId>
     5     <artifactId>test-hd-parent</artifactId>
     6     <version>0.0.1-SNAPSHOT</version>
     7     <packaging>pom</packaging>
     8     <modules>
     9         <module>test-hd-api</module>
    10         <module>test-hd-service</module>
    11         <module>test-hd-resource</module>
    12         <module>test-hd-foundation</module>
    13         <module>test-hd-modules</module>
    14     </modules>
    15 
    16 
    17     <!-- maven依赖 -->
    18     <dependencyManagement>
    19 
    20         <dependencies>
    21             <!-- hd -->
    22             <dependency>
    23                 <groupId>com.hd</groupId>
    24                 <artifactId>test-hd-api</artifactId>
    25                 <version>0.0.1-SNAPSHOT</version>
    26             </dependency>
    27 
    28             <dependency>
    29                 <groupId>com.hd</groupId>
    30                 <artifactId>test-hd-service</artifactId>
    31                 <version>0.0.1-SNAPSHOT</version>
    32             </dependency>
    33 
    34             <dependency>
    35                 <groupId>com.hd</groupId>
    36                 <artifactId>test-hd-resource</artifactId>
    37                 <version>0.0.1-SNAPSHOT</version>
    38             </dependency>
    39 
    40             <dependency>
    41                 <groupId>com.hd</groupId>
    42                 <artifactId>test-hd-foundation</artifactId>
    43                 <version>0.0.1-SNAPSHOT</version>
    44             </dependency>
    45 
    46             <!-- Servlet -->
    47             <dependency>
    48                 <groupId>javax.servlet</groupId>
    49                 <artifactId>javax.servlet-api</artifactId>
    50                 <version>3.0.1</version>
    51                 <scope>provided</scope>
    52             </dependency>
    53             <dependency>
    54                 <groupId>javax.servlet.jsp</groupId>
    55                 <artifactId>jsp-api</artifactId>
    56                 <version>2.2</version>
    57                 <scope>provided</scope>
    58             </dependency>
    59 
    60             <!-- jstl -->
    61             <dependency>
    62                 <groupId>javax.servlet</groupId>
    63                 <artifactId>jstl</artifactId>
    64                 <version>1.2</version>
    65             </dependency>
    66 
    67             <dependency>
    68                 <groupId>taglibs</groupId>
    69                 <artifactId>standard</artifactId>
    70                 <version>1.1.2</version>
    71             </dependency>
    72 
    73             <dependency>
    74                 <groupId>junit</groupId>
    75                 <artifactId>junit</artifactId>
    76                 <version>3.8.1</version>
    77                 <scope>test</scope>
    78             </dependency>
    79 
    80         </dependencies>
    81     </dependencyManagement>
    82 
    83 </project>
    复制代码

     

  •     test-hd-foundation中的依赖
    复制代码
     1 <?xml version="1.0"?>
     2 <project
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     4     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     5     <modelVersion>4.0.0</modelVersion>
     6     <parent>
     7         <groupId>com.hd</groupId>
     8         <artifactId>test-hd-parent</artifactId>
     9         <version>0.0.1-SNAPSHOT</version>
    10     </parent>
    11     <artifactId>test-hd-foundation</artifactId>
    12 
    13     <dependencies>
    14 
    15         <!-- servlet -->
    16         <dependency>
    17             <groupId>javax.servlet</groupId>
    18             <artifactId>jstl</artifactId>
    19         </dependency>
    20 
    21         <dependency>
    22             <groupId>taglibs</groupId>
    23             <artifactId>standard</artifactId>
    24         </dependency>
    25 
    26         <dependency>
    27             <groupId>junit</groupId>
    28             <artifactId>junit</artifactId>
    29         </dependency>
    30     </dependencies>
    31 
    32     <build>
    33         <plugins>
    34             <!-- define the project compile level -->
    35             <plugin>
    36                 <groupId>org.apache.maven.plugins</groupId>
    37                 <artifactId>maven-compiler-plugin</artifactId>
    38                 <version>2.3.2</version>
    39                 <configuration>
    40                     <source>1.7</source>
    41                     <target>1.7</target>
    42                 </configuration>
    43             </plugin>
    44         </plugins>
    45     </build>
    46 </project>
    复制代码

     

  •     test-hd-api中的依赖关系
    复制代码
     1 <?xml version="1.0"?>
     2 <project
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     4     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     5     <modelVersion>4.0.0</modelVersion>
     6     <parent>
     7         <groupId>com.hd</groupId>
     8         <artifactId>test-hd-parent</artifactId>
     9         <version>0.0.1-SNAPSHOT</version>
    10     </parent>
    11     <artifactId>test-hd-api</artifactId>
    12     <dependencies>
    13 
    14         <dependency>
    15             <groupId>com.hd</groupId>
    16             <artifactId>test-hd-foundation</artifactId>
    17         </dependency>
    18 
    19         <!-- servlet -->
    20         <dependency>
    21             <groupId>javax.servlet</groupId>
    22             <artifactId>jstl</artifactId>
    23         </dependency>
    24 
    25         <dependency>
    26             <groupId>taglibs</groupId>
    27             <artifactId>standard</artifactId>
    28         </dependency>
    29 
    30         <dependency>
    31             <groupId>junit</groupId>
    32             <artifactId>junit</artifactId>
    33         </dependency>
    34     </dependencies>
    35     <build>
    36         <plugins>
    37             <!-- define the project compile level -->
    38             <plugin>
    39                 <groupId>org.apache.maven.plugins</groupId>
    40                 <artifactId>maven-compiler-plugin</artifactId>
    41                 <version>2.3.2</version>
    42                 <configuration>
    43                     <source>1.7</source>
    44                     <target>1.7</target>
    45                 </configuration>
    46             </plugin>
    47         </plugins>
    48         <finalName>test-hd-api</finalName>
    49     </build>
    50 </project>
    复制代码

     

  •     test-hd-resource中的依赖关系
    复制代码
     1 <?xml version="1.0"?>
     2 <project
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     4     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     5     <modelVersion>4.0.0</modelVersion>
     6     <parent>
     7         <groupId>com.hd</groupId>
     8         <artifactId>test-hd-parent</artifactId>
     9         <version>0.0.1-SNAPSHOT</version>
    10     </parent>
    11     <artifactId>test-hd-resource</artifactId>
    12     <dependencies>
    13 
    14         <dependency>
    15             <groupId>junit</groupId>
    16             <artifactId>junit</artifactId>
    17         </dependency>
    18     </dependencies>
    19 
    20     <build>
    21         <plugins>
    22             <!-- define the project compile level -->
    23             <plugin>
    24                 <groupId>org.apache.maven.plugins</groupId>
    25                 <artifactId>maven-compiler-plugin</artifactId>
    26                 <version>2.3.2</version>
    27                 <configuration>
    28                     <source>1.7</source>
    29                     <target>1.7</target>
    30                 </configuration>
    31             </plugin>
    32         </plugins>
    33     </build>
    34 </project>
    复制代码

     

  •     test-hd-service中的依赖关系
    复制代码
     1 <?xml version="1.0"?>
     2 <project
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     4     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     5     <modelVersion>4.0.0</modelVersion>
     6     <parent>
     7         <groupId>com.hd</groupId>
     8         <artifactId>test-hd-parent</artifactId>
     9         <version>0.0.1-SNAPSHOT</version>
    10     </parent>
    11     <artifactId>test-hd-service</artifactId>
    12     <dependencies>
    13 
    14         <dependency>
    15             <groupId>com.hd</groupId>
    16             <artifactId>test-hd-foundation</artifactId>
    17         </dependency>
    18 
    19         <dependency>
    20             <groupId>com.hd</groupId>
    21             <artifactId>test-hd-api</artifactId>
    22         </dependency>
    23 
    24         <!-- servlet -->
    25         <dependency>
    26             <groupId>javax.servlet</groupId>
    27             <artifactId>jstl</artifactId>
    28         </dependency>
    29 
    30         <dependency>
    31             <groupId>taglibs</groupId>
    32             <artifactId>standard</artifactId>
    33         </dependency>
    34 
    35         <dependency>
    36             <groupId>junit</groupId>
    37             <artifactId>junit</artifactId>
    38         </dependency>
    39     </dependencies>
    40 
    41 
    42     <build>
    43         <plugins>
    44             <!-- define the project compile level -->
    45             <plugin>
    46                 <groupId>org.apache.maven.plugins</groupId>
    47                 <artifactId>maven-compiler-plugin</artifactId>
    48                 <version>2.3.2</version>
    49                 <configuration>
    50                     <source>1.7</source>
    51                     <target>1.7</target>
    52                 </configuration>
    53             </plugin>
    54         </plugins>
    55         <finalName>test-hd-service</finalName>
    56     </build>
    57 </project>
    复制代码

     

  •   test-hd-module中的依赖关系
    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5     <parent>
     6         <groupId>com.hd</groupId>
     7         <artifactId>test-hd-parent</artifactId>
     8         <version>0.0.1-SNAPSHOT</version>
     9     </parent>
    10 
    11     <artifactId>test-hd-modules</artifactId>
    12     <packaging>pom</packaging>
    13 
    14     <modules>
    15         <module>test-hd-www</module>
    16         <module>test-hd-admin</module>
    17     </modules>
    18 
    19     <dependencies>
    20 
    21         <dependency>
    22             <groupId>com.hd</groupId>
    23             <artifactId>test-hd-foundation</artifactId>
    24         </dependency>
    25 
    26         <dependency>
    27             <groupId>com.hd</groupId>
    28             <artifactId>test-hd-service</artifactId>
    29         </dependency>
    30         <dependency>
    31             <groupId>com.hd</groupId>
    32             <artifactId>test-hd-api</artifactId>
    33         </dependency>
    34 
    35         <dependency>
    36             <groupId>com.hd</groupId>
    37             <artifactId>test-hd-resource</artifactId>
    38         </dependency>
    39 
    40         <!-- servlet -->
    41         <dependency>
    42             <groupId>javax.servlet</groupId>
    43             <artifactId>jstl</artifactId>
    44         </dependency>
    45 
    46         <dependency>
    47             <groupId>taglibs</groupId>
    48             <artifactId>standard</artifactId>
    49         </dependency>
    50 
    51         <dependency>
    52             <groupId>junit</groupId>
    53             <artifactId>junit</artifactId>
    54         </dependency>
    55 
    56     </dependencies>
    57 </project>
    复制代码

     

  •     test-hd-www中的依赖关系
    复制代码
     1 <?xml version="1.0"?>
     2 <project
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     4     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     5     <modelVersion>4.0.0</modelVersion>
     6     <parent>
     7         <groupId>com.hd</groupId>
     8         <artifactId>test-hd-modules</artifactId>
     9         <version>0.0.1-SNAPSHOT</version>
    10     </parent>
    11     <artifactId>test-hd-www</artifactId>
    12     <packaging>war</packaging>
    13 
    14     <build>
    15         <plugins>
    16             <!-- define the project compile level -->
    17             <plugin>
    18                 <groupId>org.apache.maven.plugins</groupId>
    19                 <artifactId>maven-compiler-plugin</artifactId>
    20                 <version>2.3.2</version>
    21                 <configuration>
    22                     <source>1.7</source>
    23                     <target>1.7</target>
    24                 </configuration>
    25             </plugin>
    26         </plugins>
    27         <finalName>test-hd-www</finalName>
    28     </build>
    29 
    30 </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包

  

posted @ 2018-03-21 10:14  无天666  阅读(1118)  评论(0编辑  收藏  举报