Force Maven仅使用第一级依赖项 [英] Force Maven use only first level dependencies

查看:170
本文介绍了Force Maven仅使用第一级依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven Java项目。在编译项目时,我不希望通过一系列子依赖项偶然满足我的项目依赖项。在构建最终战争时,当maven必须检查所有使用的依赖项并为战争添加必要的lib时,这是可以的,但是在编译代码时我想确保只使用直接依赖项。为什么?

I have a Maven Java project. I don't want my project dependencies to be satisfied by chance through a chain of subdependencies when compiling the project. It is OK for me when building the final war when maven must check all used dependencies and add necessary libs to the war, but when compiling the code I want to be sure that only direct dependencies are used. Why?

假设我有两个依赖项:

<dependency>
    <groupId>com.package</groupId>
    <artifactId>module-1</artifactId>
</dependency>

<dependency>
    <groupId>com.package</groupId>
    <artifactId>module-2</artifactId>
</dependency>

对于我们的项目模块-1和模块2提供完全不同的用途,但在依赖树中的某处在模块-2中,使用模块-1。我删除了module-1依赖项,但是maven继续构建我的项目而没有编译错误,因为它从module-2子依赖项中解析了module-1。这种变化没有被注意到。

For our project module-1 and module-2 serve completely different purposes, but somewhere in the dependency tree of module-2, module-1 is used. I delete module-1 dependency, but maven continue to build my project without compilation errors, because it resolves module-1 from module-2 sub-dependencies. This change goes unnoticed.

一段时间后我们决定删除module-2,因为我们不需要它。很奇怪,但我们不能再使用从模块1导入的编译类,并且没有连接到模块2逻辑。

After sometime we decide to remove module-2, because we don't need it. Strange enough but we can not any more compile classes which were using imports from module-1 and which are not connected to module-2 logic.

这是一个简单的案例,但在大项目中,这可能会造成相当大的依赖。

This is a simple case, but in big project this can make quite a dependency mess.

推荐答案

您可以使用 Maven依赖插件目标依赖:分析,为您提供未在当前模块上声明的所有已使用依赖项的报告(包含传递)。那样Maven仍然会使用传递依赖(我猜不知道),但你可以通过插件强制自己确保这些也被声明。它还会警告您不必要的依赖。介意,插件分析编译的类。有时,您可能需要配置插件,因为有时它可能无法检测到编译时需要依赖项,而不是在运行时,例如因为内联常量。

You can use the Maven dependency plugin goal "dependency:analyze" to give you a report of all used dependencies which are not declared on the current module (included transitively). That way Maven will still use transitive dependencies (no way around that I guess), but you can force yourself via the plugin to make sure these are also declared. It will also warn you of unnecessary dependencies. Mind, the plugin analyzes the compiled classes. At times, you may need to configure the plugin, because occasionally it may not detect that a dependency is required at compile time but not at runtime, e.g. because a constant was inlined.

这篇关于Force Maven仅使用第一级依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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