user11611653
user11611653

Reputation: 139

How to add maven parameters in eclipse?

I created a Maven Project in Eclipse 2019-09. That project has src/java and src/test files on each. Several tests has a try-with-resources statements. I'm using JDK8 and I already set that JDK and the JUnit library in the java build path in eclipse.

When I select Right click on project -> Run as -> Maven test / install I got this output:

[ERROR] /C:/Users/Workstation/eclipse-workspace/my-project/src/test/java/me/myname/myproject/MyProjectTest.java:[43,21] try-with-resources is not supported in -source 1.5 [ERROR] (use -source 7 or higher to enable try-with-resources)

Also, I changed the "Compiler compliance level" to 1.8 on the project properties.

So, the question is how to use -source 7 or higher to enable try-with-resources with Maven in eclipse?

Upvotes: 0

Views: 1966

Answers (1)

Daniel
Daniel

Reputation: 4221

Please don't change the compiler settings directly in Eclipse, let the POM be the master for such configuration. Changing the configuration in Eclipse will override your settings in the POM.

Add this to your POM (Maven's default Java settings are set to 1.5):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <!-- You can use properties to set the parameters below -->
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Then right-click your project and select:

Maven > Update Project ... > Check "Update project configuration from pom.xml" > OK

It will change the prjoect's runtime and compilation settings from this:

Default JRE settings

to this:

Changed JRE settings

It should change everything, but I have found that sometimes the Run Configurations (such JUnit) don't always pick up the change. In that case, just delete those Run Configurations, recreate them and they should reflect the new JRE.

Upvotes: 1

Related Questions