Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Mike
5 years ago

The link for the source code is not valid

James McGill
9 years ago

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.

Authentical
4 years ago

This example is overcomplicated. Use this

pastebin bGjQgwge

Betty
4 years ago
Reply to  Authentical

This pastebin example does not work on my end. (I am trying to include a dependency from the remote maven repository)

Authentical
4 years ago

Here’s a simpler, complete pom snippet and more to-the-point example:


org.apache.maven.plugins
maven-jar-plugin
3.1.0

true
lib/
YOUR.BLABLA

kiran
6 years ago

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

Ashkan
6 years ago

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

Mikaël
6 years ago

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
6 years ago

I get a 404 when trying to download the source code

Denis Ezhov
7 years ago

Thanks! You save my time!

dalepres
8 years ago

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.

Florian Courtial
10 years ago

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

Authentical
4 years ago

Simpler and clearer:


org.apache.maven.plugins
maven-jar-plugin
3.1.0

true
lib/
COM.DEMO

PyaePhyo
11 years ago

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

Ewan
11 years ago

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?

Seigo Osawa
9 years ago
Reply to  Ewan

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” .

AV
9 years ago
Reply to  Ewan

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

Dscho
11 years ago

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.