maven管理多模块,并且用父模块管理公共依赖时的一些说明

简单聊一下maven管理公共依赖的一些技巧

当我们的项目模块很多的时候,我们使用Maven管理项目非常方便,帮助我们管理构建、文档、报告、依赖、scms、发布、分发的方法。可以方便的编译代码、进行依赖管理、管理二进制库等等。 由于我们的模块很多,所以我们又抽象了一层,抽出一个父模块来管理子项目的公共的依赖。为了项目的正确运行,必须让所有的子项目使用依赖项的统一版本,必须确保应用的各个项目的依赖项和版本一致,才能保证测试的和发布的是相同的结果。

但是现在会有两个问题,一个是大部分模块有共同依赖,把依赖放在父模块管理没问题,但是如果直接用 dependencies 管理依赖,所有生命在 dependencies 里的依赖都会自动引入,并默认被所有的子项目继承。 会造成有些没用到该依赖的子模块也 被迫 引入了某些依赖;

另一个问题是基于第一个问题而来的,那么maven是否提供这样一种能力,可以维护公共的依赖,让子模块选择性继承,让父模块只对依赖的版本进行控制;答案是有的 dependencyManagement 标签 ,可以使我们在父模块的pom文件中声明一些公共依赖,然后子模块要使用其中的依赖,只需要在自己的pom中引入相关依赖,但是不需指定版本,如果你指定了版本,则会以子模块指定的jar版本为主;

在pom文件里面的写法如下:

<!--父类声明公共依赖-->
<dependencyManagement>   
     <dependencies>  
         <dependency>  
             <groupId>org.eclipse.persistence</groupId>  
             <artifactId>org.eclipse.persistence.jpa</artifactId>  
             <version>${org.eclipse.persistence.jpa.version}</version>  //版本号 
             <scope>provided</scope>  
         </dependency>  
           
         <dependency>  
             <groupId>javax</groupId>  
             <artifactId>javaee-api</artifactId>  
             <version>${javaee-api.version}</version>  //版本号 
         </dependency>  
     </dependencies>  
 </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">
<!--继承父类-->  
	<parent>  
	   <artifactId>itoo-base-parent</artifactId>  
	   <groupId>com.tgb</groupId>  
	   <version>0.0.1-SNAPSHOT</version>  
	   <relativePath>../itoo-base-parent/pom.xml</relativePath>  
	</parent>  
      <modelVersion>4.0.0</modelVersion>  
       <artifactId>itoo-base</artifactId>  
       <packaging>ejb</packaging>  
         
       <!--依赖关系-->  
       <dependencies>  
       <dependency>  
           <groupId>javax</groupId>  
           <artifactId>javaee-api</artifactId>  
       </dependency>  
         
       <dependency>  
           <groupId>com.fasterxml.jackson.core</groupId>  
           <artifactId>jackson-annotations</artifactId>  
       </dependency>  
         
       <dependency>  
           <groupId>org.eclipse.persistence</groupId>  
           <artifactId>org.eclipse.persistence.jpa</artifactId>  
           <scope>provided</scope>  
       </dependency>  
   </dependencies>  
</project> 

注:另外还需要告知一点,在父模块中 dependencyManagement 和 dependencies是可以同时使用,也就是说你觉得在子模块还要再引入一个依赖(虽然不用指定版本),仍然很麻烦;对全部子模块都用到的依赖可以写在 dependencies中,如上所说:所有生命在 dependencies 里的依赖都会自动引入,并默认被所有的子项目继承

<!-- dependencyManagement决定了子pom.xml是否可以直接引用父pom.xml的包 -->
<dependencyManagement>
    <dependencies>
        <!-- SpringBoot的依赖配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 子模块继承全部 -->
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
</dependencies>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值