Java JAR 清单文件 - 规范和实现之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20994766/
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-13 05:48:09  来源:igfitidea点击:

JAR Manifest file - Difference between Specification and Implementation

javajarmanifest.mf

提问by Joachim Kurz

I want to add versioning information (and possibly some other metadata about the jar) to a jar of a library I created. However, I am not sure what attribute to use. I found that the specificationas well the documentationexplain that there can be a Specification-Versionand an Implementation-Version(and a title and vendor for both). But neither properly explains what the difference between Specification and Implementation is.

我想将版本信息(可能还有一些关于 jar 的其他元数据)添加到我创建的库的 jar 中。但是,我不确定要使用什么属性。我发现规范文档都解释了可以有一个Specification-Version和一个Implementation-Version(以及两者的标题和供应商)。但是两者都没有正确解释规范和实现之间的区别是什么。

I also looked at different examples.

我也看了不同的例子。

  • The one from the documentationuses a human readable name for the Specification-Title and a package name for the Implementation-Title. A dot-separated version number is used for the Specification-Version while a simple build number is used for the Implementation-Version.
  • The gradle tutorial seems to just use an Implementation-Version and a human-readable string for the Implementation-Title
  • In another questionI found an example in which there were several Implementation-Versions for different packages.
  • 从所述一个文件使用为规范-标题和用于执行所有权的包名称的人类可读的名称。以点分隔的版本号用于规范版本,而简单的内部版本号用于实现版本。
  • gradle 教程似乎只使用一个实现版本和一个人类可读的字符串作为实现标题
  • 另一个问题中,我找到了一个示例,其中针对不同的包有多个实现版本。

What exactly is the difference between the Specification and Implementation Metadata here? How should these different attributes (especially the version numbers) be used? How does it make sense that the vendor of the specification and the implementation are different?

这里的规范和实现元数据之间到底有什么区别?应该如何使用这些不同的属性(尤其是版本号)?规范的供应商和实现的不同有什么意义?

Does it even play a role what I put in there?

它甚至发挥了我放在那里的作用吗?

采纳答案by VGR

The meaning of each is explained in the documentation of java.lang.Package.

java.lang.Package文档中解释了每个的含义。

The Specification-Versionmust consist of sequences of ASCII digits, separated by ASCII periods. No other characters are allowed, periods cannot be at the start or end of the value, and consecutive periods are not allowed.

部分规格版本必须由ASCII数字,由ASCII句点分隔的序列。不允许使用其他字符,句点不能位于值的开头或结尾,并且不允许出现连续的句点。

The Implementation-Versionis a free-form string. It can have any format.

实施-版本是无格式的字符串。它可以是任何格式。

Specification-Version is always associated with a package. If you specify it for the entire manifest instead of for a specific package, it applies to all packages in the .jar file.

规范版本始终与包相关联。如果您为整个清单而不是特定包指定它,则它适用于 .jar 文件中的所有包。

The Specification-Version is used by a number of Java technologies as a means of resolving dependencies. If some program says it needs, say, version 2.1 or later of the JMF library, some Java environments will analyze the numbers in the Specification-Version of each manifest with a matching Specification-Title, and will make sure that the correct version (and no other version) is available in the classpath at runtime.

规范版本被许多 Java 技术用作解决依赖关系的手段。如果某个程序说它需要 JMF 库的 2.1 版或更高版本,则某些 Java 环境将使用匹配的规范标题分析每个清单的规范版本中的数字,并确保正确的版本(和没有其他版本)在运行时在类路径中可用。

In fact, the Package.isCompatibleWithmethod does that very check. You can even use it to check for a minimum Java version:

事实上,Package.isCompatibleWith方法会进行非常检查。您甚至可以使用它来检查最低 Java 版本:

if (System.class.getPackage().isCompatibleWith("1.6")) {
    System.out.println("Running in Java 1.6 or later.");
}

Update

更新

The above will not work in a Java 9 modular application. From the documentation for java.lang.Package:

以上在 Java 9 模块化应用程序中不起作用。从java.lang.Package文档中

A Package automatically defined for classes in a named module has the following properties:

? The specification and implementation titles, versions, and vendors are unspecified.

A Package automatically defined for classes in an unnamed module has the following properties:

? The specification and implementation titles, versions, and vendors are unspecified.

为命名模块中的类自动定义的包具有以下属性:
...
? 未指定规范和实现标题、版本和供应商。

为未命名模块中的类自动定义的包具有以下属性:
...
? 未指定规范和实现标题、版本和供应商。

Java 9 programs running as modules should use ModuleDescriptor.version()instead. Note that the ModuleDescriptor.Version classis Comparable:

作为模块运行的 Java 9 程序应该使用ModuleDescriptor.version()代替。请注意ModuleDescriptor.Version 类是 Comparable :

Module libraryModule = SomeLibraryClass.class.getModule();
Optional<ModuleDescriptor.Version> libraryVersion =
    libraryModule.getDescriptor().version();

ModuleDescriptor.Version minimumRequiredVersion =
    ModuleDescriptor.Version.parse("2.0");

if (libraryVersion.isPresent() &&
    minimumRequiredVersion.compareTo(libraryVersion.get()) >= 0) {

    System.out.println(libraryModule + " is version " +
        minimumRequiredVersion + " or later.");
}

回答by Puce

Well, the specification is your contract, e.g.:

好吧,规范是您的合同,例如:

  • a standard such as JAXB, JDBC or one of the various Java EE standards (e.g. EJB 2.x, EJB 3.0, EJB 3.1)
  • the API to your library, framework or service
  • 一种标准,例如 JAXB、JDBC 或各种 Java EE 标准之一(例如 EJB 2.x、EJB 3.0、EJB 3.1)
  • 库、框架或服务的 API

The implementation is, well, the implementation of that specification.

实现就是该规范的实现。

And while the API of the specification (and thus the specification version) might not change, the implementation version might change as you're fixing bugs etc.

虽然规范的 API(以及规范版本)可能不会改变,但实现版本可能会随着您修复错误等而改变。

回答by McDowell

Consider the servlet-api.jar manifest from Apache Tomcat:

考虑来自 Apache Tomcat 的 servlet-api.jar 清单:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_40-b43 (Oracle Corporation)
X-Compile-Source-JDK: 1.7
X-Compile-Target-JDK: 1.7

Name: javax/servlet/
Specification-Title: Java API for Servlets
Specification-Version: 3.1
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: javax.servlet
Implementation-Version: 3.1.FR
Implementation-Vendor: Apache Software Foundation

Apache are one of several implementators of the Servlet 3.1 specification defined by the JCP(founded by Sun.)

Apache 是JCP(由 Sun 创立)定义的 Servlet 3.1 规范的几个实现者之一。