详解使用Maven构建多模块项目(图文)

所属分类: 软件编程 / java 阅读数: 24
收藏 0 赞 0 分享

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)

<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包

    

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。 

更多精彩内容其他人还在看

Java的面向对象编程基本概念学习笔记整理

这篇文章主要介绍了Java的面向对象编程基本概念学习笔记整理,包括类与方法以及多态等支持面向对象语言中的重要特点,需要的朋友可以参考下
收藏 0 赞 0 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多