关于使用maven打包war时多个依赖jar版本的最佳实践的问题
我有一个带有子模块的 Maven War 项目。一个模块使用 google-api-client
,另一个模块使用 google-cloud-storage
。下面我画了他们的一些依赖关系
A
|-google-api-client:jar:1.33.1
|-google-http-client-gson:jar:1.41.1
B
|-google-cloud-storage:jar:2.4.4
|-google-api-client:jar:1.33.1
|-google-http-client-gson:jar:1.41.2
,当打包战争时,gson 1.41.1
和1.41.2
都会被打包。我知道maven有一个最接近的规则来确定编译时使用哪个jar。但是当网络服务器加载我的项目时,我无法控制首先加载哪个 jar。所以我想为每个罐子只保留一个较新的版本。
我知道我可以向依赖项添加
标签,并添加一个新的依赖项来告诉 maven 使用特定版本的 jars。但是,我不确定这是否是最佳实践,因为它需要我检查第三方库的依赖关系。他们的数量太多了。
关于如何正确处理多个版本的 jar 有什么建议吗?
I have a maven war project with submodules. One module uses google-api-client
, another use google-cloud-storage
. I draw some of their dependencies below
A
|-google-api-client:jar:1.33.1
|-google-http-client-gson:jar:1.41.1
B
|-google-cloud-storage:jar:2.4.4
|-google-api-client:jar:1.33.1
|-google-http-client-gson:jar:1.41.2
When packaging wars, both gson 1.41.1
and 1.41.2
will be packaged. I know maven has a nearest rule to determine which jar to use when compiling. But when the webserver loads my project I have no control to which jar will be loaded first. So I want to keep only a newer version for each jar.
I know that I can add <exclusion>
tags to the dependencies and add a new dependency to tell maven to use a specific version of jars. However, I am not sure if that is the best practice because it requires me to go through the dependencies of third-party libraries. There are just too many of them.
Any suggestions on how to handle the multiple versions of jars properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议的一个好习惯是使用具有依赖收敛目标的enforcer插件 。这样您就必须决定哪个版本将出现在类路径上。当然,这可能需要额外的工作,因为您必须处理冲突(也可以通过设置排除),但最终它是明确定义的,您将获得哪些版本。
A good practice I recommend is to use enforcer Plugin with dependency convergence goal. This way you are forced to decide which version will be on the class path. Of course it might be additional effort because you have to handle conflicts (also by setting exclusions), but in the end it's well defined, which versions you get.