Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between maven scope compile and provided for JAR packaging

Tags:

maven

jar

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?

like image 907
emstol Avatar asked Jul 11 '11 07:07

emstol


People also ask

What does scope compile mean in Maven?

Compile. This is the default scope when no other scope is provided. Dependencies with this scope are available on the classpath of the project in all build tasks. They are also propagated to the dependent projects.

What is scope provided scope in Maven?

Maven dependency scope – provided Maven dependency scope provided is used during build and test the project. They are also required to run, but should not exported, because the dependency will be provided by the runtime, for instance, by servlet container or application server.

What is compile scope?

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.

What is provided dependency in Maven?

Dependency scope is used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath. 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.


2 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
like image 123
Jacob Avatar answered Oct 26 '22 06:10

Jacob


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.

like image 44
Owen Cao Avatar answered Oct 26 '22 06:10

Owen Cao