Grails application root path - grails

Is there a variable where I can find out the root directory of my Grails application?
for example, I have a folder named chart under \%root%\web-app\images\ where I put my charts in. Once I deploy my Grails application on Jetty, I will get FileNotFoundException because the root path becomes /srv/www/vhosts/domain-name/jetty-version/
I would like to know if there is a variable that returns the root path (like /srv/www/vhosts/domain-name/jetty-version/webapps/myapp), and there should be because CSS uses relative path just fine.

solved.
request.getSession().getServletContext().getRealPath("/")
this actually gives me the path to where my application puts the images, css, WEB-INF, etc. folders.

System.properties['base.dir']

I know it is an old question, but this could work if you are not in an http request:
ServletContextHolder.servletContext.getRealPath('/')

If you want to establish this is GSPs try this:
${createLink(uri: '/')}

Related

Path-relative URIs for resource files VS routes that "look-like" sub-directories

I'm using the URL like web.com/sys/test, which is rewritten in nginx config to web.com?system=sys&id=test via rewrite ^/(\w+)/(\w+)$ /?system=$1&id=$2 break;.
Also, all my resource files have relative path.
Therefore I get error:
Unable to find web.com/sys/css/user.css, however user.css is located in /css/.
Is there a way to solve the problem remaining the relative path for resource files?
Used base tag:
<base href="/">
However, in some cases this method may just add a whole lot of new problems. Look at this link

Generate URL of resources that are handled by Grails AssetPipeline

I need to access a local JSON file. Since Grails 2.4 implements the AssetPipeline plugin by default, I saved my local JSON file at:
/grails-app/assets/javascript/vendor/me/json/local.json
Now what I need is to generate a URL to this JSON file, to be used as a function parameter on my JavaScript's $.getJSON() . I've tried using:
var URL.local = ""${ raw(asset.assetPath(src: "local.json")) }";
but it generates an invalid link:
console.log(URL.local);
// prints /project/assets/local.json
// instead of /project/assets/vendor/me/json/local.json
I also encountered the same scenario with images that are handled by AssetPipeline1.9.9— that are supposed to be inserted dynamically on the page. How can I generate the URL pointing this resource? I know, I can always provide a static String for the URL, but it seems there would be a more proper solution.
EDIT
I was asked if I could move the local JSON file directly under the assets/javascript root directory instead of placing it under a subdirectory to for an easier solution. I prefer not to, for organization purposes.
Have you tried asset.assetPath(src: "/me/json/local.json")
The assets plugin looks in all of the immediate children of assets/. Your local.json file would need to be placed in /project/assets/foo/ for your current code to pick it up.
Check out the relevant documentation here which contains an example.
The first level deep within the assets folder is simply used for organization purposes and can contain folders of any name you wish. File types also don't need to be in any specific folder. These folders are omitted from the URL mappings and relative path calculations.

grails elfinder plugin: use placeholder in rootDir

in my Grails project I'm using elfinder plugin to manage files and directories. I want to have a dynamic root directory, because I use the plugin for different folders.
The path of directory is like the following:
grails.plugin.elfinder.rootDir = "${userHome}/docm_patients_doc/{patientcf}/"
where patientcf is the id of an entity in my application. When I enter into the show.gsp page of that entity, I need to replace the patientcf with the related value.
How can I do it?
EDIT:
I've tried to modify the placeholder before the script and div that shows elfinder in gsp page, but I notice that the path is not modified. Maybe the gsp is not the place in which the placeholder can be modified...
I am author of elfinder plugin, though plugins isn't developed with multiple roots in mind.
You can try this. Plugin registers a spring bean with name elfinderFileManager which has a property with name ‘root’ which is path to your root directory. Try setting the root property at runtime. The bean can be injected in your controller/service and you can try changing the root property.

How to get application's root directory?

How do I get application's root directory within an action?
The first thing ZF2 does is to change the current dir via chdir(dirname(__DIR__));
This means that every future include is based off of the ROOT PATH of your application and NOT the public folder. Or any other current folder.
Of course this only holds true for PHP-Files.
If you want to define the root path manually, you'd go to /public/index.php and add a line like define('ROOT_PATH', dirname(__DIR__));
As i said before, for INCLUDES this is NOT required though ;) as you're ALWAYS in the root folder when it comes to PHP-Files ;)
getcwd() works best for me, DIR return the module root. Which isn't much use in this case
#Sam:
I don't really understand your question. Basically the current path equals the ZF2-Apps Root. [...] You can always go up-levels, too via ../
Not exactly. When You create module shared within several applications ex. FileUpload Module in vendor, outside application. You would like to upload file to Application subdirectory not shared module :) In this case __DIR__ equals module path not app path and ../ wouldn,t be good solution ;)
I like ROOT_PATH as You have mentioned:
define('ROOT_PATH', dirname(__DIR__));
or even better:
getcwd()

How can i get the path of dlls when using Ninject binding?

My web app is in 'D:\WebApp folder' and there is a dll in 'D:\Bin' folder , I want to using ninject bindthe dll using the relative path instead of using the absolutely path 'D:\Bin' directly, since the absolutely path maybe changed somewhere.
the code like the following:
string path="how to get the path";
kernel.Bind(x => x.FromAssembliesInPath("path")
.SelectAllClasses()
.BindDefaultInterface());
How can i got the path in the code?
The path can be relative from the working dir e.g. "..\bin\the.dll". Make sure the path is in the probing path configuration of the application.

Resources