详解IDEA多module项目maven依赖的一些说明
作者:sandman 时间:2023-07-21 06:09:05
不管eclipse有没有被被时代抛弃,反正是被我抛弃了,因为IDEA是真的好用
现在公司的项目基本都是基于maven的多module项目,controller,service,model,dao等都被分成了不同的module,这样做当然也是为了解耦。
这些module可根据需要在pom.xml配置来打成war包或者jar包
<packaging>jar</packaging>
web主项目设置packaging级别为war,dao、model这些module设置packaging级别为jar。
module之间可以通过module自己的pom.xml来进行相互引用或依赖,如:
<dependency>
<groupId>cn.com.autohome.mall</groupId>
<artifactId>mall-common</artifactId>
</dependency>
<dependency>
<groupId>cn.com.autohome.mall</groupId>
<artifactId>mall-api-model</artifactId>
</dependency>
这样在 File -> project structure 下,选中主web项目
从上面的截图可以看出来依赖的第三方jar和依赖项目子module的区别。
maven在执行install,packaging是jar的会被打成jar放在target目录下,packaging是war的会被打成war放在target目录下。
另外两个target目录会有一点区别,war的target目录会多出来一个和module同名的文件夹,这个文件夹和war解压后完全一致。
所有依赖的jar(包括依赖的module,被打成jar)都会被放lib下
这样在部署的时候,只需要部署相应的war即可。
关于Maven pom.xml中的元素modules、parent、properties以及import
多个module不需要分别执行mvn命令,可以使用聚合(aggregator)来一次构建全部模块
modules
在父pom.xml中通过
<modules>
<!-- 模块都写在此处 -->
<module>mall-common</module>
<module>mall-api-model</module>
</modules>
来引用所有需要构建的子模块
parent
继承,和java中的继承相当,作用就是复用
场景
若每个子模块都都用的了spring,那么我们是不是每个子模块都需要单独配置spring依赖了?这么做是可以的,但是我们有更优的做法,那就是继承,用parent来实现。
实现
父(account-aggregator)pom.xml
<modules>
<!-- 模块都写在此处 -->
<module>mall-common</module>
<module>mall-api-model</module>
</modules>
<dependencies> <!-- 配置共有依赖 -->
<!-- spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
······
<!-- junit 依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
子pom.xml
<parent>
<groupId>xx.xx.xx</groupId>
<artifactId>aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- 与不配置一样,默认就是寻找上级目录下得pom.xml -->
</parent>
<dependencies> <!-- 配置自己独有依赖 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
依赖管理
继承可以消除重复,那是不是就没有问题了? 答案是存在问题,假设将来需要添加一个新的子模块util,只是提供一些简单的帮助工具,不需要依赖spring、junit,那么继承后就依赖上了,有没有什么办法了?
有,maven已经替我们想到了,那就是dependencyManagement元素,既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。在dependencyManagement元素下得依赖声明不会引入实际的依赖,不过它能够约束dependencies下的依赖使用。
在父pom.xml中配置dependencyManagement元素
<modules>
<!-- 模块都写在此处 -->
<module>mall-common</module>
<module>mall-api-model</module>
</modules>
<dependencyManagement>
<dependencies> <!-- 配置共有依赖 -->
<!-- spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
······
</dependencies>
</dependencyManagement>
子pom.xml
<dependencies>
<!-- spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- junit 依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16</version>
</dependency>
</dependencies>
使用这种依赖管理机制似乎不能减少太多的POM配置,就少了version(junit还少了个scope),感觉没啥作用呀;其实作用还是挺大的,父POM使用dependencyManagement能够统一项目范围中依赖的版本,当依赖版本在父POM中声明后,子模块在使用依赖的时候就无须声明版本,也就不会发生多个子模块使用版本不一致的情况,帮助降低依赖冲突的几率。如果子模块不声明依赖的使用,即使该依赖在父POM中的dependencyManagement中声明了,也不会产生任何效果。
来源:https://segmentfault.com/a/1190000016778269
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java httpClient介绍以及使用示例
Java基础之练习打印三角形
![](https://img.aspxhome.com/file/2023/7/58187_0s.png)
详细聊聊SpringBoot中动态切换数据源的方法
![](https://img.aspxhome.com/file/2023/9/59879_0s.png)
SSM框架搭建图文教程(推荐)
![](https://img.aspxhome.com/file/2023/1/59061_0s.png)
springboot集成RestTemplate及常见的用法说明
netty pipeline中的inbound和outbound事件传播分析
![](https://img.aspxhome.com/file/2023/5/58285_0s.png)
spring中bean的生命周期详解
详解IDEA启动多个微服务的配置方法
![](https://img.aspxhome.com/file/2023/0/59660_0s.png)
android开发仿ios的UIScrollView实例代码
java实现文件下载的两种方式
Java SpringBoot的相关知识点详解
![](https://img.aspxhome.com/file/2023/0/58620_0s.png)
java面试题——详解HashMap和Hashtable 的区别
java动态绑定和静态绑定用法实例详解
Spring容器的创建过程之如何注册BeanPostProcessor详解
![](https://img.aspxhome.com/file/2023/2/65172_0s.png)
解决idea web 配置相对路径问题
![](https://img.aspxhome.com/file/2023/9/62729_0s.png)
解析android中的dip,dp,px,sp和屏幕密度
详解Java并发包中线程池ThreadPoolExecutor
![](https://img.aspxhome.com/file/2023/9/64389_0s.png)
java图形界面编程之模拟血压计
完美解决springboot中使用mybatis字段不能进行自动映射的问题
![](https://img.aspxhome.com/file/2023/7/61157_0s.png)
java实现单机版五子棋
![](https://img.aspxhome.com/file/2023/9/63379_0s.jpg)