Wednesday, February 18, 2009

Maven Assemblies: Including Runtime Dependencies

I wanted to share a tip I picked up from Maven: The Definitive Guide
Previously, I thought you had to list every runtime dependency you wanted to include in a dependencySet:
   <dependencySets>
<dependencySet>
<includes>
<include>commons-dbcp:commons-dbcp</include>

</includes>
<outputDirectory>mule/lib/user</outputDirectory>
</dependencySet>
</dependencySets>
But all runtime dependencies can be brought into the assembly with an empty dependencySet element (runtime scope implicitly includes compile scope):
   <dependencySet/>
This will put all the runtime dependencies in the root of the assembled artifact, so you can add an outputDirectory:
   <dependencySet>
<outputDirectory>mule/lib/user</outputDirectory>
</dependencySet>
And in the case where you want to exclude something (such as mule-#.tar.gz or isoft-#.tar.gz) you can add exclusions:
   <dependencySet>
<outputDirectory>mule/lib/user</outputDirectory>
<excludes>
<exclude>*:tar.gz</exclude>
</excludes>
</dependencySet>
Doing things this way puts a heavier burden on properly managing your dependencies however (which really is a good thing). If transitive dependencies you may not need are not excluded in the POM’s dependency declaration or the parent’s dependencyManagement, the transitive dependencies will end up in your artifact, leading to potential collisions and jar bloat. An example of an exclusion:
    <dependency>
<groupId>toplink.essentials</groupId>
<artifactId>toplink-essentials</artifactId>
<scope>runtime</scope>
<excludes>
<exclude>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
</exclude>
</excludes>
</dependency>
You can run each of the following to help with your dependency manangement:
mvn dependency:analyze
mvn dependency:resolve
mvn dependency:tree

No comments:

Post a Comment