This is the structure for my Gradle project:
- Game [parent]
|- Server [module]
-- Cache
|- Client [module]
The problem occurs when I try to access the cache from my server java project using:
new File("/cache/cacheItem.dat");
Java will now look in the parent folder (Game) for the cache instead of the server folder and I don't know how to change the root for the server project using Gradle. I would like to know how to change the root directory of the server to it's own folder instead of the parent.
You should not rely on your project structure for accessing and especially writing files. If you package your application for deployment into JARs, any of these paths will no longer be available to write data to and may change even during development, as you have noticed. Instead, you should make your cache path configurable, e.g. from a configuration file and/or command line parameter. This allows the cache path to be configured to an external location upon deployment and to some temporary path during development.
If you only need read access to files at a known location place them under src/main/resources
(or src/test/resources
for tests). Gradle by convention packages the content of this directory together with your code and everything therein will be available at runtime at root level, during development as well as after deployment. To access such resources on the classpath in Java use getClass().getResource("path/to/resource")
.