问题描述
在将 Maven 项目升级到 Java 9 并添加模块描述符后,javac
抱怨自动模块的传递依赖:
After upgrading a Maven project to Java 9 and adding a module descriptor, javac
complains about a transitive dependency for an automatic module:
[警告]/.../src/main/java/module-info.java:[3,35] 需要传递性
指令用于自动模块
重现问题的示例 module-info.java
:
module com.example.mymodule {
exports com.example.mymodule.myexportedpackage;
requires transitive com.google.common;
}
这个警告的含义已经很清楚了,这里有一些相关的链接:
The meaning of this warning is completely clear, here are some related links:
问题是——如何抑制这个警告,而不解决实际问题,并且不禁用所有其他javac
警告?
The question is — how to suppress this warning, without fixing the actual issue, and without disabling all the other javac
warnings?
我尝试了以下选项,但都没有奏效:
I've tried the following options, but none of them worked:
@SuppressWarnings("module")
在module-info.java
@SuppressWarnings("all")
在module-info.java
-Xlint:all,-module
命令行选项
@SuppressWarnings("module")
inmodule-info.java
@SuppressWarnings("all")
inmodule-info.java
-Xlint:all,-module
command line option
不幸的是,我无法解决实际问题(目前),因为我的"模块具有来自第三方(自动)模块(例如 Guava)的返回类型和注释.因此,如果我使用requires com.google.common"(没有 transitive
),那么会有一个不同的警告,例如:
Unfortunately, I cannot fix the actual issue (for now) because "my" module has return types and annotations from third-party (automatic) modules (e.g. Guava). Thus, if I'd use "requires com.google.common" (without transitive
), then there would be a different warning, e.g.:
[警告] .../MyClass.java:[25,20] 模块 com.google.common
中的类 com.google.common.collect.Table
不使用需要传递
当然,我无法为第三方库(现在是自动模块)定义模块描述符.
And of course I cannot define module descriptors for the third-party libraries (which are automatic modules right now).
我正在使用 -Werror
我更愿意保留它,所以警告不仅仅是烦人的......
I'm using -Werror
which I'd prefer to keep, so the warning isn't merely annoying...
附言我不打算将我的工件发布到任何公共存储库.
P.S. I do not intend to publish my artifacts to any public repositories.
推荐答案
您可以尝试使用
-Xlint:-requires-transitive-automatic
与 JDK-8178011 合并的更改说明:-
The changes for which were merged with JDK-8178011 stating:-
应该有两个新警告:
- 当一个命名模块需要传递"一个自动模块(默认开启)
- 当一个命名模块需要"一个自动模块时(默认关闭)
从此处做出的更改推断此以及来自对 JEP 261: Module System 的编辑证实了这一点(重点是我的):-
Inferring this from the changes made here and also from the edit to the JEP 261: Module System which confirms that(emphasis mine):-
在这两种模块化模式下,编译器默认会生成与模块系统相关的各种警告;这些可能被禁用通过选项 -Xlint:-module
.
更精确地控制这些警告可通过 exports、opens、requires-automatic 和-Xlint
选项需要传递自动键.
More precise control of these warnings is available via the exports, opens, requires-automatic, and requires-transitive-automatic keys for the -Xlint
option.
这篇关于如何抑制“自动模块需要传递指令"正确警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!