May 19, 2012

Create a maven distribution package including a jar with dependencies

In this post I will show how to create a distribution package (zip/tar.gz) using maven. Package will contain the executable jar and other relevant file such as README, License etc.

First I will create the jar including all the dependencies. To do that I add following code to my pom.xml
<project>
 ...
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <archive>
                                <manifest>
                                    <addClasspath>true</addClasspath>
                                    <mainClass>HelloWorld.App</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                    <execution>
                        <id>dist</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/dist.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

In this pom.xml, it reffers to another xml file call dist.xml. This xml file contains all the details about the distribution package we are going to generate.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 
   http://maven.apache.org/xsd/assembly-1.1.2.xsd">
 <id>dist</id>
 <formats>
  <format>zip</format>
  <format>tar.gz</format>
 </formats>
 <files>
  <file>
   <source>pom.xml</source>
   <outputDirectory>/source</outputDirectory>
  </file>
  <file>
   <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source>
   <outputDirectory>/</outputDirectory>
  </file>
 </files>
 <fileSets>
  <fileSet>
   <directory>/src</directory>
   <outputDirectory>source/src</outputDirectory>
  </fileSet>
 </fileSets>
</assembly>

You should note following thing on pom.xml and dist.xml.

In pom.xml

  • Replace this " <mainclass<HelloWorld.App</mainclass> " with your main class.
  • Replace this " <descriptor>src/main/dist.xml</descriptor> " with your dist.xml file path.

In dist.xml

  • Use <files> to add files.
  • Use <fileSets> to add directories.
  • Use <outputDirectory> to specify the destination of each file/directory.

For more details : Maven assembly plugin examples

3 comments:

  1. what is maven command?

    mvn package do only jar

    ReplyDelete
    Replies
    1. Please refer : http://maven.apache.org/what-is-maven.html
      You can use this tool to build a java program including all dependency packages and create a single JAR archive.

      Delete

Your comments are always welcome ...