325

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?

Neuron
  • 5,141
  • 5
  • 38
  • 59
emstol
  • 5,966
  • 6
  • 27
  • 32

6 Answers6

350

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

Here is the reference.

Matt
  • 23,363
  • 39
  • 111
  • 152
Owen Cao
  • 7,955
  • 2
  • 27
  • 35
  • 21
    You 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. – Alberto Aug 21 '15 at 08:39
  • can I use provided if I am referencing another JAR deployed on the same application server ?? – Samy Omar Aug 24 '15 at 18:40
  • 1
    So to be clear, a provided dependency is not added to the classpath when `mvn exec:java` is run, but a compiled dependency is. – Jamie Jan 11 '16 at 19:50
  • I asked this question - http://stackoverflow.com/questions/37360132/osgi-bundles-class-inheritance-and-maven-scope 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_K May 21 '16 at 11:31
  • Read the first answer : https://coderanch.com/t/502091/tools/difference-maven-compile-scope – AllTooSir Aug 02 '16 at 17:18
345

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
Neuron
  • 5,141
  • 5
  • 38
  • 59
Jacob
  • 41,721
  • 6
  • 79
  • 81
  • 12
    Yes, 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 in `JAR packaging` context, there isn't any difference between `compile` and `provided` (except dependency transition). Am I right? – emstol Jul 11 '11 at 08:02
  • And "classpath-availability", yes. – Jacob Jul 11 '11 at 08:19
  • 3
    @Jacob what is meant by "whereas compile scope is available in *all classpaths*."? – Geek Apr 02 '13 at 07:24
  • @Geek compile,test,run time? – Kalpesh Soni Apr 11 '14 at 05:18
  • 2
    I 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. – Seetha Oct 25 '17 at 13:57
  • 3
    I 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. – Addo Zhang Jul 03 '18 at 02:55
  • 6
    @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 is `provided`. – LeoLuz Aug 01 '18 at 17:04
  • Just a doubt. If I use scope `provided`, is the web container bound to provide that specific version of the dependency? Or it can provide any version ( if a new one is available) ? – InconsistentHashing Aug 22 '19 at 09:41
30

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

jfcorugedo
  • 9,793
  • 8
  • 39
  • 47
  • 12
    provided dependency ==> the dependency will **NOT** be packaged. – Gab是好人 May 01 '17 at 21:11
  • 4
    The OP confusion is clearly resolved when you package with `maven-assembly-plugin`, interesting that the most up-voted answers do not mention it. – Henrique G. Abreu May 30 '17 at 23:29
  • I don't understand this answer. It looks more like a comment. – reinierpost Jun 09 '17 at 11:12
  • 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. – lyrio Mar 08 '21 at 07:50
  • 1
    Try to compute the effective pom.xml. Most IDEs have this option. Here you will see if someone is overriding your scope. – jfcorugedo Mar 08 '21 at 12:05
  • 1
    Yes this is the answer. I use assembly plugin and in goal `single`, provided jars are not included in the `xxx-with-dependency.jar` (uber jar). – WesternGun Jun 20 '22 at 12:09
13
  • 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)

Vijay
  • 471
  • 6
  • 10
4

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.

Rick
  • 61
  • 3
1

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.

Shapur
  • 498
  • 7
  • 17