When to use <optional>true</optional> and when to use <scope>provided</scope>

百般思念 提交于 2019-11-30 12:32:43

问题


the 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?


回答1:


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 he actually uses. 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.




回答2:


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.



来源:https://stackoverflow.com/questions/40393098/when-to-use-optionaltrue-optional-and-when-to-use-scopeprovided-scope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!