简体   繁体   English

在将Maven与Tomcat一起使用时,将第3方库放在哪里?

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

I'm a new to Maven and I didn't find the proper answer to the following problem. 我是Maven的新手,但没有找到以下问题的正确答案。

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. 例如,我有项目所依赖的库,例如:log4j,连接器j,servlet api,junit,struts等。当我仅使用Tomcat时,可​​以将此jar放在%CATALINA_HOME%\\lib文件夹中并使用它们。

But as Maven comes in I can configure pom.xml for all dependencies and their scopes. 但是随着Maven到来,我可以为所有依赖项及其作用域配置pom.xml

If I do it - can I remove libs from %CATALINA_HOME%\\lib folder ? 如果这样做-是否可以从%CATALINA_HOME%\\lib文件夹中删除库? If I can then how Tomcat knows where to find this 3rd party jars ? 如果可以的话, Tomcat如何知道在哪里可以找到该第三方罐子?

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 ? 并且仅在%CATALINA_HOME%\\lib文件夹中具有tomcat本地库(tomcat-util,tomcat-juli,jasper等)并在pom.xml指定其他库是一种好习惯吗?

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 ? 并且仅在%CATALINA_HOME%\\ lib文件夹中具有tomcat本地库(tomcat-util,tomcat-juli,jasper等)并在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). 这样,您的Tomcat安装将保持不变,您可以使用它来部署另一个webapp(该webapp可能具有另一组依赖项,或者在另一个版本中具有相同的依赖项)。

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. Maven会自动将依赖项放在最终war文件的WEB-INF/lib ,而Tomcat(以及其他所有服务器)将在此目录中查找库。

By the way, I noticed you mentioned the servlet-api in your post. 顺便说一句,我注意到您在您的文章中提到了servlet-api。 This particular dependency is provided by Tomcat, which means you do not need to include it in your final war. Tomcat提供了这种特定的依赖关系,这意味着您不必在最终的战争中将其包括在内。 With Maven, that means you need to add <scope>provided</scope> to this dependency. 使用Maven,这意味着您需要向此依赖项添加<scope>provided</scope>

If I do it - can I remove libs from %CATALINA_HOME%\\lib folder ? 如果这样做-是否可以从%CATALINA_HOME%\\ lib文件夹中删除库?

Yes. 是。

If I can then how Tomcat knows where to find this 3rd party jars ? 如果可以的话,Tomcat如何知道在哪里可以找到该第三方罐子?

The classloader knows, that it has to look in your war/WEB-INF/lib Directory . 类加载器知道,它必须在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 ? 并且仅在%CATALINA_HOME%\\ lib文件夹中具有tomcat本地库(tomcat-util,tomcat-juli,jasper等)并在pom.xml中指定其他库是一种好习惯吗?

Yes it is. 是的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM