Percy

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

java

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

当我将应用程序打包到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为附加的classpath元素(除了上一点之外)
我已经仔细检查了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


        <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>

阅读 331

收藏
2020-11-30

共1个答案

小编典典

你尝试exec:exec代替exec:java吗?

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

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

因为Maven为每个Maven插件构造了自己的类加载器,所以即使您声明包含SPI的编译时相关性,这些类也仅对Maven类加载器可见,而对应用程序类加载器不可见。

2020-11-30