Maven —— dependencies与dependencyManagement的区别
文章目录
问题
pom.xml配置文件中的dependencies与dependencyManagement有什么区别?在使用上有什么注意要点呢?
解决
dependencyManagement主要用来控制所有子项目依赖的版本号。
为了项目的正确运行,必须让所有的子项目使用依赖项的统一版本,必须确保应用的各个项目的依赖项和版本一致,才能保证测试的和发布的是相同的结果。
dependencyManagement中依赖的版本号,是当前所有子项目依赖的默认版本号。
如果dependencies与dependencyManagement中的依赖版本号不同,则选用dependencies的依赖版本号。
扩展
如果dependencyManagement中有很多依赖,在修改某一个依赖的版本号时会产生查找不便的问题。
可以使用<properties></properties>以变量的形式将版本号聚集在一起,便于后续维护。
代码示例
1. <properties>
2. <commons.httpclient>3.1</commons.httpclient>
3. </properties>
5. <dependencies>
6. <!--httpclient-->
7. <dependency>
8. <groupId>commons-httpclient</groupId>
9. <artifactId>commons-httpclient</artifactId>
10. </dependency>
11. </dependencies>
13. <dependencyManagement>
14. <!--httpclient-->
15. <dependency>
16. <groupId>commons-httpclient</groupId>
17. <artifactId>commons-httpclient</artifactId>
18. <version>${commons.httpclient}</version>
19. </dependency>
20. </dependencyManagement>
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论