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 :
- Define the entry point of the Application, make the Jar executable.
- 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.
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.
<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
.
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.
<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/
.
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
The link for the source code is not valid
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.
This example is overcomplicated. Use this
pastebin bGjQgwge
This pastebin example does not work on my end. (I am trying to include a dependency from the remote maven repository)
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
maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e.
Thanks for your great site. You saved me much time in several parts of my project, and I appreciate that.
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.
I get a 404 when trying to download the source code
Thanks! You save my time!
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.
Thanks ! The only way I found to add jar dependency to a maven project.
Simpler and clearer:
org.apache.maven.plugins
maven-jar-plugin
3.1.0
true
lib/
COM.DEMO
how to create jsf + spring + hibernate integration project using maven 3.0.4 ?
https://mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/
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?
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” .
Same query, how can In include depended jar in superman jar
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.