javamaven

Compile using -source and -target javac options


I have upgraded my web application to Java 7 with JAVA_HOME pointing to 1.7. My Maven plugin is reading the Java version from java_home. But I am bit confused after seeing the below setting in pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <fork>true</fork>
        <compilerVersion>1.6</compilerVersion>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

If I keep the above settings as it is, will Maven compile the Java code with 1.6 or 1.7? As per my understanding based on this link, the above settings will get preference and the project will be compiled with 1.6 instead of 1.7. Is that correct?

If I give a setting like below and if I have code specific to JDK 1.7, will my code compile now?

 <source>1.6</source>
  <target>1.7</target>

I am not sure; what do the above settings actually mean?


Solution

  • Compiler configuration without plugin

    <properties>
      <maven.compiler.source>1.7</maven.compiler.source>
      <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    

    javac -help

    javac -help
    
      -source <release>          Provide source compatibility with specified release
      -target <release>          Generate class files for specific VM version
    

    javac - Java programming language compiler documentation

    source Specifies the version of source code accepted. The following values for release are allowed:

    • 1.6 No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as in previous releases of Java SE.
    • 6 Synonym for 1.6.
    • 1.7 This is the default value. The compiler accepts code with features introduced in Java SE 7.
    • 7 Synonym for 1.7.

    target Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7). The default for -target depends on the value of -source:

    Compatibility