问题
I have a multi-module maven project which is making use of the JPMS features. The consumer module is not loading the implementations present in the provider module.
Here's the maven project structure:
ServiceLoaderExample
├── consumer
├── distribution
├── provider
├── service
The interface TestService
is defined in the "service" module. The implementation is TestServiceImpl
which is defined in the "provider" module. And the main() method in the "consumer" module uses the ServiceLoader API to load up implementations of TestService
. And the "distribution" module is where I create a JAR with all of the dependencies using the maven-assesmbly-plugin.
As such, here are the corresponding module-info files:
1 - "service" module (which defines org.example.service.TestService
):
module org.example.service {
exports org.example.service;
}
2 - "provider" module (which defines org.example.provider.TestServiceImpl
):
module org.example.provider {
requires org.example.service;
provides org.example.service.TestService with org.example.provider.TestServiceImpl;
}
3 - "consumer" module (which uses the ServiceLoader API to get implementations of TestService
):
module org.example.consumer {
requires org.example.service;
uses org.example.service.TestService;
}
I am using the maven-assesmbly-plugin, and so it's possible things are getting screwed up when the JAR is getting built. For reference, here's the plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>org.example.consumer.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
I'm using Java 13 and Maven 3.6.3, and I'm running the program using the command java -jar distribution-1.0-SNAPSHOT-jar-with-dependencies.jar
. What am I doing wrong?
回答1:
I was unable to get it work with an uber-jar, however, everything does work when using JLink. So decided to just use JLink instead.
来源:https://stackoverflow.com/questions/59369028/why-wont-the-serviceloader-load-any-implementations-from-another-module