简体   繁体   English

Maven中不同模块之间的ServiceLoader和META-INF /服务生成器?

[英]ServiceLoader and META-INF/services generator between different modules in maven?

I would like to use service ServiceLoader between diferrent moduls in a maven Project. 我想在Maven项目中的不同模块之间使用服务ServiceLoader。 I have a parent modul called iMage. 我有一个称为iMage的父模块。 In the parent modul there is a modul with a name jmjrst.main and it has a public abstract class called 'JmjrstPlugin'. 在父模块中,有一个名为jmjrst.main的模块,它有一个称为'JmjrstPlugin'的公共抽象类。

Then there is another modul called prizm-plugin with the following class: public class HelloWorldPlugin extends JmjrstPlugin{ ... }. 然后是另一个名为prizm-plugin的模块,具有以下类:公共类HelloWorldPlugin扩展了JmjrstPlugin {...}。 I added jmjrst.main as a dependency to prizm-plugin and vica-versa as well. 我将jmjrst.main添加为对prizm-plugin和vica-versa的依赖项。

In order to use ServiceLoader I wanted to use META-INF/services generator . 为了使用ServiceLoader,我想使用META-INF / services generator I added the following line to the pom.xml of prizm-plugin: 我将以下行添加到prizm-plugin的pom.xml中:

<dependency>
  <groupId>org.kohsuke.metainf-services</groupId>
  <artifactId>metainf-services</artifactId>
  <version>1.1</version>
  <optional>true</optional>
</dependency>

And the class HelloWorldPlugin starts like that: 并且类HelloWorldPlugin像这样开始:

@MetaInfServices(JmjrstPlugin.class) 
public class HelloWorldPlugin extends JmjrstPlugin{ ... }

On the website on META-INF/services generator goes: "When you use javac in JavaSE6, META-INF/services/* files are generated automatically. No additional compiler switches are necessary. This library handles incremental compilation correctly, too." META-INF / services生成器的网站上显示:“当在JavaSE6中使用javac时,将自动生成META-INF / services / *文件。不需要其他编译器开关。该库也可以正确处理增量编译。”

At my case nothing is generated. 就我而言,什么也没有产生。 Can somebody help me with that? 有人可以帮我吗?

From the code snippet of your class HelloWorldPlugin it is not clear which interface is 'the contract'. 从类HelloWorldPlugin的代码片段中,不清楚哪个接口是“合同”。 And in the link that you gave: 在您提供的链接中:

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来显式指定合约类型。

So first of all you have to be sure which contract you wish to fulfill and if the parent class(es) implement several then you'll need to explicitly state which one in the @MetaInfServices annotation. 因此,首先您必须确定要履行的合同,并且如果父类实现了多个合同,则需要在@MetaInfServices批注中明确声明哪个@MetaInfServices

That's the first thing to check I think. 我认为这是要检查的第一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM