Reputation: 1216
I'm sorry if this question is refering to a common one, but I find this pretty abstract, and can't really make up a good Google search term for it.
I'm trying to understand and find a use-case for the provided dependency in maven. My idea is this:
let's say, I have 3 maven projects: A, B, C
if I deploy A and C on the same Weblogic domain/server. Will A see C on the classpath as implementation of B?
If not, what is a good use-case for the provided scoped dependeny?
Thanks in advance
Upvotes: 0
Views: 625
Reputation:
As the maven documentation states for scope provided:
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
One use case is already mentioned in there: When building a JavaEE application that you later want to deploy on a JavaEE Application Server the Application Server provides the javaEE implementation.
So to tell maven that you need this dependency during compile time but not packaged into the project later on you would use the scope provided like:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
Another use case is when building a JavaEE Application that used different container types. There is a JavaEE EJB and a Web Container and you need to make sure that your classes don't get packed/loaded in the wrong container or even get loaded into both containers as that can cause all kinds of problems for your application and classloaders.
Say you are building an Application that contains both an EJB module as well as a web-module and you want your EJBs to run in the EJB Container. You also want to use your EJBs in the web-module. Since EJBs run in the EJB container while the Web-Module runs in the webcontainer you cannot simply add the EJB-Dependency with scope compile in your web-project. Because if you did that maven would package the ejb in the war file and the EJB will end up in the web-container.
So in your web-application you would add the dependency as
<artifactId>my-web</artifactId>
<packaging>war</packaging>
<dependency>
<groupId>your.group</groupId>
<artifactId>my-ejb</artifactId>
<type>ejb</type>
<scope>provided</scope>
</dependency>
That way you are telling maven that you want to use your EJB module, but that it shouldn't package it into your war-file and that you will make sure that this module is available during runtime yourself.
Upvotes: 2