Thursday, 21 June 2012

Difference between "Provided" and "Compiled" scopes Maven?

Q: Maybe this question seems to be a cliché but it bothers me somehow. What it difference in using maven scope compile and provided when artifact is builded as a JAR? If it was WAR, then I understand - artifact would be attached or not to WEB-INF/lib. But in case of the JAR it doesn't matter - dependencies aren't attached. 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?

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.

  • 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


No comments:

Post a Comment