Monday, 30 March 2015

Maven : Creating jar with dependencies

While working with the maven, it is a case that all the dependent jars will not be packed into the jar, whereas in case of war, all the dependent jars wil be packed into web/lib folder. If you want to create a simple jar with all its dependency jars then you need to create uber jar.(Uber means Super, extreme or outstanding). Here are the steps to create uber jar using maven.


  • Step 1 : Create an assembly xml and put it into your resources folder.
  • <assembly>
    <id>uberjar</id>
    <formats>
    <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
    <dependencySet>
    <unpack>false</unpack>
    <scope>runtime</scope>
    <useProjectArtifact>false</useProjectArtifact>
    </dependencySet>
    </dependencySets>
    <fileSets>
    <fileSet>
    <directory>${project.build.outputDirectory}</directory>
    <outputDirectory>/</outputDirectory>
    </fileSet>
    </fileSets>
    </assembly>
    view raw assembly.xml hosted with ❤ by GitHub
  • Step 2 : Include assembly.xml in your pom in the plugin-configuration node along with the manifest-mainclass.
  • <build>
    <pluginManagement>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
    <descriptors>
    <descriptor>src/main/resources/assembly.xml</descriptor>
    </descriptors>
    <archive>
    <manifest>
    <mainClass>com.bala.MyMainClass</mainClass>
    <addClasspath>true</addClasspath>
    </manifest>
    </archive>
    </configuration>
    </plugin>
    </plugins>
    </pluginManagement>
    </build>
    view raw partial-pom.xml hosted with ❤ by GitHub
  • Step 3 : Fire mvn assembly:assembly, it will create simple and uber jar in your target folder. 

No comments:

Post a Comment