Grails Plugin Get Plugin's Root or Installed Directory
Asked Answered
D

2

6

I'm writing a plugin and I'm trying to write a file inside of the Plugin's root or installed directory (not sure what to refer to this to). I can't seem to figure out how to get a hold of this value. Doing System.properties['base.dir'] will result in the implementing Grails project's root directory. So right now I have two directories:

C:/PluginProject/

C:/GrailsAppThatUsesPlugin/

It's my understanding that when this becomes a distributed plugin a user will similarly have two directories:

GRAILS_HOME/grails-version/projects/projectName/plugins/myPlugin/

C:/GrailsAppThatUsesPlugin/

Inside of my plugin project I need to create a file. It needs to be inside of my plugin because the file I'm writing needs to reference other files that my plugin provides. The few things that I've tried that haven't worked are:

  1. System.properties['base.dir']
  2. new File("")
  3. In a groovy script within a plugin you can simply refer to pluginNamePluginDir but I'm trying to access this from a POGO.
  4. Looking at all of the System.properties none of them have the plugin directory
  5. grailsApplication doesn't seem to contain this type of information either.

Any help is greatly appreciated.

Davis answered 10/2, 2012 at 22:42 Comment(0)
B
4

You can use this:

import org.codehaus.groovy.grails.plugins.PluginManagerHolder

def pluginManager = PluginManagerHolder.pluginManager
def plugin = pluginManager.getGrailsPlugin(pluginName)
def pluginDir = plugin.descriptor.file.parentFile

The plugin name has to be of the form 'spring-security-core', not 'springSecurityCore'

Barbican answered 11/2, 2012 at 6:4 Comment(2)
I put this code in a test and plugin.descriptor is always null. The plugin is not null. Also if I do plugin.getPluginDir() that throws a NPE. Any thoughts? Do I need to configure something for the descriptor to be available? Thanks for your help!Davis
I ended up using something like this: #1163593. It got me the path that I was looking for. I was able to use the PluginManagerHolder to get other information I needed. Thanks Burt!Davis
G
12

This worked for me (both in-place and regular plug-ins):

import org.codehaus.groovy.grails.plugins.GrailsPluginUtils

GrailsPluginUtils.pluginInfos.find { it.name == pluginName }.pluginDir
Gunthar answered 6/8, 2012 at 15:34 Comment(1)
Thanks. I am using this way to avoid the find operation: org.codehaus.groovy.grails.plugins.GrailsPluginUtils.getPluginDirForName(pluginName).file.absolutePathRunesmith
B
4

You can use this:

import org.codehaus.groovy.grails.plugins.PluginManagerHolder

def pluginManager = PluginManagerHolder.pluginManager
def plugin = pluginManager.getGrailsPlugin(pluginName)
def pluginDir = plugin.descriptor.file.parentFile

The plugin name has to be of the form 'spring-security-core', not 'springSecurityCore'

Barbican answered 11/2, 2012 at 6:4 Comment(2)
I put this code in a test and plugin.descriptor is always null. The plugin is not null. Also if I do plugin.getPluginDir() that throws a NPE. Any thoughts? Do I need to configure something for the descriptor to be available? Thanks for your help!Davis
I ended up using something like this: #1163593. It got me the path that I was looking for. I was able to use the PluginManagerHolder to get other information I needed. Thanks Burt!Davis

© 2022 - 2024 — McMap. All rights reserved.