7

I am new to maven, and I find that though I change the facet of the jdk of the project to 1.8, every time I "update maven",it will get back to jdk 1.6.

Why is that?

I installed jdk 1.8 in my windows, and I am using eclipse.

I read Specify JDK for Maven to use and add the following but it does not work.

   <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <executions>
    <execution>
      <id>enforce-versions</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireJavaVersion>
            <version>1.8</version>
          </requireJavaVersion>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>
Community
  • 1
  • 1
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149

4 Answers4

7

The version of the JDK that maven will use is set in the maven-compiler-plugin like so:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
      <source>1.8</source>   <!-- use java 8 -->
      <target>1.8</target>
    </configuration>
  </plugin>

See Setting the -source and -target of the Java Compiler for more information.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
0

Check the maven compiler plugin to specify source and target java version,

http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

Regards, Prasanna

0

maybe too late, but it works for me:

<!-- we want JDK 1.8 source and binary compatiblility -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
    <!-- ... -->
    <!-- we want sources to be processed by a specific 1.8 javac -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <verbose>true</verbose>
          <fork>true</fork>
          <executable>${JAVA_1_8_HOME}/bin/javac</executable>
        </configuration>
    </plugin>

Your question help me a lot anyway :) Thanks :)

Enrique San Martín
  • 2,202
  • 7
  • 30
  • 51
0

In a comment you asked:

So what about maven-enforcer-plugin, what is this to do?

According to that documentation for the plugin:

"This goal is meant to be bound to a lifecycle phase and configured in your pom.xml. The enforcers execute the configured rules to check for certain constraints."

In other words, it checks that certain constraints have been satisfied. It does not cause them to be satisfied.

In your example, the effect of the plugin should be to cause the build to fail if the Java version is not Java 1.8.

... and why it solves the thread "Specify JDK for Maven to use"?

The answer you are referring to says this:

"And it never harms to ... add maven-enforce-plugin to make sure the right jdk is used. This is a good practice for your pom."

As you can see, it does not state that maven-enforce-plugin "solves" the problem. It is actually providing a way to ensure that the problem has been solved ... and fail the build if it hasn't.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216