Maven war 在两个地方有 META-INF 文件夹

标签 maven package war meta-inf

我正在开发一个使用 JAAS 的项目,不幸的是,Tomcat 需要将一个文件放在 war 根目录的 META-INF 文件夹中

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

我认为这已经很奇怪了,因为 WAR 的默认 META-INF 位置位于类文件夹中。
app.war
  |__WEB-INF
  |    |__classes
  |         |__META-INF
 ...

所以我正在使用 Maven,它指出 src/main/resources/META-INF 中的任何内容都将被复制到适当的位置,它确实如此。奇怪的是,它还在文件结构的根目录中创建了一个 META-INF 文件夹,给我留下了 2 个 META-INF 文件夹。

项目结构
app
  |__src/main/java
  |__src/main/resources
  |       |__META-INF
  |             |__context.xml
 ...

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

所以,如果 war 标准规定 META-INF 应该在 classes 文件夹下,如 #2 中,为什么 maven war 会创建 #1 文件夹。有没有办法让它将文件复制到该文件夹​​而不是#2?

问候

最佳答案

所以我发现了这个:

Two Meta-Inf folders - normal structure?

这表明拥有 2 个 META-INF 文件夹不是问题。挖掘了一下我发现:

JAR File Specification

其中说明了 META-INF 文件夹:

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.



还有这个:

JSR-000315 JavaTM Servlet 3.0

其中,在第 10.6 节中,说明了 WAR 文件结构:

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.



因此,从 WAR 规范来看,正确的位置是 WEB-INF/classes/META-INF。然而,由于 war 是一个特殊的 jar 文件,因此将/META-INF 作为扩展点是有意义的。可以在 JPA persistence.xml 与 Tomcat context.xml 文件中看到如此不同的用途:前者应放在 WEB-INF/classes/META-INF 中,而后者应放在/META-INF 中。

关于Maven war 在两个地方有 META-INF 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997731/

相关文章:

file - dart 中的 lib/src 和/bin 有什么区别?

java - Maven WAR 依赖项 - 无法解析包?

jar - 将war文件转换为exe文件

spring-boot - 当我设置 spring.active.profile 时,Spring Boot 不读取 Application.yml 文件

maven - KafkaSpout BrokerHosts 错误

java - Maven 配置文件 JxBrowser 中其他依赖项内的依赖项

java - 如何编译位于不同包中的依赖 .java 文件

java - Spring Boot之类模块maven项目更改父级

ruby-on-rails - 在 OSX .app 容器中分发 Rails 应用程序

deployment - 在 Wildfly 中,在我的 WAR 中包含 JAR 文件与通过 jboss-deployment-structure.xml 链接到它有区别吗?