How do I tell custom-gradle-plugin to read its own file instead of looking for it in the plugin project?

Hi there

I have written a gradle-plugin, and it has a configuration file in its resources, but after connecting to the project, gradle-plugin tries to look for it in the plugin project. How to explain gradle-plugin to read this file in itself and not in the project?

try (var inputStream = new FileInputStream("src/main/resources/properties-default.yml")) 

Things like new File(...) or new FileInputStream(...) with a relative path are almost always wrong, in any software project, independent from Gradle. Because those APIs resolve the relative path according to the current working directory of the caller. For a Gradle build this often is the project directory, but this is not always the case, not guaranteed and might change in the future. The practically only case where this is valid, is with a path the user have as argument to a command, as this is usually a relative path from the working directory.

In your case what your want is to access the file as resource from the class path through Class or Classloader.

1 Like