Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using /META-INF/resources, where does WEB-INF go? [duplicate]

Tags:

jakarta-ee

I'm putting all my web resources into /META-INF/resources, so I can reuse them in different web apps.

But what about the web.xml inside of WEB-INF/? Do I have to keep that in src/main/webapp/ or can I put it under src/main/resources/ as well?

If so, where do I have to put it? src/main/resources/WEB-INF/ or src/main/resources//META-INF/resources/WEB-INF/?

I'm using Maven and the Assembly plugin to build my project, not the WAR Plugin.

like image 960
Aaron Digulla Avatar asked Oct 16 '22 15:10

Aaron Digulla


1 Answers

In a nutshell, you can.

You can share java classes, web resources and web-fragment.xml files between web applications in a regular jar file since Servlet 3.0. These jars are referred to as "web fragments".

From §8.2.1 of the Servlet 3.1 Specification:

...

A web fragment is a logical partitioning of the web application in such a way that the frameworks being used within the web application can define all the artifacts without asking developers to edit or add information in the web.xml. It can include almost all the same elements that the web.xml descriptor uses. However the top level element for the descriptor MUST be web-fragment and the corresponding descriptor file MUST be called web-fragment.xml. The ordering related elements also differ between the web-fragment.xml and web.xml See the corresponding schema for web-fragments in the deployment descriptor section in Chapter 14.

If a framework is packaged as a jar file and has metadata information in the form of deployment descriptor then the web-fragment.xml descriptor must be in the META-INF/ directory of the jar file.

...

Therefore, instead of trying to embed a web.xml file somehow, you just need to place the same content in a META-INF/web-fragment.xml file (it shares most of the web.xml XSD).

Your web resources go in META-INF/resources as you have already discovered.

In summary, you can easily build shared components in normal jar file. You don't really need to mess with the maven-assembly-plugin to accomplish this.

like image 176
Steve C Avatar answered Oct 21 '22 09:10

Steve C