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.
回答
META-INF文件夹是MANIFEST.MF文件的宿主。该文件包含有关JAR内容的元数据。例如,有一个名为Main-Class的条目,该条目使用可执行的JAR文件的静态main()指定Java类的名称。
回答
一般来说,我们不应该自己在META-INF中放入任何东西。相反,我们应该依靠任何用于打包JAR的东西。这是我认为Ant真正擅长的领域之一:指定JAR文件清单属性。像这样说很容易:
<jar ...> <manifest> <attribute name="Main-Class" value="MyApplication"/> </manifest> </jar>
至少,我认为这很容易... :-)
关键是应将META-INF视为内部Java元目录。不要惹它!我们要包含在JAR中的任何文件都应放在其他子目录中或者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中到底有什么问题。但这似乎是一个新兴的事实上的标准。或者,或者新兴的反模式:-)
回答
仅在此处添加信息,如果是WAR文件,则META-INF / MANIFEST.MF文件为开发人员提供了一种通过容器启动部署时间检查的功能,以确保容器可以找到应用程序的所有类。取决于。这样可以确保在我们错过JAR的情况下,我们不必等到应用程序在运行时崩溃就知道它丢失了。
回答
如果我们使用的是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