My goal is to add some Json files as project resources accessible from the code. The solution must work in IntelliJ and deployed in a Tomcat. I don't mind if the files are located under grails-app/conf
or src
.
Getting my classpath root using one of
grailsApplication.parentContext.getResource("classpath:")
orgrailsApplication.parentContext.getResource("classpath:.")
resolves in $MY_APP/grails-app/views
However, trying to navigate from this root to grails-app/src
using
grailsApplication.parentContext.getResource("classpath:..")
results in: "java.io.FileNotFoundException: class path resource [..] cannot be resolved to URL because it does not exist". This might be acceptable since a root is a root, I guess.
grails-app/views
?I'm using Gradle 3.5, Grails 3.3.11, Groovy 2.4.10. I'm working in IntelliJ. Rebuilding the project using the Gradle view has not helped so far. The project setup consists of
I've been exploring endless questions and answers of similar "how can I add my file to the classpath" questions, but nothing has helped so far.
@PuneetBehl's answer helped me realizing that it isn't about the project code configuration. It is about the configuration of my IDE, which is IntelliJ. You can set Project Structure... --> Project Settings --> Modules. There, "Resource Folders" on the right hand side was listing as: "conf", "i18n" and "views". I dont know why IntelliJ chose "views" for the path "/."
.
Anyway, it was enough to:
getClass().getResource("/myfolder/example.json")