Building an executable jar with Maven
This is one of the most commonly asked questions on Maven.
How to build executable jar with maven.
The idea is to build a jar with all its dependencies so that you can execute java – <nameof jarfile.jar>
Here is how to do it in maven. Add this to your pom.xml
<build>
<plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.nameOfCLass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
You can run mvn assembly:assembly and a jar file with all the dependencies will be built.
This is good if this is a standalone project. But if you have a parent pom and there is sub project which needs an executable jar, use goal single to build it along with the rest of the application.
Here is the pom.xml for this
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.nameOfCLass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now if you run as mvn single package or just mvn, it will build the executable jar along with the rest of the project.









Thanks Samarth. We did not face this problem but I am sure some of the readers might face this issue.
But there is problem when you use overlapping resources(like .properties e.g. spring.handlers or spring.schemas). Then you need to append them rather than overwriting. This can be achieved using shade. To see how it works http://xebee.xebia.in/2010/12/31/create-an-executable-jar-using-maven-shade-plugin/
The idea is that the executable jar would be run independent of main package. For eg if you have a project which builds .war file, we are building a jar core.jar which has core classes. Now I also want to create a core.jar with all the dependencies because I want to run on command prompt a main class.
When I am packaging as .war I will include core.jar and exclude core-with-dependencies.jar
But will not this kind of mashed-together jars give you e.g. resource conflicts?
How do you control the order that dependencies are loaded if they are put in the same jar?
One scenario is when your application overrides a resource file from one of the dependencies. This works if your classpath includes your jars/classes before the deps. But when they are located in the same jar, in the same path, with the same name …?
That is true. But that gives duplicate class warnings.
http://www.skill-guru.com/blog/2010/12/24/duplicate-class-warnings-when-using-shade-plugin/
There’s also a Shade plugin that builds up an executable uberjar.