在Java中,您经常会看到包含一些元文件的meta - inf文件夹。这个文件夹的目的是什么?我可以在那里放什么?
当前回答
添加到这里的信息,META-INF是一个特殊的文件夹,ClassLoader将其与jar中的其他文件夹区别对待。 嵌套在META-INF文件夹内的元素不会与它外部的元素混合。
把它想象成另一个根。从Enumerator<URL> ClassLoader#getSystemResources(字符串路径)方法等透视图:
当给定的路径以“META-INF”开头时,该方法搜索类路径中所有jar的META-INF文件夹内嵌套的资源。
当给定的路径不是以“META-INF”开头时,该方法在类路径中所有jar和目录的所有其他文件夹(在META-INF之外)中搜索资源。
如果您知道getSystemResources方法特别处理的另一个文件夹名称,请对其进行评论。
其他回答
你也可以在那里放置静态资源。
在示例:
META-INF/resources/button.jpg
并通过web3.0容器获取它们
http://localhost/myapp/button.jpg
>阅读更多
/ meta - inf / MANIFEST。MF有特殊的含义:
如果你使用java -jar myjar.jar org.myserver.MyMainClass运行一个jar,你可以将主类定义移动到jar中,这样你就可以将调用缩小到java -jar myjar.jar。 如果你使用java.lang.Package.getPackage("org.myserver"). getimplementationtitle(),你可以为包定义元结构。 您可以在Applet/Webstart模式中引用您喜欢的数字证书。
我注意到一些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中,到底有什么问题。但这似乎是一个新兴的事实标准;要么这样,要么出现反模式:-)
来自官方JAR文件规范(链接到Java 7版本,但文本至少从v1.3开始就没有改变):
The META-INF directory The following files/directories in the META-INF directory are recognized and interpreted by the Java 2 Platform to configure applications, extensions, class loaders and services: MANIFEST.MF The manifest file that is used to define extension and package related data. INDEX.LIST This file is generated by the new "-i" option of the jar tool, which contains location information for packages defined in an application or extension. It is part of the JarIndex implementation and used by class loaders to speed up their class loading process. x.SF The signature file for the JAR file. 'x' stands for the base file name. x.DSA The signature block file associated with the signature file with the same base file name. This file stores the digital signature of the corresponding signature file. services/ This directory stores all the service provider configuration files.
自Java 9实现JEP 238以来新增了多版本jar。一个会看到子文件夹的版本。这个特性允许将不同Java版本的类打包到一个jar中。
所有答案都是正确的。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的使用似乎真的没有任何限制。当然,有一些限制,关于把舱单放在那里的必要性,但似乎没有任何禁止把其他东西放在那里。
为什么会这样呢?
cxf案例可能是合法的。这里是建议使用非标准的另一个地方,它可以绕过JBoss-ws中防止针对wsdl模式进行服务器端验证的严重错误。
http://community.jboss.org/message/570377#570377
但似乎真的没有什么标准,没有什么千言万语。通常这些事情都有非常严格的定义,但出于某种原因,这里似乎没有标准。奇数。META-INF似乎已经成为了一个无所不包的地方,任何所需的配置都无法通过其他方式轻松处理。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap