如何排除 Maven 插件的直接依赖关系

2022-01-19 00:00:00 java maven jaxb

我想排除 Maven 插件的直接依赖,并且 this answer 中描述的方法不起作用(如此评论).

I want to exclude a direct dependency of a Maven plugin and the approach described in this answer does not work (as indicated by this comment).

举个例子:

<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.2</version>
            <!-- more config -->
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.13.2</version>
                    <exclusions>
                        <exclusion>
                            <groupId>javax.xml.bind</groupId>
                            <artifactId>jaxb-api</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

我仍然在依赖项列表中看到 javax.xml.bind:jaxb-api(使用 mvn ... -X).我做错了什么?

I still see javax.xml.bind:jaxb-api in the list of dependencies (with mvn ... -X). What am I doing wrong?

(如果有人知道如何用该 API 的 JDK 9 等效项替换对该工件的依赖项[就像在 Java 8 上发生的那样,其中JAXB API os 从 [jar:...jre/lib/rt.jar]"],我很高兴为此打开一个新问题.)

(In case someone has an idea for how to replace the dependency on that artifact with the JDK 9 equivalent for that API [as seems to happen on Java 8, where "JAXB API os loaded from the [jar:...jre/lib/rt.jar]"], I'm happy to open a new issue for that.)

想法用尽了,无论如何这是一个实验,我通过在本地存储库中编辑插件的 pom.xml 排除了依赖项.现在 mvn ... -X 表明我还存在一个间接依赖关系(在本例中为 org.jvnet.jaxb2.maven2:maven-jaxb22-plugin)可以通过上述机制成功排除.仅使用 maven-jaxb2-pluginmaven-jaxb22-plugin 中的两个排除项并不能解决问题.这表明排除在一般情况下有效,但显然不适用于插件的直接依赖关系.

Running out of ideas and this being an experiment anyways, I excluded the dependency by editing the plugin's pom.xml in my local repository. Now mvn ... -X shows that there is also an indirect dependency (in this case by org.jvnet.jaxb2.maven2:maven-jaxb22-plugin) that I can successfully exclude with the mechanism above. Just using both excludes, from maven-jaxb2-plugin and maven-jaxb22-plugin, does not do the trick. This indicates that exclusion works in general but apparently not on a plugin's direct dependency.

(顺便说一句,这确实导致Java JAXB API 是从 [jrt:/java.xml.bind] 加载的",这是我的目标.)

(By the way, this indeed lead to "Java JAXB API is loaded from the [jrt:/java.xml.bind]", which was my goal.)

推荐答案

到目前为止,还没有任何理由这样做,但这似乎是一个有效的理由.我能想到的最干净的解决方案是允许使用none"覆盖插件依赖项的范围.

Up until now there hasn't been any reason to do this, but this seems like a valid one. Most clean solution I can think of is allowing to override the scope with "none" for plugin dependencies.

我已经为它创建了 MNG-6222,不确定我们是否'将为 Maven3 解决此问题,但至少为下一个专业这样做是有意义的.

I've created MNG-6222 for it, not sure if we'll fix this for a Maven3, but it makes sense to do it at least for the next major.

相关文章