Jar Manifest File

Jar 包的描述清单文件。

我们先来 看下清单文件是什么。

The Manifest File

清单文件名为MANIFEST.MF,位于 Jar 包的 META-INF

目录。它是一个简单的键值对列表。如下所示

Manifest-Version: 1.0
Main-Class: io.github.himcs.todo.cli.BootStrap

这些键值对提供元数据,描述 Jar 的各种信息,例如包的版本,执行的类,类路径,签名等等

创建 Manifest File

默认 Manifest

默认,构建 Jar 包, 不常用

jar cf MyJar.jar classes/

查看生成的文件

Manifest-Version: 1.0
Created-By: 11.0.3 (AdoptOpenJDK)

Maven

清单文件会由工具生成。

例如,Maven 添加了额外的头

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.3.9
Built-By: baeldung
Build-Jdk: 11.0.3

我们可以添加自定义的头,在 pom 文件修改

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <archive>
            <manifest>
                <packageName>com.baeldung.java</packageName>
            </manifest>
            <manifestEntries>
                <Created-By>baeldung</Created-By>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

会生成下面的MANIFEST.MF文件

Manifest-Version: 1.0
Build-Jdk-Spec: 11
Package: com.baeldung.java
Created-By: baeldung

Maven Jar插件文档open in new window

Gradle

在构建脚本 build.gradle 添加

tasks.named('jar') {
    manifest {
        attributes('Implementation-Title': project.name,
                   'Implementation-Version': project.version)
    }
}

打包 ./gradlew jar

生成的 META-INF/MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: lib
Implementation-Version: 0.1.0

Gradle jar文档open in new window

Headers

主要的 Headers

  • Manifest-Version: the version of the specification
  • Created-By: the tool version and vendor that created the manifest file
  • Multi-Release: if true, then this is a Multi-Release Jaropen in new window
  • Built-By: this custom header gives the name of the user that created the manifest file

启动入口 和 ClassPath

  • Main-Class: 包含main方法的启动类
  • Class-Path: 空格分割的库或资源的相对路径列表
Main-Class: com.baeldung.Application
Class-Path: core.jar lib/ properties/

其他属性 参考

jar-manifestopen in new window

Last Updated:
Contributors: mcs