title: Can't make Maven test-scoped dependencies work with Java 9 (nor 10) modules
body: <p>Edit: 
  I analyzed the given answers. In particular I tested Till Brychcy's hypothesis, which seems to hold, but opens more questions. 
  I added that analysis to the end of the question, after the following big separator:
  "-----------Analysis of Till Brychcy's answer----------"</p>
  
  <p>Somehow it compiles and runs in IJ, but <code>mvn clean install</code> fails to compile the test</p>
  
  <h1>Explanation of the 2 modules, and what fails</h1>
  
  <p>I have a java 9 maven project with 2 modules: <code>apimod</code> and <code>clientmod</code>.
  Module <code>clientmod</code> depends on module <code>apimod</code> (those modules are both Maven modules and Java 9 modules).</p>
  
  <p>Also, i want module <code>clientmod</code> to be able to reuse not only production code from <code>apimod</code>, but also test code.
  This is a common pattern, that I used many times with Java 8.
  With Java 9 (it's the same with Java 10) it also works fine, as long as i don't declare <code>module-info.java</code> (that is, as long as I don't run with the module system).</p>
  
  <p>But as soon as I do, enabling the test dependency seems to disable the production dependency: <code>api.Base</code> (an <code>src/main</code> class of module <code>apimod</code>) is no longer visible from <code>client.test.DerivedTest</code> (an <code>src/test</code> class of module <code>clientmod</code>). The test doesn't compile anymore. </p>
  
  <p>Is this is a bug in Maven or in Java 9? This is with the most recent releases: Java 9.0.4 (it's the same with Java 10), Maven 3.5.3, maven-compiler-plugin 3.7.0</p>
  
  <h1>My analysis so far</h1>
  
  <h2>The code</h2>
  
  <p>The source code is at:</p>
  
  <pre><code>git clone https://github.com/vandekeiser/wires.git
  </code></pre>
  
  <p>I "dichotomized" the issue with a failing test in a branch:</p>
  
  <pre><code>git checkout MINIMIZE_ISSUE
  `mvn clean install` 
  </code></pre>
  
  <p>-> BUILD FAIL (compilation error in the test of <code>clientmod</code>)     </p>
  
  <h2>Maven test-scoped dependency</h2>
  
  <p>I want module <code>clientmod</code> to be able to reuse not only production code from <code>apimod</code> but also test code. With Maven you do it like that (<code>clientmod/pom.xml</code>):</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;fr.cla&lt;/groupId&gt;
      &lt;artifactId&gt;apimod&lt;/artifactId&gt;
      &lt;version&gt;${project.version}&lt;/version&gt;
      &lt;classifier&gt;tests&lt;/classifier&gt;
      &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <h2>Java 9 modules</h2>
  
  <pre><code>module apimod {
      exports api;
  }
  
  module clientmod {
      requires apimod;
  }
  </code></pre>
  
  <h2>The failure when trying to enable both module systems</h2>
  
  <p>With Java 9 if I declare both the test-scoped dependency and the Java 9 modules the test doesn't compile anymore (<code>mvn clean install</code> output):</p>
  
  <pre><code>[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project clientmod: Compilation failure
  [ERROR] /G:/projets/wires/wires/wires/clientmod/src/test/java/client/test/DerivedTest.java:[8,22] cannot access api.Base
  [ERROR]   class file for api.Base not found
  </code></pre>
  
  <h2>Reproducing the issue with javac: a bug in module patching?</h2>
  
  <p>It's as if enabling the test dependency (<code>src/test</code>) disables the production dependency (<code>src/main</code>). I know that in this scenario Maven is supposed to use the javac <code>--patch-module</code> flag.
  So i reproduced the issue using just javac (using the debug output of <code>mvn -X</code>):</p>
  
  <p>Same compilation, skipping Maven:</p>
  
  <pre><code>javac "G:\projets\wires\wires\wires\clientmod\src\test\java\client\test\DerivedTest.java" \
  -d "G:\projets\wires\wires\wires\clientmod\target\test-classes" \
  -classpath "G:\projets\wires\wires\wires\clientmod\target\test-classes;" \
  --module-path "G:\projets\wires\wires\wires\apimod\target\apimod-1.0-SNAPSHOT-tests.jar;G:\projets\wires\wires\wires\clientmod\target\classes;G:\projets\wires\wires\wires\apimod\target\apimod-1.0-SNAPSHOT.jar;" \
  -sourcepath "G:\projets\wires\wires\wires\clientmod\src\test\java;" \
  --release 9 \
  -Xlint:all \
  --patch-module clientmod="G:\projets\wires\wires\wires\clientmod\target\classes;G:\projets\wires\wires\wires\clientmod\src\test\java;"
  </code></pre>
  
  <p>Same compilation error:</p>
  
  <pre><code>G:\projets\wires\wires\wires\clientmod\src\test\java\client test\DerivedTest.java:8: error: cannot access Base
  new Derived().equals(null);                         ^
  class file for api.Base not found
  1 error
  </code></pre>
  
  <p>I tried to use the javac flags that are supposed to disable the module system but they don't seem to exist in my 64 bits Windows Oracle JVM? (Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode):</p>
  
  <pre><code>javac --illegal-access=warn
  javac: invalid flag: --permit-illegal-access
  javac --permit-illegal-access
  javac: invalid flag: --illegal-access=warn
  </code></pre>
  
  <p>Adding (logically unneeded, done in desperation) exports or reads doesn't change anything either:</p>
  
  <pre><code>--add-reads apimod=ALL-UNNAMED \
  --add-reads clientmod=ALL-UNNAMED \
  --add-exports apimod/api=ALL-UNNAMED \
  --add-exports clientmod/client=ALL-UNNAMED \
  </code></pre>
  
  <p>Output of mvn -version: </p>
  
  <pre><code>Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T20:49:05+01:00)
  Maven home: G:\software\apache-maven-3.5.3
  Java version: 9.0.4, vendor: Oracle Corporation
  Java home: C:\Program Files\Java\jdk-9.0.4
  Default locale: fr_FR, platform encoding: Cp1252
  OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows" 
  </code></pre>
  
  <h1>Analysis of Till Brychcy's answer</h1>
  
  <p>Thanks for your detailed answer, which is essentially that "It looks like maven doesn't support this use case yet"
  -->So let's try to reproduce the issue without maven. I created branch <code>TRY_ADAPT_khmarbaise-MINIMIZE_ISSUE</code> for these trials (sorry for the confusing branch name).</p>
  
  <ol>
  <li><p>Adapting my former command line which is logged by maven, that is:</p>
  
  <pre><code>javac "G:\projets\wires\wires\wires\clientmod\src\test\java\client\test\DerivedTest.java"
  -d "G:\projets\wires\wires\wires\clientmod\target\test-classes"
  -classpath "G:\projets\wires\wires\wires\clientmod\target\test-classes;"
  --module-path "G:\projets\wires\wires\wires\apimod\target\apimod-1.0-SNAPSHOT-tests.jar;G:\projets\wires\wires\wires\clientmod\target\classes;G:\projets\wires\wires\wires\apimod\target\apimod-1.0-SNAPSHOT.jar;"
  -sourcepath "G:\projets\wires\wires\wires\clientmod\src\test\java;"
  --release 9
  -Xlint:all
  --patch-module clientmod="G:\projets\wires\wires\wires\clientmod\target\classes;G:\projets\wires\wires\wires\clientmod\src\test\java;"
  --add-reads apimod=ALL-UNNAMED
  --add-reads clientmod=ALL-UNNAMED
  --add-exports apimod/api=ALL-UNNAMED
  --add-exports clientmod/client=ALL-UNNAMED
  --add-modules apimod
  </code></pre></li>
  <li><p>I remove <code>G:\projets\wires\wires\wires\apimod\target\apimod-1.0-SNAPSHOT-tests.jar;</code> from <code>--module-path</code></p></li>
  <li><p>I add the same to <code>--patch-module clientmod</code>, giving me:</p>
  
  <pre><code>javac "G:\projets\wires\wires\wires\clientmod\src\test\java\client\test\DerivedTest.java" \
  -d "G:\projets\wires\wires\wires\clientmod\target\test-classes" \
  -classpath "G:\projets\wires\wires\wires\clientmod\target\test-classes;" \
  --module-path "G:\projets\wires\wires\wires\clientmod\target\classes;G:\projets\wires\wires\wires\apimod\target\apimod-1.0-SNAPSHOT.jar;" \
  -sourcepath "G:\projets\wires\wires\wires\clientmod\src\test\java;" \
  --release 9 \
  -Xlint:all \
  --patch-module clientmod="G:\projets\wires\wires\wires\clientmod\target\classes;G:\projets\wires\wires\wires\clientmod\src\test\java;G:\projets\wires\wires\wires\apimod\target\apimod-1.0-SNAPSHOT-tests.jar;" \
  --add-reads apimod=ALL-UNNAMED \
  --add-reads clientmod=ALL-UNNAMED \
  --add-exports apimod/api=ALL-UNNAMED \
  --add-exports clientmod/client=ALL-UNNAMED \
  --add-modules apimod
  </code></pre></li>
  </ol>
  
  <p>-->OK, it compiles now! So your hypothesis that maven-compiler-plugin, or maven, doesn't support this yet seems validated.
  <strong>But</strong> I think it is supposed to be supported in the version I use, which is the latest. I wonder where to start to check that..</p>
  
  <p>Anyway, meanwhile, I tried configuring maven-compiler-plugin explicitly but to no avail.
  The general structure is I tried is (maven-compiler-plugin.version = 3.7.0):</p>
  
  <pre><code>&lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
      &lt;version&gt;${maven-compiler-plugin.version}&lt;/version&gt;
      &lt;configuration&gt;
          &lt;source&gt;${java.version}&lt;/source&gt;
          &lt;target&gt;${java.version}&lt;/target&gt;
          &lt;release&gt;${java.version}&lt;/release&gt;
          &lt;compilerArgs&gt;
              [...]
          &lt;/compilerArgs&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>I tried the following <code>compilerArgs</code> (corresponding mvn clean install errors in xml comments):</p>
  
  <p>1:</p>
  
  <pre><code>&lt;!--1. Syntaxically OK, but:--&gt;
  &lt;!--[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project apimod: Compilation failure: Compilation failure:--&gt;
  &lt;!--[ERROR] /G:/projets/wires/wires/wires/apimod/src/main/java/api/Base.java:[1,1]--&gt;
  &lt;!--file should be on source path, or on patch path for module--&gt;
  &lt;!--[ERROR] /G:/projets/wires/wires/wires/apimod/src/main/java/module-info.java:[1,1]--&gt;
  &lt;!--file should be on source path, or on patch path for module--&gt;
  &lt;compilerArgs&gt;
      &lt;arg&gt;--class-path=/G/projets/wires/wires/wires/clientmod/target/test-classes;&lt;/arg&gt;
      &lt;arg&gt;
          --module-path=/G/projets/wires/wires/wires/clientmod/target/classes;/G/projets/wires/wires/wires/apimod/target/apimod-1.0-SNAPSHOT.jar;
      &lt;/arg&gt;
      &lt;arg&gt;--source-path=/G/projets/wires/wires/wires/clientmod/src/test/java;&lt;/arg&gt;
      &lt;arg&gt;-Xlint:all&lt;/arg&gt;
      &lt;arg&gt;
          --patch-module=clientmod=/G/projets/wires/wires/wires/clientmod/target/classes;/G/projets/wires/wires/wires/clientmod/src/test/java;/G/projets/wires/wires/wires/apimod/target/apimod-1.0-SNAPSHOT-tests.jar;
      &lt;/arg&gt;
      &lt;arg&gt;--add-reads=apimod=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-reads=clientmod=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-exports=apimod/api=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-exports=clientmod/client=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-modules=apimod&lt;/arg&gt;
  &lt;/compilerArgs&gt;
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>&lt;!--2.--&gt;
  &lt;!--[ERROR] Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.--&gt;
  &lt;!--[ERROR] There was an error in the forked process--&gt;
  &lt;!--[ERROR] api/foo/BaseTest (wrong name: apimod/api/foo/BaseTest)--&gt;
  &lt;!--[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process--&gt;
  &lt;!--[ERROR] api/foo/BaseTest (wrong name: apimod/api/foo/BaseTest)--&gt;
  &lt;!--[ERROR]         at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:673)--&gt;
  &lt;compilerArgs&gt;
      &lt;arg&gt;--module-source-path=./*/src/main/java;./*/src/test/java/;&lt;/arg&gt;
      &lt;arg&gt;
          --source-path=/G/projets/wires/wires/wires/apimod/src/main/java;/G/projets/wires/wires/wires/apimod/src/test/java;/G/projets/wires/wires/wires/clientmod/src/test/java;/G/projets/wires/wires/wires/clientmod/src/main/java;
      &lt;/arg&gt;
      &lt;arg&gt;-Xlint:all&lt;/arg&gt;
      &lt;arg&gt;
          --patch-module=clientmod=/G/projets/wires/wires/wires/clientmod/target/classes;/G/projets/wires/wires/wires/clientmod/src/test/java;/G/projets/wires/wires/wires/apimod/target/apimod-1.0-SNAPSHOT.jar;/G/projets/wires/wires/wires/apimod/target/apimod-1.0-SNAPSHOT-tests.jar;
      &lt;/arg&gt;
      &lt;arg&gt;--add-reads=apimod=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-reads=clientmod=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-exports=apimod/api=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-exports=clientmod/client=ALL-UNNAMED&lt;/arg&gt;
      &lt;arg&gt;--add-modules=apimod&lt;/arg&gt;
  &lt;/compilerArgs&gt;    
  </code></pre>