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.
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 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
Copy
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>
Copy
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
Copy
Download Source Code
Download it – Generate-Manifest-Using-Maven.zip (7 KB).
References
- Maven manifest references
- Maven manifest examples
- How to create a Jar file with Maven
还没有评论,来说两句吧...