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)

     <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>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
    <module>test-hd-api</module>
    <module>test-hd-service</module>
    <module>test-hd-resource</module>
    <module>test-hd-foundation</module>
    <module>test-hd-modules</module>
    </modules> <!-- maven依赖 -->
    <dependencyManagement> <dependencies>
    <!-- hd -->
    <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency> <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency> <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-resource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency> <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-foundation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency> <!-- Servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
    </dependency> <!-- jstl -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency> <dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
    </dependency> <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency> </dependencies>
    </dependencyManagement> </project>
  • test-hd-foundation中的依赖
     <?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-foundation</artifactId> <dependencies> <!-- 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>
    </build>
    </project>
  • 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>
  • test-hd-service中的依赖关系
     <?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-service</artifactId>
    <dependencies> <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-foundation</artifactId>
    </dependency> <dependency>
    <groupId>com.hd</groupId>
    <artifactId>test-hd-api</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-service</finalName>
    </build>
    </project>
  •   test-hd-module中的依赖关系
     <?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>
  • test-hd-www中的依赖关系
     <?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-modules</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>test-hd-www</artifactId>
    <packaging>war</packaging> <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-www</finalName>
    </build> </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包

    

    

    

  

【Maven】使用Maven构建多模块项目的更多相关文章

  1. 使用Maven构建多模块项目

    [转] 使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层).dao(数据库访问层).service(业务 ...

  2. maven 单独构建多模块项目中的单个模块

    maven 单独构建多模块项目中的单个模块, maven选项说明 -pl, --projects Build specified reactor projects instead of all pro ...

  3. Maven学习(五)使用Maven构建多模块项目

    使用Maven构建多模块项目 一般的web项目构成: 建立解决方案目录parent 首先使用命令进入到我们需要建立maven项目的目录: mvn archetype:generate -DgroupI ...

  4. Maven单独构建多模块项目中的单个模块

    Maven单独构建多模块项目中的单个模块   说明: 1.可能存在的场景,多模块项目没有互相引用,那么此时可以单独构建单个项目,指定到子模块的pom.xml文件即可完成编译. 2.如果多模块项目各自都 ...

  5. (转)Maven学习总结(八)——使用Maven构建多模块项目

    孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(八)——使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为doma ...

  6. Maven构建多模块项目

    使用Maven构建多模块项目 转自:http://www.cnblogs.com/xdp-gacl/p/4242221.html 在平时的Javaweb项目 开发中为了便于后期的维护,我们一般会进行分 ...

  7. Maven学习总结(8)——使用Maven构建多模块项目

    Maven学习总结(八)--使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层).dao(数据库访问层). ...

  8. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  9. Maven 搭建spring boot多模块项目

    Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...

  10. IDEA + maven 零基础构建 java agent 项目

    200316-IDEA + maven 零基础构建 java agent 项目 Java Agent(java 探针)虽说在 jdk1.5 之后就有了,但是对于绝大多数的业务开发 javaer 来说, ...

随机推荐

  1. Maven依赖排除 禁止依赖传递 取消依赖的方法

    大家都知道Maven的优点是依赖管理,特别是前期使用ANT的开发者都有很多感触.最近要开发一个java工程,定的要使用maven,会使用hadoop和hbase的客户端,而引入一个hadoop-cli ...

  2. Blackfin DSP(六):BF533的SPORT接口

    1.特性 bf533有两个SPORT口(synchronous serial Port),即同步串行接口.完全独立的接收和发送通道,且每个通道都具有缓冲,最高速度可达SCLK/2.最大支持32bit字 ...

  3. C++标准转换运算符

    C++类型转换在实际编程中会经常使用,其实,本质上对象的类型用来解释(interpret)对象.因为,每个对象都占据一块内存空间,这块内存空间存放了一段二进制数据.通过标记该对象的类型,告诉如何看待这 ...

  4. mysql中中文乱码问题

    作用:约束用来保证数据有效性和完整性 . 定义主键约束 主键约束 primary key : 信息记录某个字段可以唯一区分其他信息记录,这个字段就可以是主键 (唯一 非空)   primary key ...

  5. 浅谈js的键值对key和value

    > 昨晚无意中看到类似下面结构的一段代码的取值问题,引起我的兴趣,花了点时间写了个demo给大家分享一下... var obj = [ {"2011":{"name ...

  6. java笔记1

    第五天学习笔记 1.面对对象的理解并举例? 面对对象的核心:找合适的对象做合适的事情. 面对对象编程的思想:尽可能的用计算机语言来描述现实生活中的事物. 面对对象:侧重于对象 2.类与对象之间的关系? ...

  7. 初识Memcached

    一,什么是memcached? Memcached是一个高性能的分布式内存对象缓存系统,用于动态web应用以减轻数据库负载..它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱 ...

  8. vbs获取命令行里的参数

    var args1=WScript.Arguments.Item(0) var args2=WScript.Arguments.Item(1)

  9. linux配置的问题

    1 从系统设置-文本设置中把双拼删掉 2 通过sudo passwd root 修改root密码 3 通过su获取root权限 4 通过sudo pppoeconf输入宽带帐号密码 5 把更新源修改成 ...

  10. linq 延迟执行带来的困扰

    有这样一个案例: var filteredResult = from f in orgFileList select f; ; i < WorkStatusFilters.ListWorkSta ...