Saturday, June 8, 2013

Maven package all jar dependencies into one jar

Many times you would have wanted to combine your main class code along with all dependent project jars into single jar which helps in easy distribution of your project. You can acheive the same using below maven plugin.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.yatra.solrtcperformance.poc.TestTCPOC</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

To generate the assembled jar, execute below command
  • mvn assembly:assembly -DdescriptorId=jar-with-dependencies
If you want to do this as part of regular maven package command, then include below execution tag after configuration tag  in above pom
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>

Courtesy : http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven