Java META-INF 的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/70216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:55:04  来源:igfitidea点击:

What's the purpose of META-INF?

javameta-inf

提问by Kristian

In Java, you often see a META-INF folder containing some meta files. What is the purpose of this folder and what can I put there?

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

采纳答案by Daniel Spiewak

Generally speaking, you should not put anything into META-INF yourself. Instead, you should rely upon whatever you use to package up your JAR. This is one of the areas where I think Ant really excels: specifying JAR file manifest attributes. It's very easy to say something like:

一般来说,你不应该自己在 META-INF 中放入任何东西。相反,您应该依赖于您用来打包 JAR 的任何内容。这是我认为 Ant 真正擅长的领域之一:指定 JAR 文件清单属性。很容易说这样的话:

<jar ...>
    <manifest>
        <attribute name="Main-Class" value="MyApplication"/>
    </manifest>
</jar>

At least, I think that's easy... :-)

至少,我认为这很容易... :-)

The point is that META-INF should be considered an internal Java metadirectory. Don't mess with it! Any files you want to include with your JAR should be placed in some other sub-directory or at the root of the JAR itself.

关键是 META-INF 应该被视为内部 Java目录。不要惹它!您想要包含在 JAR 中的任何文件都应该放在其他一些子目录或 JAR 本身的根目录中。

回答by aku

