Where to put 3rd party libs when using Maven with Tomcat?

时光毁灭记忆、已成空白 提交于 2021-02-05 07:49:06

问题


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 ?


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/28638944/where-to-put-3rd-party-libs-when-using-maven-with-tomcat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!