在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
你也可以在那里放置静态资源。
在示例:
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模式中引用您喜欢的数字证书。
一般来说,您自己不应该在META-INF中放入任何东西。相反,您应该依赖于您用来打包JAR的任何东西。这是我认为Ant真正擅长的领域之一:指定JAR文件清单属性。这样说很容易:
<jar ...>
<manifest>
<attribute name="Main-Class" value="MyApplication"/>
</manifest>
</jar>
至少,我认为这很简单……: -)
关键是meta - inf应该被视为一个内部Java元目录。别乱动它!您希望包含在JAR中的任何文件都应该放在其他子目录中或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是一个特殊的文件夹,ClassLoader将其与jar中的其他文件夹区别对待。 嵌套在META-INF文件夹内的元素不会与它外部的元素混合。
把它想象成另一个根。从Enumerator<URL> ClassLoader#getSystemResources(字符串路径)方法等透视图:
当给定的路径以“META-INF”开头时,该方法搜索类路径中所有jar的META-INF文件夹内嵌套的资源。
当给定的路径不是以“META-INF”开头时,该方法在类路径中所有jar和目录的所有其他文件夹(在META-INF之外)中搜索资源。
如果您知道getSystemResources方法特别处理的另一个文件夹名称,请对其进行评论。