简体   繁体   English

Maven编译器的源和目标

[英]Maven compiler source and target

I have searched all the answers on why we should have source different than target but non is convencing: 我搜索了所有答案,以了解为什么我们的来源应与目标不同,但非在召集:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
      <source>1.7</source>
      <target>1.8</target>
    </configuration>
  </plugin>

If JDK is backward compatible why do I need to set the target to 1.8 when the compilation is done on 1.7 ? 如果JDK向后兼容,为什么在1.7上完成编译时我需要将目标设置为1.8?

Setting the source higher than target won't work , example for the error : 将源设置为高于目标将不起作用,例如以下错误:

Source release 8 requires target release 1.8 源版本8需要目标版本1.8

Also from Maven : 同样来自Maven:

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. 仅设置目标选项不能保证您的代码实际在具有指定版本的JRE上运行。 The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error 陷阱是意外使用仅在以后的JRE中存在的API,这会使您的代码在运行时由于链接错误而失败

Can anyone give example ? 谁能举个例子? maven source and target are for my own source code not for the dependencies , then what is the case I can use API in later JRE ( in my own code ) compiles successfully ? maven源代码和目标代码是我自己的源代码,而不是依赖项,那么在以后的JRE(以我自己的代码)中可以成功使用API​​的情况是什么?

Generally one would set the target JVM to match the source JVM, otherwise. 通常,将目标JVM设置为与源JVM匹配,否则。 If the target JVM is NOT set, the version of the Java compiler will set it to that JVM version, eg If you used Java 10 to build, then the target class files will only run under Java 10 unless you specified target JVM to be something lower. 如果未设置目标JVM,则Java编译器的版本会将其设置为该JVM版本,例如,如果您使用Java 10进行构建,则除非您将目标JVM指定为目标Java文件,否则目标类文件将仅在Java 10下运行。降低。

I guess in the case where your enterprise is targeting a target JVM (eg Java 8 b/c or either security and/or performance reasons) which is higher than what you would like your source Java to be compatible with (eg Java 7), then you would set the target version to be higher than the source. 我想如果您的企业针对的是目标JVM(例如Java 8 b / c或出于安全和/或性能方面的原因)高于您希望源Java兼容的目标JVM(例如Java 7),那么您可以将目标版本设置为高于源版本。 Maybe you might "share" this code library to others that could still be targeting a Java 7 JVM. 也许您可以将该代码库“共享”给可能仍然针对Java 7 JVM的其他人。

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

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