How to add directory with resources that is outside of the submodule?

I’m trying to share resources between all submodules but can’t find a solution for that.

Structure of the project:

-root
    - src
    - data:
          file.csv
    - benchmark_module
    - another_module

There are some similar questions on the forum. For example solution from How to add a single extra resource to the existing resources would not work for me

processResources {
   from("data") 
}

First of all I don’t understand whether “extra” should be a directory or a file ( or it could be both). If I will try to access resource by running test from benchmark_module module like below I would get null.

getClass.getResource("/file.csv") 

So it means that content of data directory was not copied over to a resource output directory.

Any ideas are appreciated!

Depending on what folder you invoke Gradle from, you may also get different results when you use relative references. Try with from("$rootDir/data") instead.

Also, are you declaring the processResources in the root module and then make dependencies to the root from the sub-modules, or did you put processResources in each sub-module?

Thank you for your time Bjørn Vester!

One maybe important note: I’m running tests with IDE. But my understanding is that integration between Gradle and IDE should respect what is written in config files. And I’m running test class from within submodule.

I’ve given a try to

processResources {
    from("$rootDir/data")
}

but still It would not see my resource file.

But the following would work if I put it in a submodule’s config even though it is not a solution for this topic:

sourceSets {
    main {
        resources {
            srcDirs += [
                    "$rootDir/data"
            ]
        }
    }
}

It works because my submodule depends on root project. I tried to use name of the submodule instead of main but this would not work. Are these main and test words reserved ones and I can use arbitrary sourceSet names only for convenience but in the end should assign values to main or test?

Both approaches should work.

You can try running the test from the command line to see if this is an issue with the Gradle build or in the way your IDE runs your tests. If it is an IDE issue, it might be because your IDE is not delegating the build to Gradle.

If it is a Gradle issue, you can try using processResources in the root project and check if the file ends up in the jar file for the root project. If it does, the problem is likely in the way the sub-modules depend on the root project.

As for using sourceSets, the difference is that if you use main it will show up in the final jar file, whereas test only makes it available on the classpath when testing. So it depends on whether this is a test file or a “production” file. These two names are created by the java plugin.

You can also create your own sourceSet with a name you decide, but in that case you will have to glue it together with the existing tasks for compiling and/or testing. This is mostly useful if the new sourceSet is separate to the other ones, like sources for an integration test or classes that need to be enhanced somehow. And that doesn’t seem to be what you want in this case.