Joe Beuckman
Joe Beuckman

Reputation: 2296

Grails files visible to app running locally / missing when deployed?

I have a folder in web-app/ called 'templates'. When I 'run-war' locally, this works great:

new File('web-app/templates/').list()

and I can list the files in my folder. But, when I deploy my war file to a remote tomcat, the list is empty. Do I have to tell grails explicitly to copy the folder into the war file?

Upvotes: 0

Views: 413

Answers (2)

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

Your code is highly dependent both on the war file being 'exploded' (which it may or may not be in Tomcat, depending on configuration), and on what the container sets as the 'current directory' for your application.

IIRC, the Java EE spec says that the only place in the file system that you can safely access directly is the temporary directory identified by System.getProperty("java.io.tmpdir").

Of course, lots of applications do otherwise, but it's non-standard, and therefore fragile.

The safest thing to do is to place the template directory in the classpath (i.e. somewhere in the tree under the WEB-INF/classes directory in the war file), and then access them via the ClassLoader and its findResource and getResource methods.

Upvotes: 0

Bob McCracken
Bob McCracken

Reputation: 516

Perhaps the following is what you're looking for:

def baseFolder = grailsAttributes.getApplicationContext().getResource("/").getFile().toString()

def dirList= new File(baseFolder + "/templates").list()

This worked for me when deploying a WAR to tomcat. In my case, the baseFolder was the absolute path to the web-app directory in my app.

See http://googolflex.com/?p=664

Upvotes: 3

Related Questions