Tuesday, April 10, 2012

Build an application plus a separate library uber-jar using Maven

I've been working on a small Java application with a colleague to simulate animal movements and look at the efficiency of different survey methods. It uses the GeoTools library to support map projections and shapefile output. GeoTools is great but comes at a cost in terms of size: the jar for our little application alone is less than 50kb but bundling it with GeoTools and its dependencies blows that out to 20Mb.

The application code has been changing on a daily basis as we explore ideas, add features and fix bugs. Working with my colleague at a distance, over a fairly feeble internet connection, I wanted to package the static libraries and the volatile application into separate jars so that he only needed to download the former once (another option would have been for my colleague to set up a local Maven repository but for various reasons this was impractical).

A slight complication with bundling GeoTools modules into a single jar (aka uber-jar) is that individual modules make extensive use of META-INF/services files which need to be merged properly. The Maven Shade plugin does this nicely and I'd previously used it when creating single, executable jars for GeoTools projects. For the current case I realised all I needed to do was set up a multi-module project with one module for the application and another, inside a profile, for the combined libraries jar.

Following is the top level POM. All of the application dependencies are declared here to be inherited by the child application and libraries uber-jar POMs. Note that we define a profile "libs" to enclose the module for the libraries jar:

<?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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.cafeanimal</groupId>
<artifactId>trapsimulation</artifactId>
<packaging>pom</packaging>

<version>0.1-SNAPSHOT</version>
<name>Fauna trapping simulation</name>

<properties>
<geotools.version>8.0-M3</geotools.version>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>

<!-- Lots of other dependencies omitted here for brevity -->

</dependencies>

<modules>
<module>app</module>
</modules>

<!-- specify -Dlibs to build the lib module with its uber-jar of dependencies -->
<profiles>
<profile>
<id>libs</id>
<activation>
<property>
<name>libs</name>
</property>
</activation>

<modules>
<module>lib</module>
</modules>

</profile>
</profiles>

</project>


The Maven module for the application resides in a sub-directory (app) of the parent project directory. Since all of the dependencies were declared in the parent POM there is very little required for the application itself:

<?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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.cafeanimal</groupId>
<artifactId>trapsimulation</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>

<artifactId>app</artifactId>
<name>Simulation app</name>
<packaging>jar</packaging>

</project>


The Maven module for the libraries uber-jar is in another sub-directory (lib) of the parent project directory. It references the Maven Shade plugin which will merge the GeoTools META-INF/services files and combine the individual jars for the many dependencies into a single jar:

<?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>

<parent>
<artifactId>trapsimulation</artifactId>
<groupId>org.cafeanimal</groupId>
<version>0.1-SNAPSHOT</version>
</parent>

<artifactId>lib</artifactId>
<packaging>jar</packaging>
<name>Library uber-jar</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- This transformer combines the META-INF/services files from the individual GeoTools modules -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>


Now to build both the application and the libraries jar from the command line, cd to the parent project directory and enter:
mvn clean install -Dlibs

To build the application alone, simply omit the -Dlibs profile variable.

1 comment:

  1. I had a similar problem. I implemented what you have suggested here. Thanks a lot! KUTGW!

    ReplyDelete