When to use "optional" dependencies and when to use "provided" scope?

MavenDependency Management

Maven Problem Overview


Dependencies decorated by <optional>true</optional> or <scope>provided</scope> will be ignored when they are dependent transitively. I have read this, my understanding is like the difference between @Component and @Service in Spring, they only vary semantically.

Is it right?

Maven Solutions


Solution 1 - Maven

In addition to the comment, there is more important semantic difference: "Provided" dependencies are expected to be supplied by the container, so if your container gives you hibernate, you should mark hibernate as provided.

Optional dependencies are mainly used to reduce the transitive burden of some libraries. For example: If you can use a library with 5 different database types, but you usually only require one, you can mark the library-dependent dependencies as optional, so that the user can supply the one they actually use. If you don't do, you might get two types of problems:

  1. The library pulls a huge load of transitive dependencies of which you actually need very few so that you blow up your project without reason.

  2. More dangerously: You might pull two libraries with overlapping classes, so that the class loader cannot load both of them. This might lead to unexpected behaviour of your library.

Solution 2 - Maven

A minor difference I'd like to point out is the treatment of optional vs. provided by various plugins that create packages.

Apparently war plugin will not package optional dependencies, but there is an open bug about it: https://issues.apache.org/jira/browse/MWAR-351

The assembly plugin doesn't seem to provide any way to filter based on optional status, while it allows you to filter based on scope.

It seems the same is true for the shade plugin.

TL;DR if you are not developing a library, but a top-level application provided scope will give you more flexibility.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionqining.shiView Question on Stackoverflow
Solution 1 - MavenJ Fabian MeierView Answer on Stackoverflow
Solution 2 - MavenJakub BochenskiView Answer on Stackoverflow