How to use @SourceURI annotation to get full path to script file in Groovy 2.3?

I need to get at runtime the full path to the script file in Groovy 2.3. I actually have the same problem as described here: getting the path to the Groovy source file at runtime .

My script can be executed from GroovyShell or not.

My script is located at:

C:\users\myname\documents\scripts\myscript.groovy

=> I just want to get this path at runtime.

If I use the accepted solution:

println new File(".").absolutePath

what i actually get:

C:\groovy-2.3.7\.

which is the Groovy home folder. This is not true. Another suggested answer:

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

, script Groovy load groovy -starter.conf. null. , , , , script .

JIRA, : JIRA-1642

, , @SourceURI . , . , : SourceURI

@groovy.transform.SourceURI def sourceURI
assert sourceURI instanceof java.net.URI
path = sourceURI.toString()
println "path = $path"

(groovy 2.3.7) - , :

path = data:,@groovy.transform.SourceURI%20def%20sourceURI%0A%0A%20assert%20sourceURI%20instanceof%20java.net.URI%0Apath%20=%20sourceURI.toString()%0Aprintln%20%22path%20=%20$path%22

@SourceURI script?

+4
2

script. , :

@groovy.transform.SourceURI def sourceURI
assert sourceURI instanceof java.net.URI
sourceURI.properties.each { i -> 
    println i
}

( ):

rawAuthority=null
opaque=false
scheme=file
rawQuery=null
port=-1
rawUserInfo=null
path=/home/ttresans/GroovyScripts/TestSourceURI.groovy
class=class java.net.URI
absolute=true
schemeSpecificPart=/home/ttresans/GroovyScripts/TestSourceURI.groovy
rawPath=/home/ttresans/GroovyScripts/TestSourceURI.groovy
query=null
fragment=null
host=null
authority=null
rawFragment=null
userInfo=null
rawSchemeSpecificPart=/home/ttresans/GroovyScripts/TestSourceURI.groovy

GroovyConsole, :

rawAuthority=null
opaque=true
scheme=data
rawQuery=null
port=-1
rawUserInfo=null
path=null
class=class java.net.URI
absolute=true
schemeSpecificPart=,@groovy.transform.SourceURI def sourceURI
assert sourceURI instanceof java.net.URI
sourceURI.properties.each { i -> 
    println i
}

rawPath=null
query=null
fragment=null
host=null
authority=null
rawFragment=null
userInfo=null
rawSchemeSpecificPart=,@groovy.transform.SourceURI%20def%20sourceURI%0Aassert%20sourceURI%20instanceof%20java.net.URI%0AsourceURI.properties.each%20%7B%20i%20-%3E%20%0A%20%20%20%20println%20i%0A%7D%0A

Groovy 2.3.9 Ubuntu.

, , GroovyConsole , .

+3
0

All Articles