“maven.compiler.release"作为源和目标的替代品? [英] "maven.compiler.release" as an replacement for source and target?

查看:143
本文介绍了“maven.compiler.release"作为源和目标的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个关于 maven.compiler.release-tag

I have two questions about maven.compiler.release-tag

我想换

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

<properties>
     <maven.compiler.release>12</maven.compiler.release>
</properties>

如果我使用 <maven.compiler.release>-property,我是否还必须在插件中设置发布标签?

If I use <maven.compiler.release>-property, do I have to set the release tag also in the plugin?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <!-- do I need that ? -->
        <release>12</release>
    </configuration>
</plugin>

根据https://www.baeldung.com/maven-java-version,它被设置为两者.

According to https://www.baeldung.com/maven-java-version, it is set to both.

如果我使用 maven.compiler.release 而不是 maven.compiler.sourcemaven.compiler.target,那么 -bootclasspath 也已设置,并将进行交叉编译.这是什么意思?设置 -bootclasspath 的编译文件大小会更大还是编译需要更多时间?

If I use maven.compiler.release instead of maven.compiler.source and maven.compiler.target, then -bootclasspath is also set and will do a cross-compile. What does this mean? Will the compilation file sizes with set -bootclasspath be bigger or will the compilation need more time?

推荐答案

简单建议"在另一个答案中将无法正常工作,因为不幸的是情况并不简单.编译器选项的含义略有不同.maven.compiler.sourcemaven.compiler.target 选项分别指的是源代码格式和生成的类文件的格式.但是他们没有maven.compiler.release那样考虑API变化.

The "simple suggestion" in the other answer won't work correctly because unfortunately the situation is not simple. The compiler options mean slightly different things. The maven.compiler.source and maven.compiler.target options refer to the source code format and the format of the generated class files, respectively. But they do not take into account API changes as maven.compiler.release does.

所以如果你盲目地使用88,例如,如果您使用 Java 11 构建生成的类文件将支持 Java 8,但 API 可能仍依赖于 Java 11 更改并在 Java 8 上运行时中断.请参阅 javac 11 应该如何链接在更高版本中使用 Java 8 目标覆盖的方法? 更多讨论.

So if you blindly just use <maven.compiler.source>8</maven.compiler.source> and <maven.compiler.target>8</maven.compiler.target>, if you build using Java 11 for example the generated class files will support Java 8 but the API may still rely on Java 11 changes and break when run on Java 8. See How should javac 11 link methods overridden in later versions with a Java 8 target? for more discussion.

如果您想指定 Java 8 但在 Java 9+ 上构建,则解决方案并不那么简单.您需要使用 Java 8 支持的编译器选项指定源和目标:

The solution is not so simple if you want to designate Java 8 but build on Java 9+. You'll need to specify the source and target as you are doing, using compiler options supported by Java 8:

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

然后您需要在 <profiles> 部分中设置单独的配置文件,该配置文件仅在 Java 9+ 中激活以打开 maven.compiler.release选项:

Then you'll need to set up a separate profile in the <profiles> section that is only activated in Java 9+ to turn on the maven.compiler.release option:

<profile>
  <id>java-8-api</id>
  <activation>
    <jdk>[9,)</jdk>
  </activation>
  <properties>
    <maven.compiler.release>8</maven.compiler.release>
  </properties>
</profile>

这样,如果您在 Java 9+ 上构建,maven.compiler.release 将生效并限制 API 与 Java 8 兼容.如果您在 Java 8 上构建,另外两个options 会将源代码和类文件格式设置为 Java 8,并且 API 将根据定义与 Java 8 兼容.

That way if you build on Java 9+, maven.compiler.release will come into effect and restrict the API to be compatible with Java 8. If you build on Java 8, the other two options will set the source code and class file format to Java 8, and the API will be Java 8 compatible by definition.

对于所有相关人员来说,更好的选择当然是尽快迁移到 Java 11.

The better option for everyone involved of course is just to move to Java 11 as soon as possible.

这篇关于“maven.compiler.release"作为源和目标的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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