具有源 1.6 配置的 maven 编译器插件是否识别自 1.7 以来引入的 API? [英] Does maven compiler plugin with source 1.6 configuration recognise API introduced since 1.7?

查看:17
本文介绍了具有源 1.6 配置的 maven 编译器插件是否识别自 1.7 以来引入的 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 maven 项目,其 maven-compiler-plugin 配置如下:

I've a maven project with the maven-compiler-plugin configured as below:

    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.0</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>

在我的代码中,我使用了自 1.7 以来引入的 java.nio.charset.StandardCharsets 类.我很惊讶我的代码编译成功,插件不应该因为 java.nio.charset.StandardCharsets 不是 1.6 编译器而抛出错误吗?

In my code I use a class java.nio.charset.StandardCharsets which is introduced since 1.7. I'm surprised that my code compiles successfully, shouldn't the plugin throw an error because java.nio.charset.StandardCharsets is not 1.6 compilant?

推荐答案

targetsource 都与编译器的类路径上可用的类没有任何关系.如果您使用 1.7 编译器编译代码,则 1.7 附带的任何类都可用于您的代码.

Neither target nor source relate in any way to what classes are available on the compiler's classpath. If you're compiling your code with the 1.7 compiler then any classes that shipped with 1.7 will be available to your code.

target 的作用是告诉编译器以与 java 1.6 版本兼容的格式输出 .class 文件.source 表示只接受可以使用 1.6 版编译器编译的 Java 代码.

What target does is tell the compiler to output .class files in a format that is compatible with the 1.6 release of java. source says only accept java code that would compile with the 1.6 version of the compiler.

因此,使用写入与 Java 1.6 兼容的类文件中的 Java 1.6 兼容源代码调用仅在 1.7 或更高版本上提供的类是完全合法的.它只是无法在 1.6 上运行.

So it's perfectly legitimate to make a call to a class that shipped only on 1.7 or later using Java 1.6 compatible source code written into a class file that is compatible with Java 1.6. It just won't run on 1.6.

确保您的代码在 1.6 上运行的唯一方法(如果这是您尝试做的)是使用 1.6 JDK 来编译您的项目.

The only way to ensure your code will run on 1.6 (if that's what you're trying to do) is to use a 1.6 JDK to compile your project.

这篇关于具有源 1.6 配置的 maven 编译器插件是否识别自 1.7 以来引入的 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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