Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"optional" dependency with scope "provided" in Maven

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:

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

like image 750
Lukas Eder Avatar asked May 02 '11 17:05

Lukas Eder


People also ask

What are optional dependencies in Maven?

Optional dependencies are used when it's not possible (for whatever reason) to split a project into sub-modules. 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.

What is the use of scope in Maven dependency?

Dependency scope is used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath. This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

How do you avoid optional dependency?

In order to exclude these special dependencies from the main project, we can apply Maven's <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn't need them.

What is scope provided in POM xml?

The <scope> element can take 6 values: compile, provided, runtime, test, system and import. This scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks. This is the default scope, used if none is specified.


1 Answers

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

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.

like image 177
FrVaBe Avatar answered Sep 21 '22 08:09

FrVaBe