Jar with dependencies using maven

In the earlier post, though we had dependencies for our jar, we just used them while compiling them. Hence, when we execute our jar, it is required that, they be included in the class-path while executing our jar.

Though, this sounds reasonable enough, there are situations when it becomes tedious to include the dependencies in the class-path every-time you want to run the jar,especially, when there are more than a few dependencies and multiple main-classes. A simpler way is to bundle the other dependent jars in our jar.

Hmmm. This may sound a little off the mark. It is true you cannot bundle the dependent jars in your jar. Albeit, you can include the classes of your dependent jars in your jar. This will solve the problem. Whoa!!! how do i do it?

This can be done pretty easily using Maven in tandem with an assembly.xml. Following are the key points:

a. Use Maven Assembly plugin to assemble the class files in a jar and include their dependencies as well
b. Maven Assembly plugin requires an additional assembly.xml which in turn specifies format e.g. jar and depndency-set
c. Configure the assembly.xml as part of same project and use it from pom.xml using maven assembly plugin
d. Generate one single jar with all dependencies

The sample pom.xml looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.schoudari.blog</groupId>
  <artifactId>my-blog-reader</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>My Blog Reader</name>
  <properties>
    <log4j.version>1.2.16</log4j.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId><!--1.Use Maven assembly plugin to assemble the jar-->
        <configuration>
          <attach>false</attach>
          <descriptors>
            <descriptor>src/assemble/assembly.xml</descriptor><!--2.Assembly configuration-->
          </descriptors>
          <archive>
            <manifest>
              <mainClass>com.schoudar.blog.MyBlogReader</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>    
  </dependencies>

</project>

The sample assembly.xml is specified below:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Useful Links:
http://www.ibm.com/developerworks/java/library/j-5things13/index.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s