maven中几个标签配置记录

一、dependencyManagement标签

<dependencyManagement>标签在父工程的pom中定义,用于声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号。Maven会沿着父子层级向上寻找拥有dependencyManagement 元素的项目,然后使用它指定的版本号。
例如:
在父工程pom.xml中定义如下声明:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.2.3.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

在子类工程中,无需再声明依赖版本:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

该标签的好处是方便所有jar包版本的统一管理。
注意: dependencyManagement中定义的只是依赖的声明,并不实现引入,因此子项目需要显式的声明需要用的依赖。

二、标签的pom值

例如:

<dependencies>
  <dependency>
      <groupId>org.sonatype.mavenbook</groupId>       
      <artifactId>persistence-deps</artifactId>       
      <version>1.0</version>
     <type>pom</type>
 </dependency> 
</dependencies>

type默认的值是jar。即依赖某个jar包,以及jar包中所依赖的其他jar包,都会依赖进来。如果将type标签改为pom。那么意思就是不再依赖这个jar包本身,而是依赖这个jar包所定义的pom。将这个jar包pom中所依赖的jar都传递过来。如上面的配置,就是将persistence-deps.jar的pom里所依赖的内容,传递过来。
注意: type定义为pom,是将依赖jar包pom中<dependencies>中依赖的jar包都传递依赖过来。而 </dependencyManagement>中的内容不会弄过来。

三、标签的import值

例如:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>project-A</groupId>
        <artifactId>project-A</artifactId>
        <version>1.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      ...
    </dependencies>
  </dependencyManagement>

<scope>值写成import的意思是,将依赖jar包中pom的</dependencyManagement>内容复制一份过来,而不是继承关系。如上配置,就是将A项目的</dependencyManagement>内容复制一份到本项目,即使用A项目中定义的jar包版本。
注意: import不会将A项目具体的依赖项<dependencies>复制过来,他只是用了A项目的</dependencyManagement>,不存在传递依赖现象。
<scope>import</scope>需要和<type>pom</type>一起使用才生效,单独使用<scope>import</scope>不行。即在有<type>pom</type>的情况下,如果配置了<scope>import</scope>,那么就是复制对方的<dependencyManagement>内容过来。如果没有<scope>import</scope>,就是将对方的<depencies>内容传递依赖过来。
补充: </dependencyManagement>下的<dependencies>只是用于声明版本的。如果想依赖jar包,需要在单独的<dependencies>中定义jar包声明。

四、SpringBoot项目配置

在使用springboot时,通常工程有自己的父模块,而不能继承spring-boot-starter-parent时,文档推荐配置

<dependencyManagement>
	<dependencies>
		<dependency>
			<!-- Import dependency management from Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.12.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

我们看到,这里多了一个import,它的意思是将spring-boot-dependencies 中dependencyManagement的dependencies,全部引入到当前工程的dependencyManagement中。import只能用在dependencyManagement块中,它将spring-boot-dependencies 中dependencyManagement下的dependencies插入到当前工程的dependencyManagement中,所以不存在依赖传递。
当没有<scope>import</scope>时,意思是将spring-boot-dependencies 的dependencies全部插入到当前工程的dependencies中,并且会依赖传递。

参考文章:maven pom类型,有import和没有import的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的小小酥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值