title: Where do resource files go in a Gradle project that builds a Java 9 module?
body: <p>As of IDEA 2018.2.1, the IDE starts error-highlighting packages "not
  in the module graph" from dependencies that have been modularized. I added a <code>module-info.java</code> file to my project and added the
  requisite <code>requires</code> statements, but I'm now having trouble accessing
  resource files in my <code>src/main/resources</code> directory.</p>
  
  <p>(For a complete example, see <a href="https://github.com/chronodm/module-resource-dirs" rel="noreferrer">this GitHub project</a>.)</p>
  
  <p>When I used <code>./gradlew run</code> or <code>./gradlew installDist</code> + the resulting
  wrapper script, I was able to read resource files, but when I ran my
  app from the IDE, I wasn't.</p>
  
  <p>I filed an <a href="https://youtrack.jetbrains.com/issue/IDEA-197469" rel="noreferrer">issue</a>
  with JetBrains, and what I learned was that IDEA was using the module
  path, while Gradle, by default, was using the classpath. By adding
  the following block to my <code>build.gradle</code>, I was able to get Gradle
  to <strong>also</strong> … not be able to read any resource files.</p>
  
  <pre><code>run {
      inputs.property("moduleName", moduleName)
      doFirst {
          jvmArgs = [
                  '--module-path', classpath.asPath,
                  '--module', "$moduleName/$mainClassName"
          ]
          classpath = files()
      }
  }
  </code></pre>
  
  <p>I tried <code>export</code>-ing the resource directory I was interested in as a
  "package", and at compile time got a build failure with:</p>
  
  <blockquote>
    <p>error: package is empty or does not exist: mydir</p>
  </blockquote>
  
  <p>Using <code>opens</code> instead of <code>exports</code> got the same error, though downgraded
  to a warning. </p>
  
  <p>I even tried moving the <code>mydir</code> resource directory under <code>src/main/java</code>,
  but this produced the same errors / warnings, and also resulted in the
  resources not being copied to the <code>build</code> directory.</p>
  
  <p>Where are resources supposed to go in Java 9, and how do I access them?</p>
  
  <hr>
  
  <p><strong>Note:</strong> I've edited this question considerably after continuing to 
  research the problem. In the initial question, I was also trying to
  figure out how to list the files in a resource directory, but in the
  course of the investigation I determined that was a red herring -- 
  first, because reading resource directories only works when the
  resource is being read from a <code>file:///</code> URL (and maybe not even then),
  and second because plain files weren't working either, so clearly the
  problem was with resource files in general and not specifically with
  directories.</p>
  
  <hr>
  
  <p><strong>Solution:</strong> </p>
  
  <p>Per <a href="https://stackoverflow.com/a/51921521/27358">Slaw's answer</a>, I added the following to <code>build.gradle</code>:</p>
  
  <pre><code>// at compile time, put resources in same directories as classes
  sourceSets {
    main.output.resourcesDir = main.java.outputDir
  }
  
  // at compile time, include resources in module
  compileJava {
    inputs.property("moduleName", moduleName)
    doFirst {
      options.compilerArgs = [
        '--module-path', classpath.asPath,
        '--patch-module', "$moduleName=" 
          + files(sourceSets.main.resources.srcDirs).asPath,
        '--module-version', "$moduleVersion"
      ]
      classpath = files()
    }
  }
  
  // at run time, make Gradle use the module path
  run {
    inputs.property("moduleName", moduleName)
    doFirst {
      jvmArgs = [
        '--module-path', classpath.asPath,
        '--module', "$moduleName/$mainClassName"
      ]
      classpath = files()
    }
  }
  </code></pre>
  
  <p><strong>Side note:</strong> Interestingly, if I <em>don't</em> continue on to add Slaw's code that makes the <code>run</code> task execute against the JAR, attempting to read a resource directory <code>InputStream</code> in the run task now <a href="https://gist.github.com/chronodm/fe5788ebf0e93d84dc55e6a80472f4ef" rel="noreferrer">throws an <code>IOException</code></a> instead of providing the list of files. (Against the JAR, it simply gets an empty <code>InputStream</code>.)</p>