Grails system property base.dir

I have an application to download a grails file.

I use transferTo to save the file in the file system.

To get the base path in my controller, I use

def basePath = System.properties['base.dir'] // HERE IS HOW I GET IT

        println "Getting new file"
        println "copying file to "+basePath+"/files"
        def f = request.getFile('file')
        def okcontents = ['application/zip','application/x-zip-compressed']
        if (! okcontents.contains(f.getContentType())) {
            flash.message = "File must be of a valid zip archive"
            render(view:'create', model:[zone:create])
            return;
        }
              if(!f.empty) {
                  f.transferTo( new File(basePath+"/files/"+zoneInstance.title+".zip") )
              }
              else 
              {
                  flash.message = 'file cannot be empty'
                      redirect(action:'upload')
              }
        println "Done getting new file"

For some reason, this is always null when deployed to my WAS 6.1 server.

Why does this work when running dev, but not in prod on the WAS server? Should I access this information in a different way?

+5
source share
4 answers

Thanks j

I found the best dynamic solution. As a rule, I never wanted to code absolute paths to any part of the software. Property file or not.

So here is how to do it:

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

grailsAttributes is available in any controller.

getResource ( ) - -.

, , dev- toString "C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\ dir,

C:\WORKSPACEFOLDER\PROJECTFOLDER\-\

WAS 6.1, . toString .

mugafuga

+4

...

grailsApplication.parentContext.getResource("dir/or/file").file.toString()

(, bootstrap)? ..

def grailsApplication

!

+4

Grails, dev, Gant , basedir.

grails.bat grails.sh script, :

Unix: -Dbase.dir="." \
Windows: set JAVA_OPTS=%JAVA_OPTS% -Dbase.dir="."

dev, .

When you take WAR and deploy, you no longer use these scripts, and therefore you need to solve the problem differently; you can either

  • Set the property yourself when running the script for the application server, for example: -Dbase.dir =. / Some / dir .. however
+3
source

Another option:

def basePath = BuildSettingsHolder.settings.baseDir

+2
source

All Articles