在Java中,您经常会看到包含一些元文件的meta - inf文件夹。这个文件夹的目的是什么?我可以在那里放什么?
当前回答
只是在这里添加信息,如果是WAR文件,则是META-INF/MANIFEST。MF文件为开发人员提供了由容器发起部署时检查的工具,以确保容器可以找到应用程序所依赖的所有类。这确保了万一您丢失了一个JAR,您不必等到应用程序在运行时崩溃时才意识到它丢失了。
其他回答
来自官方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中。
我注意到一些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中,到底有什么问题。但这似乎是一个新兴的事实标准;要么这样,要么出现反模式:-)
所有答案都是正确的。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.
如果您使用的是JPA1,则可能必须在其中放入一个persistence.xml文件,该文件指定您可能想要使用的持久化单元的名称。持久化单元提供了一种方便的方式,可以指定一组元数据文件、类和包含分组中要持久化的所有类的jar。
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
// ...
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(persistenceUnitName);
点击这里查看更多信息: http://www.datanucleus.org/products/datanucleus/jpa/emf.html
只是在这里添加信息,如果是WAR文件,则是META-INF/MANIFEST。MF文件为开发人员提供了由容器发起部署时检查的工具,以确保容器可以找到应用程序所依赖的所有类。这确保了万一您丢失了一个JAR,您不必等到应用程序在运行时崩溃时才意识到它丢失了。
推荐文章
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?