Kurtiss
Kurtiss

Reputation: 525

@Override not working in Maven project

I have several override methods like this:

@Override
public int compareTo(Property p) {
    return getText().compareTo(p.getText());
}

As a Java project, it works fine, but as a Maven project, it returns the following error:

The method compareTo(Property) of type Property must override a superclass method

After researching into this, I think I'm suppose to include my JRE System Library (jdk1.6_u25) as a dependency in my POM file, or is this a completely different problem all together?

Many thanks.

Upvotes: 3

Views: 4160

Answers (3)

Kurtiss
Kurtiss

Reputation: 525

Thank you all for your comments, a lot of you stated that Maven used Java 5 by default and could be the cause of the issue, and as a result, I was able to determine the problem through this answer:

Why is javac failing on @Override annotation

The JDK compiler's compliance level was set to 1.5 by default; once I set it to 1.6, the errors were removed.

Many thanks.

Upvotes: 3

Ray
Ray

Reputation: 3201

You don't need another dependency. But by default, maven uses Java 5 language level, where @Override wasn't allowed for implementing interface methods. That was introduced in 6.

So you must configure the compiler plugin to use language level 6 like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                 <source>1.6</source>
                 <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Upvotes: 8

Grim
Grim

Reputation: 2030

compareTo is a generic method. Generics are not used so compareTo(Object) is the only method you can override.

Please check that:

  1. Maven uses a java to compile that supports generics.
  2. Source-Level is >= 5.

execute mvn -V to see what version of java maven uses to compile.

Regards

Upvotes: 3

Related Questions