Get path to groovy source file at runtime

Given the following directory structure:

/home/some/random/foler/myScript.grooy

... how can I programmatically get the path to the parent directory myScript.grooy directly in the script itself?

Ultimately, I try to read multiple files from the same directory the script is in.

EDIT: trying to run it on Windows 7, Groovy 2.0.1, Groovy console

+2
source share
1 answer

Well, the solution is in the Java File class:

println new File(".").absolutePath

If you want to get every groovy script in the same directory, perhaps you could use some other features in the groovy JDK, for example eachFile:

def files = []
new File(".").eachFile { 
    if (it.name.endsWith(".groovy") ) files << it 
}
println files

script, (http://jira.codehaus.org/browse/GROOVY -1642)

JIRA ( ):

URL scriptUrl = getClass().classLoader.resourceLoader
    .loadGroovySource(getClass().name)
+12

All Articles