Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access files in the Project Directory with Grails

I needed some templates to render some code for users to paste. I put these into

/project-dir/grails-app/resources/templates/quickInstallCode.html

Then I tried accessing them using their relative path (grails-app/resources/templates/quickInstallCode.html), and it worked great.

When we then deployed the application to a Tomcat Server, using a .war file, the paths began pointing to a wrong location.

ERROR  call, Template file /var/lib/tomcat6/grails-app/resources/templates/quickInstallCode.html not found.

I assumed, that Grails, giving good defaults for everything would handle this mess for me, but it seems like it does not.

I also tried this call, and it seemed to work great, but when deployed, the BuildSettingsHolder did not contain build Settings, which resulted in a fatal error.

BuildSettingsHolder.settings.baseDir.toString()

http://grails.org/doc/latest/api/grails/util/BuildSettingsHolder.html http://grails.org/doc/latest/api/grails/util/BuildSettings.html

I am pretty frustrated that I cannot get this easy task to work, but the reason that this is so complicated seems to be that all Files are encapsuled in a WAR and not unpacked on the Server.

So the Questions are:

  • Where in your Project would you put Files like this?
  • How to get a reliable and stable way to access this files? I just need a stable path to a base directory, without having to hardcode something in the configuration ... This cannot be so hard.
like image 463
Paul Weber Avatar asked Oct 12 '22 02:10

Paul Weber


2 Answers

I have 2 solution to propose for this situation:

  1. Save the template in the database, in a setting table. This way guarantees that nothing can go wrong.

  2. You can consider using the resource folder like Sachin & Nirmal has proposed. About security, I think you can configure SpringSecurity Plugin to protect the specific resources, so that it can only be accessed by the site user.

like image 125
Hoàng Long Avatar answered Dec 10 '22 22:12

Hoàng Long


Take a look at this link and try to use the getResource that spring provides. Its way more flexible and configurable.

def filePath = "resources/file.txt"
def appHolder=ApplicationHolder.application.parentContext.getResource("classpath:$filePath")

By the way Conf is on the root of the classpath, you can stick the files in src/java or src/groovy.

like image 31
Nirmal Avatar answered Dec 10 '22 23:12

Nirmal