Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.
Notice: I'm taking a break from blogging to focus on Atari Gamer . com, check it out!

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
I've written articles about building web services using Maven in the past as well as various other articles to do with Maven in general. This time I want to cover how Maven's dependency mechanism relates to WAR file packaging, specifically to the WEB-INF/lib directory.

I'm going to use JUnit as the dependency since it is pretty much guaranteed to be available in any Maven repository. Hence, my dependency inside the pom.xml is defined like this (minus the scope element)...

There are 5 dependency scopes available (actually 6 but the import scope is not of interest here). The default scope is compile. Taking each of the scopes into account this is what will be packaged inside the WEB-INF/lib directory inside the WAR file when each of the scopes is used...

Scopes: compile, runtime:

For the compile and runtime scopes, the WEB-INF/lib directory will contain the JAR files of the dependencies as well as any dependencies that the project's dependencies have that use a transient scope. For example since JUnit depends on Hamcrest Core, both the junit-4.12.jar and hamcrest-core-1.3.jar files are included in the WEB-INF/lib directory.

Scopes: provided, test, system:

For the provided, test and system scopes the WEB-INF/lib directory will not contain JAR files of the dependencies. In this case the WEB-INF/lib directory is not created at all since there aren't any other JARs are inside it.

As a side note - NEVER use the system scope in a J2EE project unless there is a really good reason for it (usually there isn't).


Skip down to comments...
A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. If you spot something out of place, please do let me know.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...