简体   繁体   English

JAR清单文件 - 规范和实现之间的差异

[英]JAR Manifest file - Difference between Specification and Implementation

I want to add versioning information (and possibly some other metadata about the jar) to a jar of a library I created. 我想在我创建的库的jar中添加版本信息(可能还有一些关于jar的其他元数据)。 However, I am not sure what attribute to use. 但是,我不确定使用什么属性。 I found that the specification as well the documentation explain that there can be a Specification-Version and an Implementation-Version (and a title and vendor for both). 我发现规范以及文档说明可以有Specification-VersionImplementation-Version (以及两者的标题和供应商)。 But neither properly explains what the difference between Specification and Implementation is. 但是没有正确解释规范和实现之间的区别。

I also looked at different examples. 我也看了不同的例子。

  • The one from the documentation uses a human readable name for the Specification-Title and a package name for the Implementation-Title. 文档中的一个使用了Specification-Title的可读名称和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 gradle教程似乎只是使用Implementation-Version和一个人类可读的字符串来实现Implementation-Title
  • In another question I found an example in which there were several Implementation-Versions for different packages. 另一个问题中,我找到了一个示例,其中有不同包的几个实现版本。

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? 它甚至扮演了我放在那里的角色吗?

The meaning of each is explained in the documentation of java.lang.Package . 每个的含义都在java.lang.Package文档中解释。

The Specification-Version must consist of sequences of ASCII digits, separated by ASCII periods. 规范版本必须由ASCII数字序列组成,由ASCII句点分隔。 No other characters are allowed, periods cannot be at the start or end of the value, and consecutive periods are not allowed. 不允许使用其他字符,句点不能位于值的开头或结尾,并且不允许连续句点。

The Implementation-Version is a free-form string. Implementation-Version是一个自由格式的字符串。 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. 许多Java技术使用Specification-Version作为解决依赖关系的手段。 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. 如果某个程序说它需要,比如版本2.1或更高版本的JMF库,一些Java环境将使用匹配的Specification-Title分析每个清单的Specification-Version中的数字,并确保正确的版本(和没有其他版本)在运行时在类路径中可用。

In fact, the Package.isCompatibleWith method does that very check. 实际上, Package.isCompatibleWith方法可以检查。 You can even use it to check for a minimum Java version: 您甚至可以使用它来检查最低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. 以上内容在Java 9模块化应用程序中不起作用。 From the documentation for java.lang.Package : 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. 作为模块运行的Java 9程序应该使用ModuleDescriptor.version() Note that the ModuleDescriptor.Version class is Comparable: 请注意, 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.");
}

Well, the specification is your contract, eg: 那么,规范就是你的合同,例如:

  • a standard such as JAXB, JDBC or one of the various Java EE standards (eg EJB 2.x, EJB 3.0, EJB 3.1) 标准,如JAXB,JDBC或各种Java EE标准之一(例如EJB 2.x,EJB 3.0,EJB 3.1)
  • the API to your library, framework or service 您的库,框架或服务的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(以及规范版本)可能不会改变,但实施版本可能会随着您修复错误等而改变。

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规范的几个实现者之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM