How to create a manifest file with Maven

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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
kiran
Guest
kiran

maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e.

Ashkan
Guest
Ashkan

Thanks for your great site. You saved me much time in several parts of my project, and I appreciate that.

Mikaël
Guest
Mikaël

I kept getting a NoClassDefFoundError, and since I’m a beginner with Maven, I wasn’t very comfortable with the lengthy in-depth tutorials found elsewhere. This solved it for me. Thank you very much, your website is a huge help.

ray
Guest
ray

I get a 404 when trying to download the source code

Denis Ezhov
Guest
Denis Ezhov

Thanks! You save my time!

dalepres
Guest
dalepres

Mr. Kim, thanks for sharing your site and this article. While there is a lot to learn yet about using Maven for our team, your page has become a favorite in our searches.

James McGill
Guest
James McGill

When building with Eclipse (STS 3.6.2.RELEASE / Luna 4.4.1) the eclipse Maven build puts no manifest (and no META-INF at all) into the jars that get published. I think the maven-jar-plugin is ignored. The manifest does get built when I run maven myself. Same issue with “Pivotal tc Server v3.0” and with Tomcat 7.

Florian Courtial
Guest
Florian Courtial

Thanks ! The only way I found to add jar dependency to a maven project.

PyaePhyo
Guest
PyaePhyo

how to create jsf + spring + hibernate integration project using maven 3.0.4 ?

vermer
Guest
Ewan
Guest
Ewan

Hello,

thanks for example.
I downloaded and run it.
But generated superman-1.0.jar doesn’t contain dependency jar (log4j-1.2.17.jar) file.

What’s worng?

AV
Guest
AV

Same query, how can In include depended jar in superman jar

Seigo Osawa
Guest
Seigo Osawa

The usage of “jar” and “dependency” plugins in the article only tells maven to generate a jar and copy the dependencies to another location, but it doesn’t specify how to find that location.

Different deployment environments might have different needs, but to get a simple version running you can package the dependencies with the jar.

Take a look at “maven-assembly-plugin”, using the “descriptorRefs” element with “jar-with-dependencies” .

Dscho
Guest
Dscho

Note that META-INF/ is upper-case, and only on case-challenged platforms such as Windows it is kind of correct to use meta-inf/ instead.