How to specify source and target compatibility in Java module?(如何在 Java 模块中指定源和目标兼容性?)
问题描述
我有一个 Gradle 项目,其中包含一个 Android 模块(com.android.library
插件应用在 build.gradle
文件中)和一个 Java 模块(java
插件应用在 build.gradle
文件中).在 Java 模块的 build.gradle
文件中,我之前指定了源和目标的兼容性,如下所示:
I have a Gradle project consisting of an Android module (the com.android.library
plugin is applied in the build.gradle
file) and a Java module (the java
plugin is applied in the build.gradle
file). Within the build.gradle
file of the Java module I was previously specifying the source and target compatibility as follows:
apply plugin: 'java'
compileJava {
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
我刚刚将项目的Android Plugin for Gradle"和Gradle Wrapper"版本更新为最新版本(分别为 2.2.3 和 3.2.1),现在我在 Java 模块中看到如下警告:
I've just updated the project's "Android Plugin for Gradle" and "Gradle Wrapper" versions to the latest releases (2.2.3 and 3.2.1 respectively) and I'm now seeing warnings in the Java module as follows:
Access to 'sourceCompatibility' exceeds its access rights
Access to 'targetCompatibility' exceeds its access rights
如果我将 sourceCompatibility
和 targetCompatibility
声明移动到模块的 build.gradle
文件的根级别,如下所示...
If I move the sourceCompatibility
and targetCompatibility
declarations to the root-level of the module's build.gradle
file as follows...
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
...然后我收到警告消息,告诉我分配未使用.
... then I get warning messages telling me that the assignments are not used.
在最新的 Gradle 版本中指定 Java 模块的源和目标兼容级别的正确方式是什么?
What is the correct manner of specifying the source and target compatibility level of a Java module in the latest Gradle release?
推荐答案
我认为最后一个(根中的声明)是正确的,它必须正常工作,尽管它会在 IDE 中引起警告.无处可说,声明它的首选方式是什么,official docs,它被用作:
I think that the last one(declaration in the root) is the correct one and it has to work properly, though it causes warnings in IDE. It's nowhere said, what is the prefered way to declare it and there is only one example in the official docs, where it's used as:
sourceCompatibility = 1.6
在 build.gradle
的根目录中.
这篇关于如何在 Java 模块中指定源和目标兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Java 模块中指定源和目标兼容性?


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01