Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven update resets Java version

Everytime I modify the dependencies inside the pom.xml file in my IntelliJ Maven project, the Java version is set to 1.5 and I have to receonfigure my project.

The following settings are modified by Maven:

Settings | Compiler | Java Compiler -> Target bytecode version

enter image description here

And Project Settings | Modules -> Language Level

enter image description here

Why is this happening and what do I have to do, so that maven doesn't vandalise my settings?

like image 810
maja Avatar asked May 22 '15 18:05

maja


People also ask

How do I change the Java version for a Maven project?

Alternatively, you can also change Java version for a Maven project by configuring the Maven compiler plugin as follows: [...] [...] This tells Java compiler to compile source code against Java version 11, and generate the .class files according to Java 15 format.

How to check the default JDK version of Maven?

Before moving on, we can check the default JDK version of Maven. Running the mvn -v command will show the Java version in which Maven runs. Learn how to work with Maven profiles to be able to create different build configurations. Learn how to use the Maven compiler plugin, used to compile the source code of a Maven project. 2.

How to set the version of the compiler plugin in Maven?

The first option is setting the version in compiler plugin properties: ? The Maven compiler accepts this command with – target and – source versions. If we want to use the Java 8 language features the – source should be set to 1.8. Also, for the compiled classes to be compatible with JVM 1.8, the – target value should be 1.8.

How do I know what version of Maven is running?

Running the mvn -v command will show the Java version in which Maven runs. Learn how to work with Maven profiles to be able to create different build configurations. Learn how to use the Maven compiler plugin, used to compile the source code of a Maven project. 2. Use the Compiler Plugin


2 Answers

You have to explicitely set the java version in your pom file so that version 1.5 doesn't get set by default.

<project>
 [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>
like image 122
Jihed Amine Avatar answered Sep 29 '22 12:09

Jihed Amine


Specify the java version under properties.

<properties>
        <java-version>1.8</java-version>
</properties>

and to use the same version with maven compile or package then include the same version in compile plug-in

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
like image 33
K139 Avatar answered Sep 29 '22 13:09

K139