6

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820

2 Answers2

4

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.

Steve C
  • 18,876
  • 5
  • 34
  • 37
0

In a maven project the standard location for web.xmlis src/main/webapp/WEB-INF, you can change it if you neeed, but in the resulting .war file, web.xml must be included inside WEB-INF folder.

So, putting web.xml inside src/main/resources/META-INF won't work.

For newer Servlet specitications (I think Servlet 3.0 and above), you can use annotations and web.xml is not mandatory.

EDIT: Note that web.xml is the standard configuration file for war files(web applications), and it will be ignored inside jar files(libraries)

Rafael Guillen
  • 1,343
  • 10
  • 25
  • 1
    Although OP did not use the proper terminology, OP is basically talking about a "web fragment" JAR project, not a WAR project. Your answer is invalid. โ€“ BalusC Jul 31 '18 at 13:24
  • Oh, I see, then note that web.xml is the standard configuration file for war files(web applications), and it will not work inside jar files(libraries). โ€“ Rafael Guillen Jul 31 '18 at 13:47
  • Thanks, that got me on the right track. โ€“ Aaron Digulla Jul 31 '18 at 13:59