Search code examples
gradleintellij-ideagrailsresourcesclasspath

Classpath root pointing to grails-app/views - how to amend?


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:") or
  • grailsApplication.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.

  1. How can I extend my classpath to folders outside of grails-app/views?
  2. How can I make sure that the solution works in both, IntelliJ and Tomcat deployment?

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

  • grails-app/conf/application.yml
  • grails-app/conf/application.groovy
  • grails-app/conf/ApplicationResources.groovy (I've tried to add a resource entry here and rebuild the project)
  • ... (Please feel free to have a look in the project in case of further config detail questions.)

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.


Solution

  • @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:

    • use Puneet's proposed syntax: getClass().getResource("/myfolder/example.json")
    • after I had created grails-app/conf/myfolder/example.json