我想在 Maven 项目中的不同模块之间使用服务 ServiceLoader。我有一个名为 iMage 的父模块。在父模块中有一个名为 jmjrst.main 的模块,它有一个名为“JmjrstPlugin”的公共(public)抽象类。
然后还有另一个名为 prizm-plugin 的模块,具有以下类: 公共(public)类 HelloWorldPlugin 扩展了 JmjrstPlugin{ ... }。我添加了 jmjrst.main 作为对 prizm-plugin 和 vica-versa 的依赖。
为了使用 ServiceLoader,我想使用 META-INF/services generator . 我在 prizm-plugin 的 pom.xml 中添加了以下行:
<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
<version>1.1</version>
<optional>true</optional>
</dependency>
HelloWorldPlugin 类是这样开始的:
@MetaInfServices(JmjrstPlugin.class)
public class HelloWorldPlugin extends JmjrstPlugin{ ... }
网站上META-INF/services generator继续:“当你在 JavaSE6 中使用 javac 时,META-INF/services/* 文件会自动生成。不需要额外的编译器开关。这个库也能正确处理增量编译。”
在我的例子中没有生成任何东西。有人可以帮我吗?
最佳答案
从您的类 HelloWorldPlugin
的代码片段中,不清楚哪个接口(interface)是“契约(Contract)”。在您提供的链接中:
If you have multiple interfaces and/or base type, the library cannot infer the contract type. In such a case, specify the contract type explicitly by giving it to @MetaInfServices ..
因此,首先你必须确定你希望履行哪个契约,如果父类实现了多个契约,那么你需要在 @MetaInfServices
注释中明确说明哪个契约.
我认为这是首先要检查的事情。
关于java - Maven 中不同模块之间的 ServiceLoader 和 META-INF/服务生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44345359/