ChatGPT解决这个技术问题 Extra ChatGPT

Difference between maven scope compile and provided for JAR packaging

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?


N
Neuron

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


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?
@Jacob what is meant by "whereas compile scope is available in all classpaths."?
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.
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.
@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.
M
MegaMatt

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.


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.
can I use provided if I am referencing another JAR deployed on the same application server ??
So to be clear, a provided dependency is not added to the classpath when mvn exec:java is run, but a compiled dependency is.
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?
j
jfcorugedo

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


provided dependency ==> the dependency will NOT be packaged.
The OP confusion is clearly resolved when you package with maven-assembly-plugin, interesting that the most up-voted answers do not mention it.
I don't understand this answer. It looks more like a comment.
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.
Try to compute the effective pom.xml. Most IDEs have this option. Here you will see if someone is overriding your scope.
V
Vijay

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)


R
Rick

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.


A
Ali

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.