使用-source和-target javac选项编译 [英] Compile using -source and -target javac options

查看:2507
本文介绍了使用-source和-target javac选项编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的Web应用程序升级到Java 7,使用JAVA_HOME指向1.7。我的Maven插件是从java_home读取Java版本。但我看到pom.xml中的下面的设置有点困惑:

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>

如果我保持上述设置,Maven是否会用1.6或1.7编译Java代码?根据我的理解,基于这个链接,上述设置将获得优先,项目将编译为1.6而不是1.7。这是正确的吗?

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?

如果我给一个设置如下,如果我有代码特定于JDK 1.7,我的代码现在编译?

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>

我不确定;

推荐答案

没有插件的编译器配置



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

javac -help

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



javac - Java编程语言编译器文档



javac - Java programming language compiler documentation


source 指定接受的源代码的版本。允许以下释放值:

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


  • 1.6在Java SE 6中未引入语言更改。但是,源文件中的编码错误现在

  • 6 1.6的同义词。

  • 1.7这是默认值。编译器接受在Java SE 7中引入的特性的代码。

  • 7 1.7的同义词。

  • 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 生成以指定版本的VM为目标的类文件。类文件将在指定的目标和更高版本上运行,而不是在早期版本的VM上运行。有效目标是1.1,1.2,1.3,1.4,1.5(也是5),1.6(也是6)和1.7(也是7)。
-target的默认值取决于-source的值:

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:



兼容性



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