Maven:如果依赖项已经是可传递的,如何删除它们?
例如,如果存在依赖关系:
a -> b
a -> c
b -> c
我想删除依赖关系 a -> c
,因为有a ->; b-> c
.
我知道可能存在一些不应该减少的强烈依赖性,但这与这个问题无关。
示例:
In a.pom:
<dependencies>
<dependency>b</dependency>
<dependency>c</dependency>
</dependencies>
In b.pom:
<dependencies>
<dependency>c</dependency>
</dependencies>
预期结果:
In a.pom:
<dependencies>
<dependency>b</dependency>
</dependencies>
For example, if there are dependencies:
a -> b
a -> c
b -> c
I want to remove the dependency a -> c
, because there is a -> b -> c
.
I know there may be some strong dependencies which should not be reduced, but it's not relevant to this question.
Example:
In a.pom:
<dependencies>
<dependency>b</dependency>
<dependency>c</dependency>
</dependencies>
In b.pom:
<dependencies>
<dependency>c</dependency>
</dependencies>
Expected result:
In a.pom:
<dependencies>
<dependency>b</dependency>
</dependencies>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
mvn dependency:analyze
来显示您的pom中是否存在您不需要的依赖项(它也可能识别出一些您错过的依赖项,添加-DoutputXML=true
显示缺失的条目)。使用
mvn dependency:tree
显示项目当前使用的依赖项以及 Maven 在哪里找到它们。添加-Dverbose=true
以显示所有重复项和冲突。如果
a
直接依赖于c
(即a
中的代码提及c
中的类) code>),那么 pom 应该反映它。如果a
仅直接依赖于b
,那么您可以安全地从a
的 pom.xml 中删除c
依赖项。 xml 文件。上面的命令应该使您能够确定适当的下一步操作。编辑:好的,您更新了您的问题。操作方法如下:
a
中,运行mvn dependency:tree -Dverbose=true
。这将向您显示 Maven 为您的项目考虑的所有依赖项的完整树。或者您是否正在寻找一种自动执行此操作的方法?我不认为有任何自动化的方法,除非你自己写一个,因为你试图做的是一个坏主意。你告诉人们他们的反对意见与你的问题“无关”,但事实是你的问题就像在问“我如何使用 Maven 来使使用 Maven 变得更加困难?”
您没有充分的理由想要这样做。如果你认为有充分的理由,那么你一定会努力去做以产生一些结果。您应该寻求帮助以获得所需的结果,因为您的计划很糟糕。
Use
mvn dependency:analyze
to show you whether there are dependencies in your pom that you don't need (it may also identify some that you have missed, add-DoutputXML=true
to show the missing entries).Use
mvn dependency:tree
to show you the dependencies currently in use by your project and where Maven is finding them. Add-Dverbose=true
to show all the duplicates and conflicts.If
a
directly depends onc
(that is if code ina
mentions classes inc
), then the pom should reflect it. Ifa
only depends directly onb
, then you can safely remove thec
dependency froma
's pom.xml file. The commands above should enable you to determine what is the appropriate next action.Edit: Okay, you updated your question. Here is how you do it:
a
, runmvn dependency:tree -Dverbose=true
. This will show you a complete tree of all dependencies considered by Maven for your project.Or are you looking for a way to do it automatically? I do not think there is any automated way unless you write one yourself, because what you are trying to do is a BAD IDEA. You are telling people that their objections are "irrelevant" to your question, but the fact is that your question is like asking "How can I use Maven to make it more difficult to use Maven?"
There is no good reason why you would want to do this. If you think there is a good reason, then you must be trying to do it to produce some result. You should ask for help with the desired result, because your plan is a bad one.
我假设您想要找到已经满足的虚假/不必要的依赖项,因为您可以从另一个依赖项免费获取它们。
我可以想象你可能想这样做是为了清理你的 poms。
然而,这通常不是您想要做的事情,因为明确声明您的依赖项是一个很好的做法。
您永远不知道将来的模块
b
是否会删除c
作为依赖项,从而破坏a
I assume that you want to find the spurious/unnecessary dependencies that are already met because you get them for free from another dependency.
I can imagine you might want to do that in order to cleanup your poms.
However, it's not normally something what you would like to do, since it's a good practice to explicitly state what are your dependencies.
You never know if in future module
b
removesc
as dependency and thus breaksa
和其他海报一样,我不太确定你想要实现什么。也许排除是您所需要的?您可以使用排除来删除依赖项的依赖项 - 如果由于某些原因不需要它们。
Like other posters, I'm not exactly sure what you want to achieve. May be exclusions are what you need? You can use exclusions to remove dependencies of your dependencies - if they are not desired for some reasons.