Reasons to use WEB-INF/lib over EAR's lib?

风格不统一 提交于 2020-08-27 21:32:06

问题


I am having second thoughts about something I've been taking for granted. Namely, in an EAR with the following structure:

EAR
 \-- boo-ejb.jar
 \-- foo-web.war
 |    \--WEB-INF
 |          \--lib
 \--lib

.. I think I understand correctly that the WEB-INF/lib is for jars needed only by the web application and the EAR-level lib for jars needed by the ejb.jar as well. The rationale being to somehow make the dependencies clearer. However, why not just dump everything in the EAR-level lib folder ? Surely if there is a conflict of some sort we would like to know and investigate in depth. Wouldn't that be a cleaner solution and less prone to covering up a potential compatibility / dependency mismatch problem?


回答1:


You're absolutely right - it would be much clearer and less error prone to dependency mismatch.

But...

What in cases where you don't distribute your web applications within an EAR? Where would you place the libs? How would you go about distributing your webapps throughout different EARs so you would compose enterprise applications based upon a set of webapps?

WEB-INF/lib is also for jars that can contain resources only (with no classes within). Would you then put the web-oriented resources jars in EAR-level lib? I don't think so.

There are perhaps more reasons to use WEB-INF/lib over an EAR's lib, but the above two/three cases should be enough to at least convince you to re-think the strategy to manage libraries.




回答2:


In a EAR file, the lib folder at the EAR level is across all web-apps (i.e. all .wars in the EAR). The lib within a .war is for libraries that will only be available to the web-app war.

A lot of times you have to promote specific libraries to the EAR level and set classpath loader preference priority settings (between the app-server provided and EAR provided) jars. This is a common need in servers such as WebSphere.

Here is an example war configuration for maven wherein specific jars are retrieved from the EAR level and are specifically excluded from the WAR:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
    <archive>
    <manifestEntries>
         <Class-Path>lib/wsdl4j-1.6.2.jar lib/mail-1.4.jar</Class-Path>
    </manifestEntries>
    </archive>
    <packagingExcludes>WEB-INF/lib/wsdl4j-1.6.2.jar,WEB-INF/lib/mail-1.4.jar</packagingExcludes>
    <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
    <warSourceExcludes>WEB-INF/jetty-env.xml</warSourceExcludes>
  </configuration>
 </plugin>



回答3:


I would say if you have a web application that can run on its own and may be deployed on its own, it should be packaged in a war. However, if not... if the war is just one component of a larger, ear application, and that's the way it's always going to be delivered/deployed, then just put web resources that are needed by the web application in the war, and all code jars in the ear's lib.



来源:https://stackoverflow.com/questions/14384899/reasons-to-use-web-inf-lib-over-ears-lib

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