Post

Maven - Creating an executable jar

I wanted to create an executable jar in maven , s.t whenever I run”mvn install” , it will generate both the regular one and the executable one.

all the data here is taken from this stackoverflow question and this maven example page.

In your pom.xml file , add this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<build>
	<finalName>JarFilename</finalName>
	......
	<plugins>
		<plugin>
			<artifactId>maven-assembly-plugin</artifactId>
			<configuration>
				<archive>
					<manifest>
						<mainClass>fully.qualified.ClassName</mainClass>
					</manifest>
				</archive>
				<descriptorRefs>
					<descriptorRef>jar-with-dependencies</descriptorRef>
				</descriptorRefs>
			</configuration>
			<executions>
				<execution>
					<id>make-assembly</id>
                                        <phase>package</phase>
					<goals>
						<goal>single</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

**Short explanation : **
The artifactId is the plugin name (no packageId is required , because it’s from the default maven package).
In the configuration we define the location of the main class , and which plugin we’re running
And then , the execution : that tells the plugin when to run. In this case , it will run in the packaging phase.

How to run
All you need to do is run

1
mvn install 

if you want only to create the executable jar , you can run

1
mvn assembly:assembly 
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.