是,optional dependencies are supported。从the original proposal报价:
扩展模块声明的语言,以允许在requires
指令使用的static
改性剂,具有以下含义:
因此形式
module joda.beans {
requires static joda.collect;
...
}
将确保joda.collect
模块可用在编译时,一个假设的模块声明所以joda.beans
模块引用joda.collect
在该代码可以毫不费力地编译。但是,它不能保证joda.collect
在链接时或运行时可用。
(在此期间,official documentation was created for that feature。)
我写a demo这一点。有趣的花絮是模块声明可选依赖的module-info.java
...
module org.codefx.demo.advent {
// list the required modules
requires org.codefx.demo.advent.calendar;
// with 'static' the factories are only required at compile time;
// to be present at run time either other modules most require them
// or they must be added with the '--add-modules' command line option
requires static org.codefx.demo.advent.factory.chocolate;
requires static org.codefx.demo.advent.factory.quote;
}
...和希望从它的可选依赖访问类型相同的模块中的代码。它已经写入,以便它慷慨失败,如果类型ChocolateFactory
和/或QuoteFactory
不存在:
最后,该命令行可用于定义哪些模块应用程序与启动:
$java \
--add-modules org.codefx.demo.advent.factory.chocolate,org.codefx.demo.advent.factory.quote \
-p mods -m org.codefx.demo.advent
当然也有可能其他模块要求它们是非必选的,这迫使JVM将它们包含到模块图中。
供将来参考:您是否知道可选依赖项提案是否实际进入最终版本? – Lii
目前还没有最终版本(Java 9计划于2017年7月发布),但它仍然是EA构建的一部分,并有可能被零边界所取代。 – Nicolai