• 周五. 12月 9th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Maven(10)< dependencyManagement> Use of

[db:作者]

1月 6, 2022

One :dependencyManagement The role of
      stay Maven in ,< dependencyManagement >` Tags provide a unified way to manage dependent version numbers . Usually used at the top level of the project’s parent POM in .

      Use < dependencyManagement >` Tag defines the dependent version number , Can make all of the sub projects pom.xml You don’t need to declare the version number explicitly . When no version number is declared in a subproject ,Maven Will go up the father and son level , Until you find someone who has dependencyManagement Tagged items , Then use the version number specified in the tag .

For example, in the parent project’s pom.xml The following definitions are given in :

<!-- Unified management jar Package version -->
<properties>
<mysql.version>5.1.47</mysql.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

  In a submodule or subproject , You don’t have to write the version number :

 <dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

The advantage of this is that if multiple subprojects refer to the same dependency , You can avoid declaring the version number many times . In addition, if you need to change the version number , It’s directly in the parent project’s pom.xml Just change it in , It doesn’t need to be changed one by one .
Be careful :

If a version number is declared in a subproject , Then the version specified in the subproject is used .
If you don’t declare dependencies in subprojects , Is not inherited from the parent project . Only the dependency is written in the subproject , And no specific version number is specified , Will inherit the dependency from the parent project . among version and scope All from pom
The last thing to note is this dependencyManagement It’s just a declaration of dependency , Instead of introducing . So before the announcement , Make sure that the dependency of the corresponding version has been downloaded to the local repository .

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注