Maven:如果依赖项已经是可传递的,如何删除它们?

发布于 2024-09-25 09:00:03 字数 602 浏览 1 评论 0原文

例如,如果存在依赖关系:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沙沙粒小 2024-10-02 09:00:03

使用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 文件。上面的命令应该使您能够确定适当的下一步操作。

编辑:好的,您更新了您的问题。操作方法如下:

  1. 在项目 a 中,运行 mvn dependency:tree -Dverbose=true。这将向您显示 Maven 为您的项目考虑的所有依赖项的完整树。
  2. 查看步骤 1 的输出,并列出以超过一层深度显示的所有依赖项(其中一些可能是重复的)。
  3. 在您喜欢的任何编辑器中编辑您的 pom.xml 文件,并删除与您在步骤 2 中创建的列表匹配的任何依赖项。

或者您是否正在寻找一种自动执行此操作的方法?我不认为有任何自动化的方法,除非你自己写一个,因为你试图做的是一个坏主意。你告诉人们他们的反对意见与你的问题“无关”,但事实是你的问题就像在问“我如何使用 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 on c (that is if code in a mentions classes in c), then the pom should reflect it. If a only depends directly on b, then you can safely remove the c dependency from a'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:

  1. in project a , run mvn dependency:tree -Dverbose=true. This will show you a complete tree of all dependencies considered by Maven for your project.
  2. Look at the output of step 1 and make a list of all dependencies that are shown at more than one level deep (some of them will probably be duplicates).
  3. Edit your pom.xml file in whatever editor you like and remove any dependencies that match the list you created in step 2.

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.

病毒体 2024-10-02 09:00:03

我假设您想要找到已经满足的虚假/不必要的依赖项,因为您可以从另一个依赖项免费获取它们。

我可以想象你可能想这样做是为了清理你的 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 removes c as dependency and thus breaks a

豆芽 2024-10-02 09:00:03

和其他海报一样,我不太确定你想要实现什么。也许排除是您所需要的?您可以使用排除来删除依赖项的依赖项 - 如果由于某些原因不需要它们。

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>logkit</groupId>
                    <artifactId>logkit</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>avalon-framework</groupId>
                    <artifactId>avalon-framework</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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.

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>logkit</groupId>
                    <artifactId>logkit</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>avalon-framework</groupId>
                    <artifactId>avalon-framework</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文