“可选"范围为“提供"的依赖关系;在Maven [英] "optional" dependency with scope "provided" in Maven

查看:95
本文介绍了“可选"范围为“提供"的依赖关系;在Maven的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,Maven有点麻烦……我创建了一个库,该库对slf4j和log4j具有可选的依赖关系.可选的,我的意思是:

Maven is a bit over my head sometimes... I have created a library which has an optional dependency on slf4j and on log4j. By optional, I mean:

  • 我的图书馆在编译时需要这些日志框架
  • 我的库在运行时不需要它们,但是如果发现"它们,它将使用它们.

当前,我已将该依赖项标记为可选"和提供":

Currently, I have marked that dependency as "optional" and "provided":

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
    <type>jar</type>
    <scope>provided</scope>
    <optional>true</optional>
</dependency>

但是我的一些用户报告了问题,因为他们不需要log4/slf4j.我的依赖关系正确吗?不幸的是,我发现官方文档过于抽象,无法理解此问题.

But some of my users have reported issues, because they don't need log4 / slf4j. Is my dependency correct? Unfortunately, I find the official documentation a bit too abstract to understand this problem.

推荐答案

您是否检查了

Did you check this documentation. It describes your use case very good. Marking dependencies as optional will not resolve them as transitive dependencies in the application which use your library (even if the scope is compile).

<scope>provided</scope>的区别在于,<scope>provided</scope>用于运行时环境将提供的必需依赖项,而<optional>true</optional>依赖项不一定是必需的(的想法是,某些依赖项仅用于对于项目中的某些功能,如果不使用该功能,则不需要.).

In difference to <scope>provided</scope> which is used for required dependencies which will be provided by the runtime environment an <optional>true</optional> dependency is not necessarily meant to be required (The idea is that some of the dependencies are only used for certain features in the project, and will not be needed if that feature isn't used.).

如果使用您的库的项目将使用可选依赖项提供的任何功能,则该项目必须自行声明这些依赖项.

If a project which uses your library will use any functionallity provided by the optional dependencies the project has to declare these dependencies for their own.

由于您的配置对我来说似乎是正确的,所以我不知道出现问题的原因.也许您期望的依赖项会被您期望的版本中的其他库解决.那当然会引起问题.

As your configuration seems to be correct for me I do not know the reason what probles occur. Maybe your optional dependencies get resolved by other libraries in versions you do not expect. That of course might cause problems.

这篇关于“可选"范围为“提供"的依赖关系;在Maven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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