Build an executable jar with Maven

Recently I need to build an executable jar, Maven is no doubt the first handy choice.

It is really simple to build an executable jar using Maven. You just need to add below plugin with configuration to your pom file:

<pluginManagement> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.yourcompany.YourClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </pluginManagement>

However, if you have dependencies, you need to bundle those dependencies so as you can run the jar without specify those dependencies explicitly. You can use the assembly plugin to build the executable jar which will includes all the dependencies:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <goals> <goal>attached</goal> </goals> <phase>package</phase> <configuration> <descriptorRefs> <descriptorRef>jar-with- dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.yourcompany.YourClass</mainClass> </manifest> </archive> </configuration> </execution> </executions> </plugin>

comments powered by Disqus