This tutorial'll show you and 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 exe Cutable. Add Project Dependency Classpath.
When you run the command MVN package to package project into a Jar, the following META-INF/MANIFEST.MF file would be genera Ted 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
Copy
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>
Copy
Following manifest file would be generated. If you run this Jar, it'll 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
Copy
2. Add Project dependency classpath.
Most Java projects need dependency, and it can define in manifest file easily. Normally, you'll use Maven-dependency-plugin to copy project dependencies to somewhere else. Pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-jar-plugi
n</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</ARTIFAC
tid> <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/&L
t;/outputdirectory> </configuration> </execution> </executions> </plugin>
Copy
Following manifest file would be generated. The project dependencies is 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
Copy
Download Source CodeDownload It–generate-manifest-using-maven.zip (7 KB).
ReferencesMaven manifest references maven manifest examples How to create a Jar file with Maven