Creating an executable jar file through maven with all dependecies added :
Requirement :
To pack a little app with all dependencies [external jars ] through maven and run it from the console.
maven-assembly-plugin can be used to assemble all the dependent jar into the same executable jar so that we do not need to pass the classpath of dependent jars while running the executable jar.
To do it just add the following to pom.xml and make sure we write the correct path to our main class.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.test.MavenBuildTest</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
When the maven package gets executed, two .jar files will be created.
The one with the text jar-with-dependencies on its name,
will contain is the one we should run. To do it, just go to the terminal,
navigate to it(inside the target folder) and type the command:
java -jar myapp-jar-with-dependencies.jar
Here is complete pom Example:
<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>MavenBuildTest</groupId>
<artifactId>MavenBuildTest</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>MavenBuildTest</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>
<finalName>MavenBuildTest</finalName>
</build>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.3</version>
<!-- <scope>system</scope>
<systemPath>${basedir}\src\lib\jackson-databind-2.4.1.3.jar</systemPath> -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
<!-- <scope>system</scope>
<systemPath>${basedir}\src\lib\jackson-annotations-2.4.1.jar</systemPath> -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1.1</version>
<!-- <scope>system</scope>
<systemPath>${basedir}\src\lib\jackson-core-2.4.1.1.jar</systemPath> -->
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<!-- <scope>system</scope>
<systemPath>${basedir}\src\lib\json-simple-1.1.1.jar</systemPath> -->
</dependency>
</dependencies>
<packaging>jar</packaging>
</project>