Maven中“提供”范围的“可选”依赖项

时间:2022-06-10 08:08:29

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:

有时Maven有点过头了......我创建了一个库,它对slf4j和log4j有可选的依赖。通过可选,我的意思是:

  • My library needs those logging frameworks at compile time
  • 我的库在编译时需要那些日志框架
  • My library doesn't need them at runtime, but if it "discovers" them, it will use them
  • 我的库在运行时不需要它们,但如果它“发现”它们,它将使用它们

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>

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.

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

1 个解决方案

#1


32  

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).

你看过这个文件了吗?它描述了你的用例非常好。将依赖项标记为可选不会将它们解析为使用您的库的应用程序中的传递依赖项(即使范围是编译)。

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.).

提供的 不同, 用于运行时环境提供的必需依赖项, true 依赖关系不一定是必需的(这个想法是依赖项仅用于项目中的某些功能,如果不使用该功能,则不需要。)。

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.

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

#1


32  

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).

你看过这个文件了吗?它描述了你的用例非常好。将依赖项标记为可选不会将它们解析为使用您的库的应用程序中的传递依赖项(即使范围是编译)。

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.).

提供的 不同, 用于运行时环境提供的必需依赖项, true 依赖关系不一定是必需的(这个想法是依赖项仅用于项目中的某些功能,如果不使用该功能,则不需要。)。

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.

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