Archive

Posts Tagged ‘Maven’

Sharing EAR libraries with Maven and skinnyWars

This is not a new feature, but I have seen many applications with EAR-files that include the same dependencies in multiple modules, some times with different and possibly conflicting versions. The same library is found in WEB-INF/lib for multiple WAR-files, or both in WEB-INF/lib for a WAR and at the EAR level for EJB modules. This is inefficient and may cause subtle bugs. Ideally all shared libraries should be at the EAR level.

The solution with Maven is to use the skinnyWars option for the EAR:


<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-ear-plugin</artifactId>
      <version>2.9.1</version>
      <configuration>
        <defaultLibBundleDir>lib/</defaultLibBundleDir>
        <skinnyWars>true</skinnyWars>
        <archive>
          <manifestEntries>
            <Implementation-Version>${project.version}</Implementation-Version>
          </manifestEntries>
        </archive>
        <modules>
          <webModule>
            <groupId>com.example</groupId>
            <artifactId>Example-Web</artifactId>
            <bundleFileName>Example-Web.war</bundleFileName>
            <moduleId>Example-Web.war</moduleId>
          </webModule>
          <!-- other modules -->
        </modules>
      </configuration>
    </plugin>
  </plugins>
</build>

Next, the shared libraries must be referenced as dependencies from the EAR’s pom.xml. When there are few shared libraries it is probably best to include them as dependencies both in the EAR and in the other modules. If there are many dependencies they can be extracted to a separate Maven project and configured in a single place. The Maven project with shared dependencies should be defined to use packaging pom and should be referenced as a dependency with type pom from the EAR and the respective modules. That is really all there is to it.

Note! The skinnyWars support has been improved over the years. Other descriptions mention the need to filter out shared libraries from WAR files with includes/excludes rules. I don’t do that. The war files are complete and can be used as is. The Maven EAR plugin is smart enough to remove the shared libraries from the bundled WAR-files when it builds the EAR-file.

Categories: Java