maven多模块合并打包_Java实战开发(三)丨 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工程

169f651c1247b35626641dc17c469172.png

8e60802a2e37f3b6bcac7795a2776e82.png
  • 输入Group Id、Artifact Id、Packaging,packaging选择pom包

a495ebe8a89bc9784686c1b9a0428474.png
  • 生成父工程,pom.xml如下

3ef6bd32bdbdb74eddc7860f9925e82c.png
  • 删除工程中的src 目录

a4ab60c4bbf6b549851c813a1184a7b9.png

创建子模块

  • 右击父工程名---》New---》Project,然后选择新建一个maven module工程

b1ad2eac5b2ec3ce90a601d586345942.png

c75271deca3a96313339df527d23d60d.png
  • 设置子工程名以及父工程,再设置快速创建模式

bf4fdda77f30c6b835bec070f376803a.png

755fd8eee4e520c7566a3e4bb10ea088.png
  • 得到子工程(test-hd-api,第三方接口层),设置编译的jdk

e46c9bb513370b387993e7c069bb7f0b.png

587f942831eb02f11abf17f22418250e.png
  • 同理设置,子模块:test-hd-foundation(基础工具层)、test-hd-resource(资源层) 、test-hd-service(逻辑业务层)
  • 新建test-hd-modules (web层),选择创建一个a simple project,输入Group Id、Artifact Id、Packaging,packaging选择pom包

f45a7c2c37ef5c3e92fabedab69f787e.png

4fce604e8960c2bd728e56d39b66a8c0.png

创建web子模块

  • web子模块在建在test-hd-modules (web层)里面,右击test-hd-modules 工程名---》New---》Project,然后选择新建一个maven module工程,设置子工程名以及父工程,选择新建web项目

28c63fb804b6976e9161e41f380e12a1.png
  • 配置maven web项目,参照:【Maven】Eclipse 使用Maven创建Java Web项目
  • 同理可以配置其他的web子模块 test-hd-admin(web模块2)

5f000d7cb553ec41cd8f090ca094080b.png

配置个模块的依赖

  • 在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

20b63b044a7cfa4b54ff85d63dfd5405.png

打包和发布

  • 打包,右击父工程名 test-hd-parent---->Run As--->Maven Install

11ce52695bc9f6304da306abd1362ca6.png

2cdc72760aeac09656c4102cac9aa022.png
  • 打包web子工程,右击工程名test-hd-www--->Run As ---> Maven Build...---> Goals: clean package--->Run

27e2c17f714d392cccfe003c723c1dd3.png

4eb052894987dd56d7d80ee36dc7d1bb.png

98cc553d93c2e6763bd0b004e4a7975e.png
  • 右击工程名test-hd-www,进行刷新,找到war包,放到tomcat的webapps中,启动tomcat,即可访问工程http://localhost:8080/test-hd-www

2079cb876c6f40af2fdc81c1b7d95fbd.png
  • 可以去tomcat下面webapps》test-hd-www》WEB-INF》lib中,看到引用的jar包

61b44765ab60ca311c8bd6258431c2b9.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值