问题
After upgrading a Maven project to Java 9 and adding a module descriptor, javac
complains about a transitive dependency for an automatic module:
[WARNING] /.../src/main/java/module-info.java:[3,35]
requires transitive
directive for an automatic module
An example module-info.java
to reproduce the problem:
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:
- What's the difference between requires and requires transitive statements in Java 9?
- Why does javac complain about named automatic-modules?
- Related OpenJDK issue
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")
inmodule-info.java
@SuppressWarnings("all")
inmodule-info.java
-Xlint:all,-module
command line option
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.:
[WARNING] .../MyClass.java:[25,20] class
com.google.common.collect.Table
in modulecom.google.common
is not indirectly exported usingrequires transitive
And of course I cannot define module descriptors for the third-party libraries (which are automatic modules right now).
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.
回答1:
You could try out the option of switching off the warning using
-Xlint:-requires-transitive-automatic
The changes for which were merged with JDK-8178011 stating:-
There should be two new warnings:
- when a named module "requires transitive" an automatic module (default on)
- when a named module "requires" an automatic module (default off)
Inferring this from the changes made here and also from the edit to the JEP 261: Module System which confirms that(emphasis mine):-
In both of the modular modes the compiler will, by default, generate various warnings related to the module system; these may be disabled via the option
-Xlint:-module
.More precise control of these warnings is available via the exports, opens, requires-automatic, and requires-transitive-automatic keys for the
-Xlint
option.
来源:https://stackoverflow.com/questions/49600947/how-to-suppress-the-requires-transitive-directive-for-an-automatic-module-warn