简体   繁体   English

具有源 1.6 配置的 maven 编译器插件是否识别自 1.7 以来引入的 API?

[英]Does maven compiler plugin with source 1.6 configuration recognise API introduced since 1.7?

I've a maven project with the maven-compiler-plugin configured as below:我有一个 maven 项目,其 maven-compiler-plugin 配置如下:

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

In my code I use a class java.nio.charset.StandardCharsets which is introduced since 1.7.在我的代码中,我使用了自 1.7 以来引入的 java.nio.charset.StandardCharsets 类。 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?我很惊讶我的代码编译成功,插件不应该因为 java.nio.charset.StandardCharsets 不是 1.6 编译器而抛出错误吗?

Neither target nor source relate in any way to what classes are available on the compiler's classpath. targetsource与编译器的类路径上可用的类没有任何关系。 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.如果您使用 1.7 编译器编译代码,那么 1.7 附带的任何类都可用于您的代码。

What target does is tell the compiler to output .class files in a format that is compatible with the 1.6 release of java. target作用是告诉编译器以与 1.6 版本的 java 兼容的格式输出 .class 文件。 source says only accept java code that would compile with the 1.6 version of the compiler. source说只接受可以用 1.6 版编译器编译的 Java 代码。

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.因此,使用写入与 Java 1.6 兼容的类文件中的 Java 1.6 兼容源代码来调用仅在 1.7 或更高版本上提供的类是完全合法的。 It just won't run on 1.6.它只是不会在 1.6 上运行。

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 上运行的唯一方法(如果您正在尝试这样做)是使用 1.6 JDK 来编译您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM