How to set specific Java version to Maven?

To set a specific Java version for Maven, you can specify the maven.compiler.source and maven.compiler.target properties in the pom.xml file.

The maven.compiler.source property specifies the version of the source code that will be compiled, and the maven.compiler.target property specifies the version of the compiled code that will be produced.

Here is an example of how to set the Java version to 1.8 in the pom.xml file:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

This will tell Maven to compile the source code using the Java 1.8 compiler and to produce compiled code that is compatible with the Java 1.8 runtime.

You can also specify the Java version using the -source and -target options of the mvn command. For example:

mvn clean compile -Dmaven.compiler.source=1.8 -Dmaven.compiler.target=1.8

This will tell Maven to compile the source code using the Java 1.8 compiler and to produce compiled code that is compatible with the Java 1.8 runtime.