In IntelliJ/Maven I want to work with Java9 / modules and the ServiceLoader. I built a simple 2 module project. Using maven the ServiceLoader.load( ...) cannot find the implementation of the HelloInterface.class. Why?
I created the project via IntelliJ by first adding the 2 modules. Rebuilding the project works fine. Clean/installing via Maven also works fine - well, the unit test fails ;-(
UPDATE 1: Running with IntelliJ is OK due to putting the module-info.java right under the sources root. I compile using the "Rebuild project".
How can I use this 2 module project with the ServiceLoader with Maven?
My simple project struture is:
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>
File: module 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;
}
File: module java9.com.hello / com.hello.services.HelloInterface.java
public interface HelloInterface {
String sayHello();
}
File: module 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;
}
}
File: module java9.com.hello / pom.xml (without the boilerplate)
<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>
File: module java9.com.hello.client / src / main / java / module-info.jar. This file is giving the compiler error.
module java9.com.hello.client {
requires java9.com.hello; // <==== gives the ERROR
uses com.hello.services.HelloInterface;
}
File: module 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();
}
}
The pom.xml is: module 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>
Unit test file: module 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());
}
}