java 1.7+ is not supported in -source 1.5 errors

833 Views Asked by At

I'm getting all sort of java errors not supported in -source 1.5 when creating my Maven Install in eclipse. There is nothing wrong with my code.

Errors follows:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1
[23,62] multi-catch statement is not supported in -source 1.5 
[241,29] try-with-resources is not supported in -source 1.5
[156,64] diamond operator is not supported in -source 1.5

My pom configuration follows:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
1

There are 1 best solutions below

0
On

Add the following lines in your pom.xml file should resolve your problem.

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

for java 11 use:

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

If you may take in consideration the advises that were provided to you. You would have 2 options to choose from:

Option 1) If you keep the maven-war-plugin. Update version to the latest then add the properties with the compiler info and sourceEncoding but remove the configuration lines:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
    
    ..
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.1</version>
    </plugin>
    ..
</build>

Option 2) If you replace maven-war-plugin with the maven-compiler-plugin ** There are no need to add/replace the source, target and encoding to the properties**. Make sure to update the version to the latest:

<build>
    ..
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
    ..
</build>