How to create a manifest file with Maven

本文介绍如何利用Maven插件生成包含主类定义及依赖路径的manifest清单文件,并将这些配置打包进最终的Jar文件中,使得Jar文件可直接运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


This tutorial will show you how to use the maven-jar-plugin to create a manifest file, and package / add it into the final jar file. The manifest file is normally used to define following tasks :

  1. Define the entry point of the Application, make the Jar executable.
  2. Add project dependency classpath.

When you run the command mvn package to package project into a Jar, the following meta-inf/manifest.mf file will be generated and added into the final Jar file automatically.

meta-inf/manifest.mf
Manifest-Version: 1.0
Built-By: ${user.name}
Build-Jdk: ${java.version}
Created-By: Apache Maven
Archiver-Version: Plexus Archiver

1. Make the Jar executable

Define maven-jar-plugin in pom.xml, and configure the manifest file via configuration tag.

pom.xml
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
                <mainClass>com.mkyong.core.App</mainClass>
            </manifest>
          </archive>
        </configuration>
    </plugin>

Following manifest file will be generated. If you run this Jar, it will execute the com.mkyong.core.App.

meta-inf/manifest.mf
anifest-Version: 1.0
Built-By: mkyong
Build-Jdk: 1.6.0_35
Created-By: Apache Maven
Main-Class: com.mkyong.core.App
Archiver-Version: Plexus Archiver

2. Add project dependency classpath.

Most Java projects need dependency, and it can define in manifest file easily. Normally, you will use maven-dependency-plugin to copy project dependencies to somewhere else.

pom.xml
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.mkyong.core.App</mainClass>
                <classpathPrefix>dependency-jars/</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
  </plugin>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>
                          ${project.build.directory}/dependency-jars/
                    </outputDirectory>
                </configuration>
            </execution>
        </executions>
  </plugin>

Following manifest file will be generated. The project dependencies will be copied to {project}/target/dependency-jars/.

meta-inf/manifest.mf
manifest-Version: 1.0
Built-By: mkyong
Build-Jdk: 1.6.0_35
Class-Path: dependency-jars/log4j-1.2.17.jar
Created-By: Apache Maven
Main-Class: com.mkyong.core.App
Archiver-Version: Plexus Archiver

Download Source Code

Download it –  Generate-Manifest-Using-Maven.zip (7 KB).

References

  1. Maven manifest references
  2. Maven manifest examples
  3. How to create a Jar file with Maven
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值