8
votes

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.

1

1 Answers

10
votes

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 ?