如何从 Java 9 模块导出所有包?

How to export all packages from Java 9 module?

现在,对于我拥有的每个模块,我都需要明确指定要导出的包。例如:

module core {
    exports cc.blynk.server.core;
    exports cc.blynk.server.core.protocol.handlers.decoders;
    exports cc.blynk.server.core.protocol.handlers.encoders;
}

不过,不是很方便。我想做这样的事情:

module core {
    exports cc.blynk.server.core.*;
}

有什么办法吗?这个限制从何而来?

不,您不能使用通配符导出模块中的所有包。您将必须明确导出每个包。

这是不允许的,因为这可能主要导致从不同模块导出的不同包发生冲突,这违背了模块化代码的目的。


另外引用其中一个话题:

The packages exported by a module are meant to be a stable API that consumers can rely on. For this reason, we make the module author spell out the exported packages explicitly. This also dials down the likelihood of multiple modules needlessly exporting the same package. Additionally, it avoids the confusion that would occur if com.abs.* was exported without qualification while com.abs.foo was exported with qualification.