在Java中,您经常会看到包含一些元文件的meta - inf文件夹。这个文件夹的目的是什么?我可以在那里放什么?


当前回答

As an addition the META-INF folder is now also used for multi-release jars. This is a feature which allows to package classes which are meant for different Java version in one jar, e.g. include a class for Java 11 with new features offered by Java 11 in a jar also working for Java 8, where a different class for Java 8 with less features in contained. E.g this can be useful if a newer Java version is offering enhanced, different or new API methods which would not work in earlier version due to API violations. One will see a sub folder versions then.

其他回答

Maven中的META-INF

在Maven中,META-INF文件夹被理解是因为标准目录布局,根据名称约定将您的项目资源打包到JAR中:放置在${basedir}/src/main/resources目录中的任何目录或文件都以完全相同的结构打包到JAR中,从JAR的底部开始。

文件夹${basedir}/src/main/resources/META-INF通常包含。properties文件,而在jar中包含一个生成的MANIFEST。MF,砰的一声。属性,pom.xml,以及其他文件。同样,像Spring这样的框架使用classpath:/META-INF/resources/来提供web资源。

有关更多信息,请参阅如何向Maven项目添加资源。

添加到这里的信息,META-INF是一个特殊的文件夹,ClassLoader将其与jar中的其他文件夹区别对待。 嵌套在META-INF文件夹内的元素不会与它外部的元素混合。

把它想象成另一个根。从Enumerator<URL> ClassLoader#getSystemResources(字符串路径)方法等透视图:

当给定的路径以“META-INF”开头时,该方法搜索类路径中所有jar的META-INF文件夹内嵌套的资源。

当给定的路径不是以“META-INF”开头时,该方法在类路径中所有jar和目录的所有其他文件夹(在META-INF之外)中搜索资源。

如果您知道getSystemResources方法特别处理的另一个文件夹名称,请对其进行评论。

所有答案都是正确的。Meta-inf有很多用途。此外,这里还有一个关于使用tomcat容器的示例。

去 Tomcat Doc并检查 “标准实现> copyXML”属性。

描述如下。

Set to true if you want a context XML descriptor embedded inside the application (located at /META-INF/context.xml) to be copied to the owning Host's xmlBase when the application is deployed. On subsequent starts, the copied context XML descriptor will be used in preference to any context XML descriptor embedded inside the application even if the descriptor embedded inside the application is more recent. The flag's value defaults to false. Note if the deployXML attribute of the owning Host is false or if the copyXML attribute of the owning Host is true, this attribute will have no effect.

META-INF文件夹是清单的主页。MF文件。该文件包含关于JAR内容的元数据。例如,有一个名为main - class的条目,它为可执行JAR文件指定了带有静态main()的Java类的名称。

我注意到一些Java库已经开始使用META-INF作为目录,其中包含应该打包并与jar一起包含在CLASSPATH中的配置文件。例如,Spring允许您使用以下方法导入类路径上的XML文件:

<import resource="classpath:/META-INF/cxf/cxf.xml" />
<import resource="classpath:/META-INF/cxf/cxf-extensions-*.xml" />

在本例中,我直接引用Apache CXF用户指南中的内容。在我参与的一个项目中,我们必须允许通过Spring进行多级配置,我们遵循了这个约定,并将配置文件放在META-INF中。

当我反思这个决定时,我不知道简单地将配置文件包括在特定的Java包中,而不是在META-INF中,到底有什么问题。但这似乎是一个新兴的事实标准;要么这样,要么出现反模式:-)