java - 使用 -source 和 -target javac 选项编译

标签 java maven compiler-construction

<分区>

我已将我的 Web 应用程序升级到 Java 7,JAVA_HOME 指向 1.7。我的 Maven 插件正在从 java_home 读取 Java 版本。但是在 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 代码?根据我的理解,基于 this link ,以上设置将获得优先权,项目将使用 1.6 而不是 1.7 进行编译。对吗?

如果我提供如下设置并且我有特定于 JDK 1.7 的代码,我的代码现在可以编译吗?

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

我不确定;上面的设置到底是什么意思?

最佳答案

没有插件的编译器配置

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

javac -帮助

javac -help

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

javac - Java programming language compiler文档

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:

兼容性

关于java - 使用 -source 和 -target javac 选项编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21204299/

相关文章:

java - Intellij、Maven 和 Eclipse 之间的编译差异

compiler-construction - 8086 汇编器 - 从操作码生成目标代码

java - 如何限制那个子类不能泛型?

java - try-with-ressources 在创建并关闭之前无法创建新的 BufferedWriter

java - 创建二维哈希集功能

java - 如何复制 java.lang.reflect.Method?

c# - C#编译如何避免需要头文件?

java - MySQL PersistenceException, Play 中的 "Connection is closed"! 2.2

java - 具有依赖项的可运行 jar

java - IntelliJ maven项目导入问题