使用Maven构建多模块层级项目

原文地址: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项目,参照:【Maven】Eclipse 使用Maven创建Java Web项目
  •   同理可以配置其他的web子模块   test-hd-admin(web模块2)

    

 

配置个模块的依赖

  •   在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.0modelVersion>
     4     <groupId>com.hdgroupId>
     5     <artifactId>test-hd-parentartifactId>
     6     <version>0.0.1-SNAPSHOTversion>
     7     <packaging>pompackaging>
     8     <modules>
     9         <module>test-hd-apimodule>
    10         <module>test-hd-servicemodule>
    11         <module>test-hd-resourcemodule>
    12         <module>test-hd-foundationmodule>
    13         <module>test-hd-modulesmodule>
    14     modules>
    15 
    16 
    17     
    18     <dependencyManagement>
    19 
    20         <dependencies>
    21             
    22             <dependency>
    23                 <groupId>com.hdgroupId>
    24                 <artifactId>test-hd-apiartifactId>
    25                 <version>0.0.1-SNAPSHOTversion>
    26             dependency>
    27 
    28             <dependency>
    29                 <groupId>com.hdgroupId>
    30                 <artifactId>test-hd-serviceartifactId>
    31                 <version>0.0.1-SNAPSHOTversion>
    32             dependency>
    33 
    34             <dependency>
    35                 <groupId>com.hdgroupId>
    36                 <artifactId>test-hd-resourceartifactId>
    37                 <version>0.0.1-SNAPSHOTversion>
    38             dependency>
    39 
    40             <dependency>
    41                 <groupId>com.hdgroupId>
    42                 <artifactId>test-hd-foundationartifactId>
    43                 <version>0.0.1-SNAPSHOTversion>
    44             dependency>
    45 
    46             
    47             <dependency>
    48                 <groupId>javax.servletgroupId>
    49                 <artifactId>javax.servlet-apiartifactId>
    50                 <version>3.0.1version>
    51                 <scope>providedscope>
    52             dependency>
    53             <dependency>
    54                 <groupId>javax.servlet.jspgroupId>
    55                 <artifactId>jsp-apiartifactId>
    56                 <version>2.2version>
    57                 <scope>providedscope>
    58             dependency>
    59 
    60             
    61             <dependency>
    62                 <groupId>javax.servletgroupId>
    63                 <artifactId>jstlartifactId>
    64                 <version>1.2version>
    65             dependency>
    66 
    67             <dependency>
    68                 <groupId>taglibsgroupId>
    69                 <artifactId>standardartifactId>
    70                 <version>1.1.2version>
    71             dependency>
    72 
    73             <dependency>
    74                 <groupId>junitgroupId>
    75                 <artifactId>junitartifactId>
    76                 <version>3.8.1version>
    77                 <scope>testscope>
    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.0modelVersion>
     6     <parent>
     7         <groupId>com.hdgroupId>
     8         <artifactId>test-hd-parentartifactId>
     9         <version>0.0.1-SNAPSHOTversion>
    10     parent>
    11     <artifactId>test-hd-foundationartifactId>
    12 
    13     <dependencies>
    14 
    15         
    16         <dependency>
    17             <groupId>javax.servletgroupId>
    18             <artifactId>jstlartifactId>
    19         dependency>
    20 
    21         <dependency>
    22             <groupId>taglibsgroupId>
    23             <artifactId>standardartifactId>
    24         dependency>
    25 
    26         <dependency>
    27             <groupId>junitgroupId>
    28             <artifactId>junitartifactId>
    29         dependency>
    30     dependencies>
    31 
    32     <build>
    33         <plugins>
    34             
    35             <plugin>
    36                 <groupId>org.apache.maven.pluginsgroupId>
    37                 <artifactId>maven-compiler-pluginartifactId>
    38                 <version>2.3.2version>
    39                 <configuration>
    40                     <source>1.7source>
    41                     <target>1.7target>
    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.0modelVersion>
     6     <parent>
     7         <groupId>com.hdgroupId>
     8         <artifactId>test-hd-parentartifactId>
     9         <version>0.0.1-SNAPSHOTversion>
    10     parent>
    11     <artifactId>test-hd-apiartifactId>
    12     <dependencies>
    13 
    14         <dependency>
    15             <groupId>com.hdgroupId>
    16             <artifactId>test-hd-foundationartifactId>
    17         dependency>
    18 
    19         
    20         <dependency>
    21             <groupId>javax.servletgroupId>
    22             <artifactId>jstlartifactId>
    23         dependency>
    24 
    25         <dependency>
    26             <groupId>taglibsgroupId>
    27             <artifactId>standardartifactId>
    28         dependency>
    29 
    30         <dependency>
    31             <groupId>junitgroupId>
    32             <artifactId>junitartifactId>
    33         dependency>
    34     dependencies>
    35     <build>
    36         <plugins>
    37             
    38             <plugin>
    39                 <groupId>org.apache.maven.pluginsgroupId>
    40                 <artifactId>maven-compiler-pluginartifactId>
    41                 <version>2.3.2version>
    42                 <configuration>
    43                     <source>1.7source>
    44                     <target>1.7target>
    45                 configuration>
    46             plugin>
    47         plugins>
    48         <finalName>test-hd-apifinalName>
    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.0modelVersion>
     6     <parent>
     7         <groupId>com.hdgroupId>
     8         <artifactId>test-hd-parentartifactId>
     9         <version>0.0.1-SNAPSHOTversion>
    10     parent>
    11     <artifactId>test-hd-resourceartifactId>
    12     <dependencies>
    13 
    14         <dependency>
    15             <groupId>junitgroupId>
    16             <artifactId>junitartifactId>
    17         dependency>
    18     dependencies>
    19 
    20     <build>
    21         <plugins>
    22             
    23             <plugin>
    24                 <groupId>org.apache.maven.pluginsgroupId>
    25                 <artifactId>maven-compiler-pluginartifactId>
    26                 <version>2.3.2version>
    27                 <configuration>
    28                     <source>1.7source>
    29                     <target>1.7target>
    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.0modelVersion>
     6     <parent>
     7         <groupId>com.hdgroupId>
     8         <artifactId>test-hd-parentartifactId>
     9         <version>0.0.1-SNAPSHOTversion>
    10     parent>
    11     <artifactId>test-hd-serviceartifactId>
    12     <dependencies>
    13 
    14         <dependency>
    15             <groupId>com.hdgroupId>
    16             <artifactId>test-hd-foundationartifactId>
    17         dependency>
    18 
    19         <dependency>
    20             <groupId>com.hdgroupId>
    21             <artifactId>test-hd-apiartifactId>
    22         dependency>
    23 
    24         
    25         <dependency>
    26             <groupId>javax.servletgroupId>
    27             <artifactId>jstlartifactId>
    28         dependency>
    29 
    30         <dependency>
    31             <groupId>taglibsgroupId>
    32             <artifactId>standardartifactId>
    33         dependency>
    34 
    35         <dependency>
    36             <groupId>junitgroupId>
    37             <artifactId>junitartifactId>
    38         dependency>
    39     dependencies>
    40 
    41 
    42     <build>
    43         <plugins>
    44             
    45             <plugin>
    46                 <groupId>org.apache.maven.pluginsgroupId>
    47                 <artifactId>maven-compiler-pluginartifactId>
    48                 <version>2.3.2version>
    49                 <configuration>
    50                     <source>1.7source>
    51                     <target>1.7target>
    52                 configuration>
    53             plugin>
    54         plugins>
    55         <finalName>test-hd-servicefinalName>
    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.0modelVersion>
     5     <parent>
     6         <groupId>com.hdgroupId>
     7         <artifactId>test-hd-parentartifactId>
     8         <version>0.0.1-SNAPSHOTversion>
     9     parent>
    10 
    11     <artifactId>test-hd-modulesartifactId>
    12     <packaging>pompackaging>
    13 
    14     <modules>
    15         <module>test-hd-wwwmodule>
    16         <module>test-hd-adminmodule>
    17     modules>
    18 
    19     <dependencies>
    20 
    21         <dependency>
    22             <groupId>com.hdgroupId>
    23             <artifactId>test-hd-foundationartifactId>
    24         dependency>
    25 
    26         <dependency>
    27             <groupId>com.hdgroupId>
    28             <artifactId>test-hd-serviceartifactId>
    29         dependency>
    30         <dependency>
    31             <groupId>com.hdgroupId>
    32             <artifactId>test-hd-apiartifactId>
    33         dependency>
    34 
    35         <dependency>
    36             <groupId>com.hdgroupId>
    37             <artifactId>test-hd-resourceartifactId>
    38         dependency>
    39 
    40         
    41         <dependency>
    42             <groupId>javax.servletgroupId>
    43             <artifactId>jstlartifactId>
    44         dependency>
    45 
    46         <dependency>
    47             <groupId>taglibsgroupId>
    48             <artifactId>standardartifactId>
    49         dependency>
    50 
    51         <dependency>
    52             <groupId>junitgroupId>
    53             <artifactId>junitartifactId>
    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.0modelVersion>
     6     <parent>
     7         <groupId>com.hdgroupId>
     8         <artifactId>test-hd-modulesartifactId>
     9         <version>0.0.1-SNAPSHOTversion>
    10     parent>
    11     <artifactId>test-hd-wwwartifactId>
    12     <packaging>warpackaging>
    13 
    14     <build>
    15         <plugins>
    16             
    17             <plugin>
    18                 <groupId>org.apache.maven.pluginsgroupId>
    19                 <artifactId>maven-compiler-pluginartifactId>
    20                 <version>2.3.2version>
    21                 <configuration>
    22                     <source>1.7source>
    23                     <target>1.7target>
    24                 configuration>
    25             plugin>
    26         plugins>
    27         <finalName>test-hd-wwwfinalName>
    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包

    


你可能感兴趣的:(java)