java - Java9 模块 - Maven:ServiceLoader.load(HelloInterface.class) 找不到接口类
问题描述
在 IntelliJ/Maven 中,我想使用 Java9/模块和 ServiceLoader。我构建了一个简单的 2 模块项目。使用 maven ServiceLoader.load( ...) 找不到 HelloInterface.class 的实现。为什么?
我首先添加了 2 个模块,通过 IntelliJ 创建了项目。重建项目工作正常。通过 Maven 清理/安装也可以正常工作 - 好吧,单元测试失败;-(
更新 1:由于将 module-info.java 放在源根目录下,因此可以使用 IntelliJ 运行。我使用“重建项目”进行编译。
如何将这 2 个模块项目与带有 Maven 的 ServiceLoader 一起使用?
我的简单项目结构是:
pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>java9.com.hello</module>
<module>java9.com.hello.client</module>
</modules>
文件:模块java9.com.hello/src/main/java/module-info.jar
module java9.com.hello {
exports com.hello;
exports com.hello.services;
provides com.hello.services.HelloInterface with com.hello.services.HelloWorldIntefaceImpl;
}
文件:模块 java9.com.hello / com.hello.services.HelloInterface.java
public interface HelloInterface {
String sayHello();
}
文件:模块 java9.com.hello / com.hello.services.HelloWorldIntefaceImpl.java
public class HelloWorldIntefaceImpl implements HelloInterface {
public String sayHello() {
String helloString = "Hello world by Inteface!";
System.out.println( helloString);
return helloString;
}
}
文件:模块 java9.com.hello / pom.xml(没有样板)
<parent>
<artifactId>jdk-new-features</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>java9.com.hello</artifactId>
文件:模块java9.com.hello.client/src/main/java/module-info.jar。该文件给出编译器错误。
module java9.com.hello.client {
requires java9.com.hello; // <==== gives the ERROR
uses com.hello.services.HelloInterface;
}
文件:模块 java9.com.hello.client / com.hello.client.HelloWorldClient.java
public class HelloWorldClient {
public static void main (String arg[]) {
HelloWorld hello = new HelloWorld();
System.out.println(hello.sayHelloWorld());
Iterable<HelloInterface> services = ServiceLoader.load(HelloInterface.class);
HelloInterface service = services.iterator().next();
service.sayHello();
}
public String callService() {
Iterable<HelloInterface> services = ServiceLoader.load(HelloInterface.class);
HelloInterface service = services.iterator().next();
return service.sayHello();
}
}
pom.xml是:模块java9.com.hello.client/pom.xml:
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>java9.com.hello</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
单元测试文件:模块 java9.com.hello.client / com.hello.client.HelloWorldClientTest.java
public class HelloWorldClientTest {
@Test
public void testModuleInterfaceImplementation() {
HelloWorldClient helloWorldClient = new HelloWorldClient();
assertEquals( "Hello world by Inteface!", helloWorldClient.callService());
}
}
解决方案
推荐阅读
- vb.net - System.Data.SQLite.SQLiteException:'SQLite 错误没有这样的列:aa'
- lets-encrypt - Traefik 让我们为多个域加密 ACME Route53
- c# - 从多个目标文件执行的单个 nuget.target 文件
- python - 将 astropy 表转换为字典列表
- css - 如何获取小部件的 CSS 属性?
- scala - Spark ForEachPartitionAsync 可以在工作节点上异步吗?
- angular - Item$.take 不是函数类型错误:item$.take 不是函数
- php - Laravel 5.6 - 当我使用 php artisan db:seed 时出现错误
- javascript - 将对象推送到数组
- javascript - 在javascript web3中将智能合约功能调用为aa字符串