Dependency Management
我们不妨先去看看官方的文档。
Besides inheriting certain top-level elements, parents have elements to configure values for child
POMs
and transitive dependencies. One of those elements isdependencyManagement
.dependencyManagement: is used by a POM to help manage dependency information across all of its children. If the
my-parent
project usesdependencyManagement
to define a dependency onjunit:junit:4.12
, then POMs inheriting from this one can set their dependency giving thegroupId
=junit
andartifactId
=junit
only and Maven will fill in theversion
set by the parent. The benefits of this method are obvious. Dependency details can be set in one central location, which propagates to all inheriting POMs.
Note that the version and scope of artifacts which are incorporated from transitive dependencies are also controlled by version specifications in a dependency management section. This can lead to unexpected consequences. Consider a case in which your project uses two dependences,dep1
anddep2
.dep2
in turn also usesdep1
, and requires a particular minimum version to function. If you then usedependencyManagement
to specify an older version,dep2
will be forced to use the older version, and fail. So, you must be careful to check the entire dependency tree to avoid this problem;mvn dependency:tree
is helpful.
这个东西是用来管理依赖统一的,当你在父级依赖中拥有一个依赖,他就可以替你在子模块中搞定他的版本,只要你的子模块groupId
和artifactId
跟父级模块名字一样即可。很明显的好处就是版本规范统一控制。之后他说了可能发生的错误,叫你小心管理好依赖树,这方面我们也不用担心太多,因为我们有idea。建议可以自己读一读原文,看英语文档确实有些费劲,好在程序员写技术文档都会用通俗易懂的词汇。
这样的版本规范控制,比较正规的项目应该都会采用,个人学习大部分都是单模块业务处理,哪怕一个多模块项目,也不外乎是三层提取,我们可以直接在父模块依赖中引入共有的,在子模块中引入自需的。当我想将所有的学习源码都托管于Github一个项目,这就导致我需要大量模块来进行分布,Maven就解决了我这一难题。
首先我们新建一个Maven的空项目,由于我们要用到多模块,我们将自带的src删除。
我们来到项目下)的pom.xml,既然用到dependencyManagement
,我们就直接上代码。这里直接使用mysql和Lombok这两个依赖作为演示。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
上面的写法使得版本号也在其中,依赖较多时不方便管理,我们养成习惯提取出来。
<properties>
<mysql.connector.version>8.0.18</mysql.connector.version>
<lombok.version>1.18.10</lombok.version>
</properties>
将原本的
<version>8.0.18</version>
修改为<version>${mysql.connector.version}</version>
<version>1.18.10</version>
修改为 <version>${lombok.version}</version>
我们更新一下Maven,会发现项目中并没有引入依赖,这就很好的说明了dependencyManagement
和dependencies
的区别,也展示出它的优越之处,子模块只需要放心使用依赖而无需考虑版本冲突问题。同时很好的解决了新手打包项目时优化不佳导致体积过大。
接下来我们去新建一个Module(模块),进入模块下的pom.xml
我们加入在父模块中的两个依赖,看看是否能在子模块中引入。按照官方文档,你只要groupId
和artifactId
跟父模块一一对应,也就是名字相同即可。
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
结果跟预期的一样,我们再去新建一个模块,试着只引入mysql。
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
值得一提的是,SpringBoot 项目中依赖不需要填写版本号原理也是如此。
版权属于:乐心湖's Blog
本文链接:https://xn2001.com/archives/389.html
声明:博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!