温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IDEA多module项目maven依赖的示例分析

发布时间:2021-09-03 14:57:08 来源:亿速云 阅读:394 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关IDEA多module项目maven依赖的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

不管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项目

IDEA多module项目maven依赖的示例分析

从上面的截图可以看出来依赖的第三方jar和依赖项目子module的区别。

maven在执行install,packaging是jar的会被打成jar放在target目录下,packaging是war的会被打成war放在target目录下。
另外两个target目录会有一点区别,war的target目录会多出来一个和module同名的文件夹,这个文件夹和war解压后完全一致。

IDEA多module项目maven依赖的示例分析

所有依赖的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中声明了,也不会产生任何效果。

关于“IDEA多module项目maven依赖的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI