Maven – How to generate a custom manifest file

Here is a simple solution for generating custom manifest file while building a maven project. Look at our sample pom.xml file:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.itcuties.samples.maven</groupId>
  <artifactId>custom-manifest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies>
    <dependency>
      	<groupId>commons-lang</groupId>
      	<artifactId>commons-lang</artifactId>
      	<version>2.1</version>
    </dependency>
  </dependencies>
  
  <build>
      <plugins>
      		<plugin>
		      	<groupId>org.apache.maven.plugins</groupId>
        		<artifactId>maven-jar-plugin</artifactId>
        		<configuration>
          			<archive>
            			<manifestFile>src/main/resources/custom/MANIFEST.MF</manifestFile>
            			<manifest>
              				<addClasspath>true</addClasspath>
              			</manifest>
              			<manifestEntries>
              				<Built-By>itcuties</Built-By>
            			</manifestEntries>
          			</archive>
        		</configuration>
      		</plugin>
      </plugins>
  </build>

</project>

This configuration points to the custom MANIFEST.MF file. Here is the file

src/main/resources/custom/MANIFEST.MF

Custom-Attribute: Here is some custom value
Custom-Attribute-2: Here is another custom value

After you build your project with this configuration we will end up with this MANIFEST.MF file contents.

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Build-Jdk: 1.6.0_27
Built-By: itcuties
Class-Path: commons-lang-2.1.jar
Custom-Attribute: Here is some custom value
Custom-Attribute-2: Here is another custom value

Summary

If you want to add some custom attributes to your MANIFEST.MF from a file use this configuration:

<build>
	<plugins>
     	<plugin>
			<groupId>org.apache.maven.plugins</groupId>
      		<artifactId>maven-jar-plugin</artifactId>
       		<configuration>
         		<archive>
           			<manifestFile>src/main/resources/custom/MANIFEST.MF</manifestFile>
				</archive>
			</configuration>	
		</plugin>
	</plugins>
</build>		

If you want to add a Class-Path element containing all the dependencies to your MANIFEST.MF use this configuration:

<!-- Example dependency -->
<dependencies>
    <dependency>
      	<groupId>commons-lang</groupId>
      	<artifactId>commons-lang</artifactId>
      	<version>2.1</version>
    </dependency>
</dependencies>
...
<build>
	<plugins>
     	<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
					</manifest>
				</archive>
			</configuration>	
		</plugin>
	</plugins>
</build>

If you want to change a default MANIFEST.MF element value (like Build-By) or add a new one as well you can use this configuration:

<build>
	<plugins>
     	<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<archive>
					<manifestEntries>
              			<Built-By>itcuties</Built-By>
            		</manifestEntries>
				</archive>
			</configuration>	
		</plugin>
	</plugins>
</build>

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>