在 maven 中,您能否禁止在代码中使用传递依赖,但仍将其保留在类路径中? [英] In maven, can you disallow usage of transitive dependency in your code but still keep it in the classpath?

查看:58
本文介绍了在 maven 中,您能否禁止在代码中使用传递依赖,但仍将其保留在类路径中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个依赖于库 B 的库 A.我的项目将 A 作为声明的依赖项,但这也会将 B 拉入我的项目的类路径中.有没有插件可以防止库B中的类在我的项目中使用?

There is a library A that depends on library B. My project has A as declared dependency, but that also pulls B into my project's classpath. Is there a plug-in that can prevent classes from library B from being used in my project?

我知道依赖排除机制,但这不是一个选项,因为如果我排除 B 那么程序将无法运行,因为 A 依赖它.出于同样的原因,maven-enforcer-plugin 也不起作用(至少是它的标准规则).

I know about dependency exclusion mechanism, but that's not an option since if I exclude B then the program won't work, as A relies on it. For the same reason maven-enforcer-plugin doesn't work either (at least the standard rules for it).

我正在寻找的是一种插件,它可以分析源代码或字节代码,并且如果来自 A 的传递依赖项的包从我的项目的类中导入,则构建失败.

What I am looking for is a kind of plug in that can analyze source or byte code and fail the build if packages from A's transitive dependencies are imported from my project's classes.

推荐答案

(a) 使 A 成为编译时依赖项并在该节中排除 B.

(a) Make A a compile-time dependency and exclude B in that stanza.

(b) 添加 B 作为运行时依赖项.

(b) Add B as a runtime dependency.

这有你想要的效果,即B在编译时不可用,但在运行时包含.无需借助额外的工具来检查基于包的策略.

This has the effect you want, that is, B is not available at compile time but is included at runtime. No need to resort to an extra tool that checks package-based policies.

这是一个示例(根据您的情况替换 groupId 和 version):

Here is an example (substitute groupId and version as fits your situation):

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>A</artifactId>
        <version>${project.version}</version>
        <exclusions>
            <exclusion>
                <groupId>${project.groupId}</groupId>
                <artifactId>B</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>B</artifactId>
        <version>${project.version}</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

这篇关于在 maven 中,您能否禁止在代码中使用传递依赖,但仍将其保留在类路径中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