Maven分模块以及打war包


我们如何进行模块化开发呢?

我们使用上面的例子进行演示,先进行合理的优化,我们希望dao和service作为通用的底层工具来使用,把它们合并成一个核心模块(core),build成core.jar,简单的Maven模块化项目结构如下:

---------- mall //顶级项目 |------ pom.xml //packaging = pom |------ mall-util //通用工具 | |--- pom.xml //packaging = jar |------ mall-core //核心模块 | |--- pom.xml //packaging = jar |------ mall-web-api //接口模块 | |--- pom.xml //packaging = war |------ mall-web-admin//管理后台 | |--- pom.xml //packaging = war |------ mall-web-shop//商城前台 | |--- pom.xml //packaging = war

这些模块中api、admin、shop均是可以单独部署的web应用,相互之间没有依赖关系,但都依赖于core模块,而core模块依赖于util模块。接下来我们按照上述确定的结构来搭建项目结构。

使用IDEA来创建Maven多模块项目

一、创建一个普通Maven项目

  1. New Project

image

  1. 填写基本信息,这里使用ipr作为项目描述文件

image

  1. 普通Maven项目不需要使用Maven模板搭建

image

二、给Maven项目添加模块

  1. New Module

image

  1. 填写基本信息,jar项目同样不需要使用Maven模板搭建

image

  1. 这个时候就可以看到,我们所添加的module已经被引入到parent的pom文件里了
<groupId>com.mall</groupId> <artifactId>mall</artifactId> <packaging>pom</packaging> //打包方式为pom <version>1.0-SNAPSHOT</version> <modules> <module>mall-util</module> </modules>
  1. 变更util模块的构建方式为jar
<parent> <artifactId>mall</artifactId> <groupId>com.mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <packaging>jar</packaging> //打包方式为jar <artifactId>mall-util</artifactId>

三、给Maven项目添加web模块

  1. 创建一个module,并选中“Create from archetype”选项,同时maven模板选择webapp

image

  1. 接下来耐心的等待maven帮你创建好module,模块信息已经被添加
<modules>
    <module>mall-util</module> <module>mall-web-admin</module> </modules>

目录结构如下:

image

pom:

<parent> <artifactId>mall</artifactId> <groupId>com.mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mall-web-admin</artifactId> <packaging>war</packaging> <name>mall-web-admin</name> <url>https://github.com/beiyoufx</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>mall-web-admin</finalName> </build>

四、添加模块间的依赖关系

  1. 增加core与util的依赖

image

  1. 增加admin与core的依赖关系

image

admin与core、util的依赖链

image

多模块项目的构建与发布

打包

image

所有在root项目中进行的构建都会传递到模块中,例如root中的package会打包整个项目,当文件有变动时会进行重新聚合,其他命令同理。模块中的package只会打包当前模块。

使用source:jar命令会将源码打包。

clean install 打出war包和源码。

 

使用命令打包方法:

在Debug Configurations 参数 Goals 写命令参数  DEBUG运行~

 

 

打包注意要点:eclipse工具

但是maven插件需要使用jdk,因此需要在eclipse修改Installed JRES
位置在-->【Window】-->【Prefrences】-->【Java】-->【Installed JREs】
详见下图。

 

 

 打完包后直接将war包扔到webapps里就可以了

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM