DIY & Music & Guitar & Beer & Tech

Maven Dependencies gone wild

Transient dependencies can be a bit pain when they start creeping around. Sometimes it’s hard to debug these things even though we have some nice tools at hand like dependency:tree and even eclipse has nice tree traversing tools. But, sometimes we need bigger guns. This nice plugin for maven will do it’s best to break your build as soon as possible if any criteria met. What is possible to do is e.g. ban artifacts, versions etc, converge versions and much more. Here is an example:

<plugin>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
        <execution>
            <id>no-creep-crap-is-allowed</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <DependencyConvergence />
                    <bannedDependencies>
                        <excludes>
                            <exclude>org.apache.maven</exclude>
                            <exclude>org.apache.maven:badArtifact</exclude>
                            <exclude>*:badArtifact</exclude>
                        </excludes>
                        <includes>
                            <!--only 1.0 of badArtifact is allowed -->
                            <include>org.apache.maven:badArtifact:1.0</include>
                        </includes>
                    </bannedDependencies>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

Excludes tag will use a list of artifacts to ban. The format is groupId[:artifactId][:version][:type][:scope][:classifier] where artifactId, version, type, scope and classifier are optional. Wildcards may be used to replace an entire or just parts of a section. Includes will use a list of artifacts to include. These are exceptions to the excludes. It is meant to allow wide exclusion rules with wildcards and fine tune using includes. If nothing has been excluded, then the includes have no effect. In otherwords, includes only subtract from artifacts that matched an exclude rule.

DependencyConvergence is a nice rule that requires that dependency version numbers converge. If a project has two dependencies, A and B, both depending on the same artifact, C, this rule will fail the build if A depends on a different version of C then the version of C depended on by B. It will break the build fast, and print the problem.

This plugin is a nice debug tool that complements the ‘batteries included’ from maven and eclipse.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.