Maven 实战 - 使用Maven构建多模块项目

多模块实际案例

project
    |--business  (核心业务)
        |--business-api
        |--business-service
        |--business-message
        |--business-dao |--business-web |--common (公共组件、服务、常量) |--common-component |--common-component-... |--common-service |--common-constants |--common-... |--management (管理台) |--management-... |--taskserver (定时任务、批处理) |--msgserver (消息队列)

示例一

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

    

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

示例二

在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层)、dao(数据库访问层)、service(业务逻辑层)、web(表现层),这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易,今天我们就是使用Maven来构建以上的各个层。

  项目结构如下:

  system-parent         |----pom.xml         |----system-domain                 |----pom.xml         |----system-dao                 |----pom.xml         |----system-service                 |----pom.xml         |----system-web                 |----pom.xml

一、创建system-parent项目

  创建system-parent,用来给各个子模块继承。

  进入命令行,输入以下命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在当前目录(C:\Documents and Settings\Administrator)生成了system-parent目录,里面有一个src目录和一个pom.xml文件,如下图所示:

  

  将src文件夹删除,然后修改pom.xml文件,将<packaging>jar</packaging>修改为<packaging>pom</packaging>,pom表示它是一个被继承的模块,修改后的内容如下:

复制代码
 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  5 <groupId>me.gacl</groupId>  6 <artifactId>system-parent</artifactId>  7 <version>1.0-SNAPSHOT</version>  8 <packaging>pom</packaging>  9 10 <name>system-parent</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>3.8.1</version> 22 <scope>test</scope> 23 </dependency> 24 </dependencies> 25 </project>
复制代码

二、创建sytem-domain模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-domain,里面包含src目录和pom.xml文件。如下图所示:

  

  

  同时,在system-parent目录中的pom.xml文件自动添加了如下内容:

<modules>
    <module>system-domain</module> </modules>

  这时,system-parent的pom.xml文件如下:

复制代码
 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" 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  5 <groupId>me.gacl</groupId>  6 <artifactId>system-parent</artifactId>  7 <version>1.0-SNAPSHOT</version>  8 <packaging>pom</packaging>  9 10 <name>system-parent</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>3.8.1</version> 22 <scope>test</scope> 23 </dependency> 24 </dependencies> 25 <modules> 26 <module>system-domain</module> 27 </modules> 28 </project>
复制代码

  修改system-domain目录中的pom.xml文件,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar

  修改过后的pom.xml文件如下:

复制代码
 1 <?xml version="1.0"?>
 2 <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"  3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  4 <modelVersion>4.0.0</modelVersion>  5 <parent>  6 <groupId>me.gacl</groupId>  7 <artifactId>system-parent</artifactId>  8 <version>1.0-SNAPSHOT</version>  9 </parent> 10 11 <artifactId>system-domain</artifactId> 12 <packaging>jar</packaging> 13 14 <name>system-domain</name> 15 <url>http://maven.apache.org</url> 16 </project>
复制代码

三、创建sytem-dao模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-dao,里面包含src目录和pom.xml文件。如下图所示:

  

  同时,在system-parent目录中的pom.xml文件自动变成如下内容:

复制代码
 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" 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  5 <groupId>me.gacl</groupId>  6 <artifactId>system-parent</artifactId>  7 <version>1.0-SNAPSHOT</version>  8 <packaging>pom</packaging>  9 10 <name>system-parent</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>3.8.1</version> 22 <scope>test</scope> 23 </dependency> 24 </dependencies> 25 <modules> 26 <module>system-domain</module> 27 <module>system-dao</module> 28 </modules> 29 </project>
复制代码

  修改system-dao目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时添加对system-domain模块的依赖,修改后的内容如下:

复制代码
 1 <?xml version="1.0"?>
 2 <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"  3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  4 <modelVersion>4.0.0</modelVersion>  5 <parent>  6 <groupId>me.gacl</groupId>  7 <artifactId>system-parent</artifactId>  8 <version>1.0-SNAPSHOT</version>  9 </parent> 10 11 <artifactId>system-dao</artifactId> 12 <packaging>jar</packaging> 13 14 <name>system-dao</name> 15 <url>http://maven.apache.org</url> 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 </properties> 19 <dependencies> 20 <!--system-dao需要使用到system-domain中的类,所以需要添加对system-domain模块的依赖--> 21 <dependency> 22 <groupId>me.gacl</groupId> 23 <artifactId>system-domain</artifactId> 24 <version>${project.version}</version> 25 </dependency> 26 </dependencies> 27 </project>
复制代码

四、创建system-service模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-service,里面包含src目录和pom.xml文件。如下图所示:

  

  同时,在system-parent目录中的pom.xml文件自动变成如下内容:

复制代码
 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" 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  5 <groupId>me.gacl</groupId>  6 <artifactId>system-parent</artifactId>  7 <version>1.0-SNAPSHOT</version>  8 <packaging>pom</packaging>  9 10 <name>system-parent</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>3.8.1</version> 22 <scope>test</scope> 23 </dependency> 24 </dependencies> 25 <modules> 26 <module>system-domain</module> 27 <module>system-dao</module> 28 <module>system-service</module> 29 </modules> 30 </project>
