Maven war has META-INF folder in two places

2019-02-02 10:47发布

I'm working on a project which uses JAAS and unfortunately for me Tomcat requires a file to be put in a META-INF folder in the root of the war

app.war
  |__META-INF
  |    |___context.xml
 ...

I think that it's already weird since the default META-INF location for WAR's is in the classes folders.

app.war
  |__WEB-INF
  |    |__classes
  |         |__META-INF
 ...

So I'm using Maven, which states that anything in src/main/resources/META-INF will be copied to the appropriate place, which it does. The weird thing is that it is also creating a META-INF folder in the root of the file structure leaving me with 2 META-INF folders.

Project Structure

app
  |__src/main/java
  |__src/main/resources
  |       |__META-INF
  |             |__context.xml
 ...

After mvn package

 app
  |__META-INF [1]
  |__WEB-INF
  |     |__classes
  |           |__META-INF [2]
  |                  |__context.xml
 ...

So, if the war standard states that META-INF should be under classes folder, as in #2, why maven war creates the #1 folder. And is there a way to make it copy files into that folder instead of #2?

Regards

1条回答
等我变得足够好
2楼-- · 2019-02-02 11:31

So I found this:

Two Meta-Inf folders - normal structure?

which states that having 2 META-INF folders is not a problem. Digging a little I found:

JAR File Specification

which states about the META-INF folder:

A JAR file is essentially a zip file that contains an optional META-INF directory. ...The META-INF directory, if it exists, is used to store package and extension configuration data, including security, versioning, extension and services.

and this:

JSR-000315 JavaTM Servlet 3.0

which, on section 10.6, states about the WAR file structure:

When packaged into such a form, a META-INF directory will be present which contains information useful to Java archive tools. This directory must not be directly served as content by the container in response to a Web client’s request, though its contents are visible to servlet code via the getResource and getResourceAsStream calls on the ServletContext. Also, any requests to access the resources in META-INF directory must be returned with a SC_NOT_FOUND(404) response.

So from the WAR spec the right place is WEB-INF/classes/META-INF. Yet, since war is a special jar file, it makes sense to have /META-INF as a point for extensions. One can see such different uses in JPA persistence.xml vs. Tomcat context.xml files: the former should be placed in WEB-INF/classes/META-INF while the latter in /META-INF.

查看更多
登录 后发表回答