1

I'm a new to Maven and I didn't find the proper answer to the following problem.

For example I have libraries that my project depends on like: log4j, connector j, servlet api, junit, struts etc. When I'm using just Tomcat I can put this jars to %CATALINA_HOME%\lib folder and use them.

But as Maven comes in I can configure pom.xml for all dependencies and their scopes.

If I do it - can I remove libs from %CATALINA_HOME%\lib folder ? If I can then how Tomcat knows where to find this 3rd party jars ?

And Is it a good practice just to have tomcat-native libs (tomcat-util, tomcat-juli, jasper etc) in %CATALINA_HOME%\lib folder and specify other libs in pom.xml ?

marknorkin
  • 3,904
  • 10
  • 46
  • 82

2 Answers2

1

And Is it a good practice just to have tomcat-native libs (tomcat-util, tomcat-juli, jasper etc) in %CATALINA_HOME%\lib folder and specify other libs in pom.xml ?

Yes, this is indeed a very good practice. This way, your Tomcat installation is left unchanged and you can use it to deploy another webapp (that might have another set of dependencies, or the same dependency in another version).

Maven will automatically place the dependencies inside WEB-INF/lib of your final war file and Tomcat (and every other server) will look in this directory for the librairies.

By the way, I noticed you mentioned the servlet-api in your post. This particular dependency is provided by Tomcat, which means you do not need to include it in your final war. With Maven, that means you need to add <scope>provided</scope> to this dependency.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I'm accepting your answer as it has more detailed explanation. thank you – marknorkin Feb 20 '15 at 22:14
  • also would you explain: in this case do i need to specify the servlet-api in pom.xml ? If i don't, in which cases that would be needed ? – marknorkin Feb 20 '15 at 22:19
  • Yes, you do need to specify it in the pom.xml but with the provided scope. This is a special scope that tells Maven "I need this dependency for compiling but don't package it in my war because it will be provided by the server deploying my app". If it is included in your war, you will have a warning at Tomcat startup saying that this jar shouldn't be in your librairies. – Tunaki Feb 20 '15 at 22:24
1

If I do it - can I remove libs from %CATALINA_HOME%\lib folder ?

Yes.

If I can then how Tomcat knows where to find this 3rd party jars ?

The classloader knows, that it has to look in your war/WEB-INF/lib Directory.

And Is it a good practice just to have tomcat-native libs (tomcat-util, tomcat-juli, jasper etc) in %CATALINA_HOME%\lib folder and specify other libs in pom.xml ?

Yes it is.

Jens
  • 67,715
  • 15
  • 98
  • 113