Friday, December 7, 2012

Creating executable jar with dependency using maven


Assembly plug-in is available in maven, which can be used to build jar including it's dependencies. We can associate this jar creation action with any one of maven phases. The sample code to build executable jar with dependency is as mentioned below -

<build>
  <plugins>
   <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <finalName>custom-name</finalName>
     <appendAssemblyId>false</appendAssemblyId>
     <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
     <archive>
      <manifest>
       <mainClass>StarpApp</mainClass>
      </manifest>
     </archive>
    </configuration>
    <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>
   </plugin>
  </plugins>
</build>

References -
http://maven.apache.org/plugins/maven-assembly-plugin/
http://salilstock.blogspot.in/2012/03/maven-build-and-dependency-management.html

No comments:

Post a Comment