Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven compiler plugin jdk versions for source and target

I have the following configuration in my pom.xml for maven-compiler-plugin.

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

What should be the source and target version of jdk? How it depends on version of jdk installed on my computer? May they be different? e.g. installed jdk is 1.8, in source parameter - 1.6, target - 1.7.

like image 765
Ilya Zinkovich Avatar asked Apr 06 '15 15:04

Ilya Zinkovich


People also ask

Which JDK is used by Maven?

Maven Configuration The Maven tool uses JDK version 11.0.

Is Maven compiler plugin is typically overridden for a specific Java version?

Default Configuration xml' you can override this behavior and specify your own version of compiler and compatibility version. Note: The java version used to launch Maven itself (Maven runtime) can be set to a different version than the version used for compiling your source code or project.

What are the two types of Maven plugins?

Introduction. In Maven, there are two kinds of plugins, build and reporting: Build plugins are executed during the build and configured in the <build/> element. Reporting plugins are executed during the site generation and configured in the <reporting/> element.


1 Answers

With source/target you only define the switches of javac which means to produce compatible code. For example if you have installed jdk 8 and would like to create java 7 runable classes. But it does not check if you have installed a JDK 8 at all. This would also work if you have JDK 7 installed.

If you really need to check the JDK version which is installed you have to go via the maven-enforcer-plugin and check the installed JDK...

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <id>enforce-java</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireJavaVersion>
                  <version>1.8.0</version>
                </requireJavaVersion>
              </rules>    
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

With the above you are not able to build on a JDK 7 anymore...only with JDK 8...

BTW: Why are you using such an old maven-compiler-plugin version ?

like image 93
khmarbaise Avatar answered Sep 17 '22 08:09

khmarbaise