首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在运行exec:java时注册SPI实现

如何在运行exec:java时注册SPI实现
EN

Stack Overflow用户
提问于 2016-03-22 17:54:46
回答 1查看 1.2K关注 0票数 3

我试图使VertX Mertrics在通过exec:java Maven插件运行时工作。

当我将应用程序打包到fatjar中并使用java -jar fat.jar -conf config.json -Dvertx.metrics.options.enabled=true运行时,所有这些都可以正常工作。

当我使用mvn clean package exec:java -DskipTests运行它时,我看到:2016-03-22 18:39:58.833 WARN i.v.c.i.VertxImpl:348 - Metrics has been set to enabled but no VertxMetricsFactory found on classpath

我尝试了几种方法:

  • io.vertx:vertx-dropwizard-metrics:3.2.1添加为编译依赖项
  • 创建内部度量实现并通过src/main/resources/META-INF/services/io.vertx.core.spi.VertxMetricsFactory文件注册它(反复检查它是否实际被复制到target/classes/META-INF/services/io.vertx.core.spi.VertxMetricsFactory)
  • 还添加${basedir}/src/main/resources作为额外的类路径元素(除了前面的一点)

我在调试器中再次签入,ServiceLoader实际上返回了一个空迭代器。

这是我的exec-plugin配置:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <configuration> <additionalClasspathElements> <element>${basedir}/src/main/resources</element> </additionalClasspathElements> <mainClass>io.vertx.core.Launcher</mainClass> <commandlineArgs>run ${vertx.mainVerticle} -conf ${vertx.config}</commandlineArgs> <systemProperties> <systemProperty> <key>vertx.logger-delegate-factory-class-name</key> <value>io.vertx.core.logging.SLF4JLogDelegateFactory</value> </systemProperty> <systemProperty> <key>vertx.metrics.options.enabled</key> <value>true</value> </systemProperty> </systemProperties> </configuration> </plugin>

下面是exec:exec配置,确实可以使用,但我想了解是否和为什么可以使用exec:java来实现这一配置。

代码语言:javascript
复制
     `<profile>             <id>exec</id>             <build>                 <plugins>                     <plugin>                         <groupId>org.codehaus.mojo</groupId>                         <artifactId>exec-maven-plugin</artifactId>                         <executions>                             <execution>                                 <goals>                                     <goal>exec</goal>                                 </goals>                                 <phase>process-classes</phase>                                 <configuration>                                     <executable>java</executable>                                     <arguments>                                         <argument>-Dvertx.metrics.options.enabled=true</argument>                                         <argument>-Dvertx.logger-delegate-factory-class-name=${vertx.logger-delegate-factory-class-name}</argument>                                         <argument>-classpath</argument>                                         <classpath />                                         <argument>io.vertx.core.Launcher</argument>                                         <argument>run</argument>                                         <argument>${vertx.mainVerticle}</argument>                                         <argument>-conf</argument>                                         <argument>${vertx.config}</argument>                                     </arguments>                                 </configuration>                             </execution>                         </executions>                     </plugin>                 </plugins>             </build>         </profile>`
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-22 19:47:45

你试过exec:exec而不是exec:java吗?

exec:exec在一个单独的进程中运行,这可能解决您的问题。

ServiceLoader使用应用程序类加载器加载META-INF/services中列出的任何类。这就是为什么ServiceLoader在带有自定义类加载器(例如OSGi)的环境中经常不工作的原因。

由于Maven为每个Maven插件构建了自己的类加载器,即使您声明包含SPI的编译时依赖项,这些类也只能在Maven类加载器中可见,而对应用程序类加载器则不可见。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36161795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档