复制代码

  修改system-service目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时添加对system-dao模块的依赖system-service依赖system-dao和system-domain,但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain。修改后的内容如下:

复制代码
 1 <?xml version="1.0"?>
 2 <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"  3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  4 <modelVersion>4.0.0</modelVersion>  5 <parent>  6 <groupId>me.gacl</groupId>  7 <artifactId>system-parent</artifactId>  8 <version>1.0-SNAPSHOT</version>  9 </parent> 10 11 <artifactId>system-service</artifactId> 12 <packaging>jar</packaging> 13 14 <name>system-service</name> 15 <url>http://maven.apache.org</url> 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 </properties> 19 <dependencies> 20 <!-- 21  system-service依赖system-dao和system-domain, 22  但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain 23 --> 24 <dependency> 25 <groupId>me.gacl</groupId> 26 <artifactId>system-dao</artifactId> 27 <version>${project.version}</version> 28 </dependency> 29 </dependencies> 30 </project>
复制代码

五、创建system-web模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-web,里面包含src目录和pom.xml文件。如下图所示:

  

  在\system-web\src\main\webapp目录中还生成了一个简单的index.jsp,如下图所示:

  

  里面的内容为

<html>
<body> <h2>Hello World!</h2> </body> </html>

  system-web\src\main\webapp\WEB-INF目录中生成了web.xml

  

  同时,在system-parent目录中的pom.xml文件自动变成如下内容:

复制代码
 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" 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  5 <groupId>me.gacl</groupId>  6 <artifactId>system-parent</artifactId>  7 <version>1.0-SNAPSHOT</version>  8 <packaging>pom</packaging>  9 10 <name>system-parent</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>3.8.1</version> 22 <scope>test</scope> 23 </dependency> 24 </dependencies> 25 <modules> 26 <module>system-domain</module> 27 <module>system-dao</module> 28 <module>system-service</module> 29 <module>system-web</module> 30 </modules> 31 </project>
复制代码

  修改system-web目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,因为groupId和version会继承system-parent中的groupId和version,同时添加对system-service模块的依赖修改后的内容如下:

复制代码
 1 <?xml version="1.0"?>
 2 <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"  3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  4 <modelVersion>4.0.0</modelVersion>  5 <parent>  6 <groupId>me.gacl</groupId>  7 <artifactId>system-parent</artifactId>  8 <version>1.0-SNAPSHOT</version>  9 </parent> 10 11 <artifactId>system-web</artifactId> 12 <packaging>war</packaging> 13 14 <name>system-web Maven Webapp</name> 15 <url>http://maven.apache.org</url> 16 <dependencies> 17 <!-- 18  system-web依赖system-service 19 --> 20 <dependency> 21 <groupId>me.gacl</groupId> 22 <artifactId>system-service</artifactId> 23 <version>${project.version}</version> 24 </dependency> 25 </dependencies> 26 <build> 27 <finalName>system-web</finalName> 28 </build> 29 </project>
复制代码

   注意,web项目的打包方式是war

六、编译运行项目

  经过上面的五个步骤,相关的模块全部创建完成,怎么运行起来呢。由于最终运行的是system-web模块,所以我们对该模块添加jetty支持,方便测试运行。修改system-web项目的pom.xml如下:

复制代码
 1 <?xml version="1.0"?>
 2 <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"  3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  4 <modelVersion>4.0.0</modelVersion>  5 <parent>  6 <groupId>me.gacl</groupId>  7 <artifactId>system-parent</artifactId>  8 <version>1.0-SNAPSHOT</version>  9 </parent> 10 11 <artifactId>system-web</artifactId> 12 <packaging>war</packaging> 13 14 <name>system-web Maven Webapp</name> 15 <url>http://maven.apache.org</url> 16 <dependencies> 17 <!-- 18  system-web依赖system-service 19 --> 20 <dependency> 21 <groupId>me.gacl</groupId> 22 <artifactId>system-service</artifactId> 23 <version>${project.version}</version> 24 </dependency> 25 </dependencies> 26 <build> 27 <finalName>system-web</finalName> 28 <plugins> 29 <!--配置Jetty插件--> 30 <plugin> 31 <groupId>org.mortbay.jetty</groupId> 32 <artifactId>maven-jetty-plugin</artifactId> 33 </plugin> 34 </plugins> 35 </build> 36 </project>
复制代码

  在命令行进入system-parent目录,然后执行下列命令:

mvn clean install

  如下图所示:

  

  

  命令执行完后,在system-web目录下多出了target目录,里面有了system-web.war,如下图所示:

  

  命令行进入sytem-web目录,执行如下命令,启动jetty

mvn jetty:run

  如下图所示:

  

  

  启动jetty服务器后,访问http://localhost:8080/system-web/ 运行结果如下图所示:

  

七、导入Eclipse中进行开发

  操作步骤如下所示:

  

  

  

  

  

转:https://www.cnblogs.com/xdp-gacl/p/4242221.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项目

    

    

配置个模块的依赖

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

    

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

猜你喜欢

转载自www.cnblogs.com/jiangtao1218/p/10012802.html