Maven default JDK version is 1.5

Introduction

When I was playing with Java personally, I was a little addicted to it. It may be a relatively basic thing, but I didn't know it, so I'll write it down as a memorandum. (Because I have a lot of knowledge gained from qualifications, I'm quite missing this kind of practical part ...)

Event that occurred

When Maven project update is executed, it is compiled with JDK 1.5, so a program written with the syntax of JDK 1.6 or later will cause a compilation error.

Details below.

Prior state. The JRE System Library is set to 1.8. image.png

Right click ⇒ Maven ⇒ Run Update Project (Or Alt + F5) image.png

The JRE System Library is changed to 1.5. image.png

Since 1.5, try-with-resources introduced in 1.7 is a compile error image.png

The diamond operator was introduced from 1.7, so a compile error (It is said that the type should be specified on the right side or JRE 1.7) image.png

I looked it up

Looking at the following site, it seems that the default compiler version of the Maven project is 1.5.

-How to specify the JDK version with Maven [JDK 1.5-> 1.8] -Maven: Compiler version setting -Setting the -source and -target of the Java Compiler

counter-measure

Refer to what is written in Apache Maven site, pom. Modify xml. By doing this, you can specify the version of JRE. There are the following methods ① and ②, but this time we will implement ①.

① Specify 1.8 for source and target in project properties

Specify 1.8 this time.

pom.xml


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

(2) Specify 1.8 for source and target with plugin directly

I haven't written this way this time, but it seems that I can still go.

pom.xml


<project>
  [...]
  <build>
    [...]
    <plugins>
      <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>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Confirm after countermeasures

After correcting ① (or ②) above, right-click again ⇒ Maven ⇒ Execute Update Project (Or Alt + F5) ⇒ JRE System Library becomes 1.8.

image.png

Confirm that the try-with-resources compilation error has been resolved. image.png

The diamond operator compilation error has also been resolved. image.png

that's all.

Recommended Posts

Maven default JDK version is 1.5
What is maven?
What is Maven Assembly?
Corretto (JDK) is out from Amazon!