Search code examples
javamaven-3

Maven compilation error: Incompatible lists


I want to null-check a List and assign it to a final class property:

private final ExcelDocument xls;
private final List<ValidationError> validationErrors;

public DefaultExaminationParser(ExcelDocument xls, List<ValidationError> validationErrors)
{
    this.xls = xls;
    this.validationErrors = validationErrors == null ? new ArrayList<>() : validationErrors;
}

The import is: import java.util.List; and import java.util.ArrayList;

The compiler in eclipse and maven is Java 1.8

My IDE (eclipse) does not show any problems with this line, but when I run a maven build it fails with following error:

DefaultExaminationParser.java:[84,51] error: incompatible types: List<? extends Object> cannot be converted to List<ValidationError>

The ValidationError is defined as:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class ValidationError implements Serializable, DetailInfo {
    [...]
}

I don't see any errors here. I can also run a unit test for this class without problems.


Solution

  • The problem was, that the compiler level for maven was set to 1.7 in one of the several parent pom files.

    Used following snippet to set it explicitly to 1.8:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <encoding>utf8</encoding>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>