Monday, 10 January 2011

Maven Compile Fix

It sometimes happens that an ancestor POM file references a JAR file that’s an older version than the one you want to use in your project. The symptoms of this are compile errors that seem unbelievable as you know your POM references the correct version of the JAR.

For example your POM has a log4j dependency listed as:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.8</version>
</dependency>


whilst the ancestor/super POM has the following:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.6</version>
</dependency>


When building the 1.2.6 version will be picked up first and used.
To fix this problem, use the <compile> element:


<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.8</version>
    <scope>compile</scope>
</dependency>


… and your problem goes away…