title: Properly implementing Java modules in a Maven build with inter-module test dependencies
body: <p>I have a multi-module project using Maven and Java. I am now trying to migrate to Java 9/10/11 and implement modules (as in <a href="https://jcp.org/en/jsr/detail?id=376" rel="noreferrer">JSR 376: Java Platform Module System</a>, JPMS). As the project was already consisting of Maven modules, and the dependencies were straight, creating module descriptors for the project was quite straight forward.</p>
  
  <p>Each Maven module now has their own module descriptor (<code>module-info.java</code>), in the <code>src/main/java</code> folder. There is no module descriptor for the test classes. </p>
  
  <p>However, I stumbled upon a problem I have not been able to solve, and not found any descriptions on how to solve:</p>
  
  <p><strong>How can I have inter-module <em>test</em> dependencies with Maven and Java modules?</strong> </p>
  
  <p>In my case, I have a "common" Maven module, which contains some interfaces and/or abstract classes (but no concrete implementation). In the same Maven module, I have <em>abstract</em> tests to ensure proper behavior for the implementation of these interfaces/abstract classes. Then, there are one or more sub modules, with implementations of the interface/abstract class and tests extending the abstract test.</p>
  
  <p>However, when trying to execute the <code>test</code> phase of the Maven build, the sub module will fail with:</p>
  
  <pre><code>[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project my-impl-module: Compilation failure: Compilation failure:
  [ERROR] C:\projects\com.example\my-module-test\my-impl-module\src\test\java\com\example\impl\FooImplTest.java:[4,25] error: cannot find symbol
  [ERROR]   symbol:   class FooAbstractTest
  [ERROR]   location: package com.example.common
  </code></pre>
  
  <p>I suspect that this happens because the <em>tests are not part of the module</em>. And even if Maven does some "magic" to get the tests executed within the scope of the module, it doesn't work for the tests in the module I depend on (for some reason). How do I fix this?</p>
  
  <p>The structure of the project looks like this (<a href="https://www.dropbox.com/s/cmu9xdd8mlytpfb/my-module-test.zip?dl=0" rel="noreferrer">full demo project files available here</a>):</p>
  
  <pre><code>├───my-common-module
  │   ├───pom.xml
  │   └───src
  │       ├───main
  │       │   └───java
  │       │       ├───com
  │       │       │   └───example
  │       │       │       └───common
  │       │       │           ├───AbstractFoo.java (abstract, implements Foo)
  │       │       │           └───Foo.java (interface)
  │       │       └───module-info.java (my.common.module: exports com.example.common)
  │       └───test
  │           └───java
  │               └───com
  │                   └───example
  │                       └───common
  │                           └───FooAbstractTest.java (abstract class, tests Foo)
  ├───my-impl-module
  │   ├───pom.xml
  │   └───src
  │       ├───main
  │       │   └───java
  │       │       ├───com
  │       │       │   └───example
  │       │       │       └───impl
  │       │       │           └───FooImpl.java (extends AbstractFoo)
  │       │       └───module-info.java (my.impl.module: requires my.common.module)
  │       └───test
  │           └───java
  │               └───com
  │                   └───example
  │                       └───impl
  │                           └───FooImplTest.java (extends FooAbstractTest)
  └───pom.xml
  </code></pre>
  
  <p>Dependencies in the <code>my-impl-module/pom.xml</code> is as follows:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.example&lt;/groupId&gt;
          &lt;artifactId&gt;my-common-module&lt;/artifactId&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.example&lt;/groupId&gt;
          &lt;artifactId&gt;my-common-module&lt;/artifactId&gt;
          &lt;classifier&gt;tests&lt;/classifier&gt; &lt;!-- tried type:test-jar instead, same error --&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Note: The above is just a project I created to demonstrate the problem. The real project is a lot more complex, and <a href="https://github.com/haraldk/TwelveMonkeys" rel="noreferrer">found here</a> (master branch is <em>not</em> modularized yet), but the principle is the same.</p>
  
  <p>PS: I don't think there's anything wrong with the code itself, as everything compiles and runs using normal class path (ie. in IntelliJ, or Maven without the Java module descriptors). The problem is introduced with Java modules and the module path.</p>