What is the difference between the maven scope compile
and provided
when artifact is built as a JAR? If it was WAR, I'd understand - the artifact would be included or not in WEB-INF/lib. But in case of a JAR it doesn't matter - dependencies aren't included. They have to be on classpath when their scope is compile
or provided
. I know that provided
dependencies aren't transitive - but is it only one difference?
6 Answers
From the Maven Doc:
compile
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.
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.
Recap:
- dependencies are not transitive (as you mentioned)
- provided scope is only available on the compilation and test classpath, whereas compile scope is available in all classpaths.
- provided dependencies are not packaged
-
8Yes, I know. But I ponder about difference in the scopes in
JAR packaging
context. Maven doc doesn't mention about it. I use Maven for a while, but I've just already asked myself about it :) So it seems that inJAR packaging
context, there isn't any difference betweencompile
andprovided
(except dependency transition). Am I right?– emstolJul 11, 2011 at 8:02 -
3@Jacob what is meant by "whereas compile scope is available in all classpaths."?– GeekApr 2, 2013 at 7:24
-
1I think "NOT Transitive" is the big catch here. Because dependency hell is something which developers face very frequently and Provided scope prevents it going and messing with other versions is crucial. Oct 25, 2017 at 13:57
-
2I think the difference is on packaging phase. With compile, it will include the jar in final war or jar(such as spring boot executable jar), and provided may results not. Because the provided jar may be provided by web container (such as put in ext lib folder), it is not in war package if not dependented by other scopes, compile, runtime. Jul 3, 2018 at 2:55
-
4@emstol Going back to your original question, you are right that in the case of JAR the dependencies are not packaged inside the JAR itself. But in maven, a JAR packaging means you want it to be used as a library. As soon as you import it in some other maven project the transitive dependencies will be brought if the scope is
compile
and won't be if the scope isprovided
.– LeoLuzAug 1, 2018 at 17:04
Compile means that you need the JAR for compiling and running the app. For a web application, as an example, the JAR will be placed in the WEB-INF/lib directory.
Provided means that you need the JAR for compiling, but at run time there is already a JAR provided by the environment so you don't need it packaged with your app. For a web app, this means that the JAR file will not be placed into the WEB-INF/lib directory.
For a web app, if the app server already provides the JAR (or its functionality), then use "provided" otherwise use "compile".
-
16You are not answering the OP's question? 'What it difference in using maven scope compile and provided when artifact is builded as a JAR?' Notice that the author explicitly states that they knows the difference when packaging as war.– AlbertoAug 21, 2015 at 8:39
-
can I use provided if I am referencing another JAR deployed on the same application server ?? Aug 24, 2015 at 18:40
-
1So to be clear, a provided dependency is not added to the classpath when
mvn exec:java
is run, but a compiled dependency is.– JamieJan 11, 2016 at 19:50 -
I asked this question - stackoverflow.com/questions/37360132/… The problem was solved by changing scope from provided to compile. But I don't see any difference between jar compiled with "provided" scope and jar compiled with "compile" scope. Could you explain why?– Pavel_KMay 21, 2016 at 11:31
-
Read the first answer : coderanch.com/t/502091/tools/difference-maven-compile-scope Aug 2, 2016 at 17:18
If you're planning to generate a single JAR file with all of its dependencies (the typical xxxx-all.jar), then provided scope matters, because the classes inside this scope won't be package in the resulting JAR.
See maven-assembly-plugin for more information
-
9
-
3The OP confusion is clearly resolved when you package with
maven-assembly-plugin
, interesting that the most up-voted answers do not mention it. May 30, 2017 at 23:29 -
-
in my project the dependent jar is packaged in the final jar with both scopes (compile and provided). I don't know why this is happening.– lyrioMar 8, 2021 at 7:50
-
Try to compute the effective pom.xml. Most IDEs have this option. Here you will see if someone is overriding your scope. Mar 8, 2021 at 12:05
- compile
Make available into class path, don't add this dependency into final jar if it is normal jar; but add this jar into jar if final jar is a single jar (for example, executable jar)
- provided
Dependency will be available at run time environment so don't add this dependency in any case; even not in single jar (i.e. executable jar etc)
For a jar file, the difference is in the classpath listed in the MANIFEST.MF file included in the jar if addClassPath is set to true in the maven-jar-plugin configuration. 'compile' dependencies will appear in the manifest, 'provided' dependencies won't.
One of my pet peeves is that these two words should have the same tense. Either compiled and provided, or compile and provide.
If jar file is like executable spring boot jar file then scope of all dependencies must be compile
to include all jar files.
But if jar file used in other packages or applications then it does not need to include all dependencies in jar file because these packages or applications can provide other dependencies themselves.