How to create executable jar with maven

This post explain you how to create executable jar file with maven.

Here i used maven-jar-plugin to create executable jar files. Please refer following pom.xml.

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 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.elitejavacoder</groupId>
	<artifactId>commons</artifactId>
	<packaging>jar</packaging>
	<name>commons</name>
	<version>1.0.0-SNAPSHOT</version>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.elitejavacoder.MainClass</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Change com.elitejavacoder.MainClass with your fully qualified class name of your main class.

Now build your project using mvn clean install command.

Check MANIFEST.MF file under the META-INF directory inside jar file. It should contain following entry.

Main-Class: com.elitejavacoder.MainClass (in your case fully qualified class name of your main class).

Now you can run this jar file using following command from command prompt.

java -jar commons-1.0.0-SNAPSHOT.jar