Friday, September 30, 2011

Find directory path of the current groovy script

Bash or PHP script has a function that returns the directory path of the current script file.

$my_dir=dirname(__FILE__);

Groovy script lacks such a capability but an alternative is available by calling

def my_dir = new File(getClass().protectionDomain.codeSource.location.path).parent

2 comments:

  1. Thank you. Worked perfect in my config.groovy to set grails.plugin.reveng.jdbcDriverJarPath.

    def my_dir = new File(getClass().protectionDomain.codeSource.location.path).parent
    grails.plugin.reveng.jdbcDriverJarPath = "$my_dir/../lib/ojdbc6.jar"

    ReplyDelete
  2. throws
    Caught: java.net.MalformedURLException: unknown protocol: c
    java.net.MalformedURLException: unknown protocol: c
    when any folder in the path has spaces

    ReplyDelete