1

Here is the simplified structure of each module of my project :

src
|- main
  |- java
    <My classes>
  |- resources
    config.properties
|- test
  |- java
    MainTest
  |- resources

I recently updated IntelliJ to version 2019.2. As Jetbrains removed the "uncheck create separate modules per source set" (see this post), when I import a Gradle project, I end up with one module having two sub-modules : main and test.

So, in Intellij project structure, I have this :

main sub-module

main module structure

test sub-module

test module structure

In some class, I load the config.properties file this way :

InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream("config.properties");

But when I run MainTest.main() in IntelliJ, which at some point calls the bit of code above, the resulting InputStream is null.

I guess that the test module classpath doesn't include the main resources folder...

How can I fix that ?

When I could check the box "uncheck create separate modules per source set", I had just one module, with one classpath, and my life was easier... Is there a way, maybe using Gradle configuration in build.gradle file, to enforce one unique module/source set ?

Eria
  • 2,653
  • 6
  • 29
  • 56

1 Answers1

1

IntelliJ 2019.2 defaults to delegating all build and run tasks to Gradle, so it shouldn't matter how you have structured the project in IntelliJ. You could also try running your test on the command line with gradle test to see if you get the same problem (which you should).

The 'test' runtime classpath does includes the resource folder from both the 'test' and the 'main' folder. So this also shouldn't be a problem.

In the screenshot you've shown of the main modules, there are no config.properties file in the resource folder. Did you put it in the META-INF sub-folder instead? If it actually is in the root of the resource folder, try referencing it with a slash (/) in the path, e.g.:

MyClass.class.getClassLoader().getResourceAsStream("/config.properties")
                                                   ^^^
Bjørn Vester
  • 6,851
  • 1
  • 20
  • 20
  • `config.properties` is located in the `src/main/resources` folder. Only IntelliJ module structure view shows only folders. Same behaviour with command line and a slash. – Eria Sep 13 '19 at 12:56