Maven – Difference between maven scope compile and provided for JAR packaging

jarmaven

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?

Best Answer

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