简体   繁体   English

Maven 依赖和 Java 服务提供者接口

[英]Maven Dependency & Java Service Provider Interface

Quick Background:快速背景:
I have set up a Java Service Provider Interface to allow users to create their own custom plugins.我已经建立了一个 Java 服务提供者接口,以允许用户创建他们自己的自定义插件。 The interface they implement is in its own stand alone package so I can release a smaller package to them without other bulky code.他们实现的接口在它自己的独立包中,所以我可以向他们发布一个较小的包,而无需其他庞大的代码。

Once users create their JAR, they can dynamically load it into the application during runtime using some reflection hacks (that I found here on SO).一旦用户创建了他们的 JAR,他们就可以在运行时使用一些反射技巧(我在 SO 上找到的)动态地将它加载到应用程序中。 These seem to work well, but as soon as I moved to another developers machine (and our Jenkins box) the dependencies start getting thrown off somehow.这些似乎工作得很好,但是一旦我转移到另一台开发人员机器(和我们的 Jenkins 机器),依赖项就开始以某种方式被抛弃。

The JAR for the interface is a dependency of both the main app, and the smaller user created plugins.接口的 JAR 是主应用程序和较小的用户创建的插件的依赖项。 When users upload the JAR, they have been using a JAR that contains that dependency which is helping ensure the interface is the same.当用户上传 JAR 时,他们一直在使用包含该依赖项的 JAR,这有助于确保界面相同。 This allows the Java SPI to pick it up and make it useable.这允许 Java SPI 拾取它并使其可用。 I have started switching the user packages to use the dependency as <scope>provided</scope> in hopes this fixes org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.util.ServiceConfigurationError: com.company.interface.MyInterface: Provider com.usercode.ClassName not a subtype我已经开始切换用户包以使用<scope>provided</scope>依赖项,希望这能修复org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.util.ServiceConfigurationError: com.company.interface.MyInterface: Provider com.usercode.ClassName not a subtype org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.util.ServiceConfigurationError: com.company.interface.MyInterface: Provider com.usercode.ClassName not a subtype . org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.util.ServiceConfigurationError: com.company.interface.MyInterface: Provider com.usercode.ClassName not a subtype

When using provided scope I run into different errors of java.lang.NoClassDefFoundError: com/company/interface/MyInterface when trying to load in user jars.使用提供的范围时,我在尝试加载用户 jar 时遇到了java.lang.NoClassDefFoundError: com/company/interface/MyInterface不同错误。

Anyone know what I could do with this Interface JAR and Maven to make these play nice together?有谁知道我可以用这个接口 JAR 和 Maven 做些什么来让这些一起玩得很好?

If the plugins are using provided scope for this jar then you need to make sure that the main application that runs these plugins does actually provide that jar on the classpath that the plugins can access.如果插件使用此 jar 的provided范围,那么您需要确保运行这些插件的主应用程序确实在插件可以访问的类路径上提供了该 jar。

Also this error: java.lang.NoClassDefFoundError is not the root cause.还有这个错误: java.lang.NoClassDefFoundError不是根本原因。 You should find the first instance of that error in the application logs which should show the root cause of that error.您应该在应用程序日志中找到该错误的第一个实例,它应该显示该错误的根本原因。 If the class is missing off the classpath it will be java.lang.ClassNotFoundException .如果类在类路径中丢失,它将是java.lang.ClassNotFoundException

When you run your app, you need to put the implementation jar in -Djava.ext.dirs =..., instead of -cp ....当您运行您的应用程序时,您需要将实现 jar 放在-Djava.ext.dirs =... 中,而不是 -cp ....
The app won't work or even throw exception if the implementation jar wasn't configured correctly.如果未正确配置实现 jar,该应用程序将无法运行,甚至会引发异常。

java -Djava.ext.dirs=../DictionaryServiceProvider/dist:../GeneralDictionary/dist -cp dist/DictionaryDemo.jar dictionary.DictionaryDemo

see spi tutorialspi教程

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

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