From the official JAR File Specification(link goes to the Java 7 version, but the text hasn't changed since at least v1.3):

来自官方 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 目录

Java 2 平台识别并解释 META-INF 目录中的以下文件/目录以配置应用程序、扩展、类加载器和服务:

  • MANIFEST.MF

用于定义扩展和包相关数据的清单文件。

  • INDEX.LIST

此文件由-ijar 工具的新“ ”选项生成,其中包含应用程序或扩展中定义的包的位置信息。它是 JarIndex 实现的一部分,被类加载器用来加速他们的类加载过程。

  • x.SF

JAR 文件的签名文件。“x”代表基本文件名。

  • x.DSA

与具有相同基本文件名的签名文件关联的签名块文件。该文件存储相应签名文件的数字签名。

  • services/

该目录存储所有服务提供者配置文件。

回答by Brian Matthews

The META-INF folder is the home for the MANIFEST.MFfile. This file contains meta data about the contents of the JAR. For example, there is an entry called Main-Class that specifies the name of the Java class with the static main() for executable JAR files.

META-INF 文件夹是MANIFEST.MF文件的主目录。此文件包含有关 JAR 内容的元数据。例如,有一个名为 Main-Class 的条目,它使用静态 main() 为可执行 JAR 文件指定 Java 类的名称。

回答by user38748

I've noticed that some Java libraries have started using META-INF as a directory in which to include configuration files that should be packaged and included in the CLASSPATH along with JARs. For example, Spring allows you to import XML Files that are on the classpath using:

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

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

In this example, I'm quoting straight out of the Apache CXF User Guide. On a project I worked on in which we had to allow multiple levels of configuration via Spring, we followed this convention and put our configuration files in META-INF.

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

When I reflect on this decision, I don't know what exactly would be wrong with simply including the configuration files in a specific Java package, rather than in META-INF. But it seems to be an emerging de facto standard; either that, or an emerging anti-pattern :-)

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

回答by sasuke

Just to add to the information here, in case of a WAR file, the META-INF/MANIFEST.MF file provides the developer a facility to initiate a deploy time check by the container which ensures that the container can find all the classes your application depends on. This ensures that in case you missed a JAR, you don't have to wait till your application blows at runtime to realize that it's missing.

只是在这里添加信息,如果是 WAR 文件,META-INF/MANIFEST.MF 文件为开发人员提供了一个工具来启动容器的部署时间检查,以确保容器可以找到您的应用程序的所有类取决于。这可确保万一您错过了 JAR,您不必等到应用程序在运行时崩溃时才意识到它丢失了。

回答by f0ster

If you're using JPA1, you might have to drop a persistence.xmlfile in there which specifies the name of a persistence-unit you might want to use. A persistence-unit provides a convenient way of specifying a set of metadata files, and classes, and jars that contain all classes to be persisted in a grouping.

如果您使用的是 JPA1,您可能必须persistence.xml在其中放置一个文件,该文件指定您可能想要使用的持久性单元的名称。持久化单元提供了一种方便的方式来指定一组元数据文件、类和包含要在分组中持久化的所有类的 jar。

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

// ...

EntityManagerFactory emf =
      Persistence.createEntityManagerFactory(persistenceUnitName);

See more here: http://www.datanucleus.org/products/datanucleus/jpa/emf.html

在此处查看更多信息:http: //www.datanucleus.org/products/datanucleus/jpa/emf.html

回答by Steve Cohen

I have been thinking about this issue recently. There really doesn't seem to be any restriction on use of META-INF. There are certain strictures, of course, about the necessity of putting the manifest there, but there don't appear to be any prohibitions about putting other stuff there.

我最近一直在思考这个问题。对于 META-INF 的使用似乎真的没有任何限制。当然,对于将清单放在那里的必要性有一定的限制,但似乎没有任何禁止将其他东西放在那里。

Why is this the case?

为什么会这样?

The cxf case may be legit. Here's another place where this non-standard is recommended to get around a nasty bug in JBoss-ws that prevents server-side validation against the schema of a wsdl.

cxf 案例可能是合法的。这是另一个地方,建议使用此非标准来解决 JBoss-ws 中的一个令人讨厌的错误,该错误阻止服务器端针对 wsdl 的模式进行验证。

http://community.jboss.org/message/570377#570377

http://community.jboss.org/message/570377#570377

But there really don't seem to be any standards, any thou-shalt-nots. Usually these things are very rigorously defined, but for some reason, it seems there are no standards here. Odd. It seems like META-INF has become a catchall place for any needed configuration that can't easily be handled some other way.

但似乎真的没有任何标准,任何你不应该做的。通常这些东西的定义非常严格,但不知为何,这里似乎没有标准。奇怪的。似乎 META-INF 已成为无法以其他方式轻松处理的任何所需配置的综合场所。

回答by Grim

You can also place static resources in there.

您还可以在其中放置静态资源。

In example:

例如:

META-INF/resources/button.jpg 

and get them in web3.0-container via

并通过 web3.0-container 获取它们

http://localhost/myapp/button.jpg

> Read more

> 阅读更多

The /META-INF/MANIFEST.MF has a special meaning:

/META-INF/MANIFEST.MF 有特殊的意义:

  1. If you run a jar using java -jar myjar.jar org.myserver.MyMainClassyou can move the main class definition into the jar so you can shrink the call into java -jar myjar.jar.
  2. You can define Metainformations to packages if you use java.lang.Package.getPackage("org.myserver").getImplementationTitle().
  3. You can reference digital certificates you like to use in Applet/Webstart mode.
  1. 如果您使用运行 jar,java -jar myjar.jar org.myserver.MyMainClass您可以将主类定义移动到 jar 中,以便您可以将调用缩小到java -jar myjar.jar.
  2. 如果您使用java.lang.Package.getPackage("org.myserver").getImplementationTitle().
  3. 您可以在 Applet/Webstart 模式下引用您喜欢使用的数字证书。

回答by EliuX

META-INF in Maven

Maven 中的 META-INF

In Maven the META-INFfolder is understood because of the Standard Directory Layout, which by name convention package your project resources within JARs: any directories or files placed within the ${basedir}/src/main/resourcesdirectory are packaged into your JAR with the exact same structure starting at the base of the JAR. The Folder ${basedir}/src/main/resources/META-INFusually contains .propertiesfiles while in the jar contains a generated MANIFEST.MF, pom.properties, the pom.xml, among other files. Also frameworks like Springuse classpath:/META-INF/resources/to serve web resources. For more information see How do I add resources to my Maven Project.

在 Maven 中META-INF文件夹被理解是因为标准目录布局,它按照命名约定将您的项目资源打包在 JAR 中:放置在${basedir}/src/main/resources目录中的任何目录或文件都打包到您的 JAR 中从 JAR 的底部开始具有完全相同的结构。文件夹${basedir}/src/main/resources/META-INF通常包含.properties文件,而在 jar 中包含生成的MANIFEST.MFpom.propertiespom.xml等文件。另外像框架春季使用classpath:/META-INF/resources/服务的网络资源。有关更多信息,请参阅如何将资源添加到我的 Maven 项目

回答by Tugrul

All answers are correct. Meta-inf has many purposes. In addition, here is an example about using tomcat container.

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

Go to Tomcat Docand check " Standard Implementation > copyXML" attribute.

转到 Tomcat Doc并检查“标准实施 > copyXML”属性。

Description is below.

说明如下。

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.

如果您希望在部署应用程序时将嵌入在应用程序中的上下文 XML 描述符(位于 /META-INF/context.xml)复制到拥有主机的 xmlBase,则设置为 true。在后续启动时,复制的上下文 XML 描述符将优先于嵌入在应用程序中的任何上下文 XML 描述符使用,即使嵌入在应用程序中的描述符是更新的。该标志的值默认为 false。请注意,如果拥有 Host 的 deployXML 属性为 false 或拥有 Host 的 copyXML 属性为 true,则此属性将不起作用。