如何使用@SourceURI批注在Groovy 2.3中检索脚本文件的完整路径? [英] How to use @SourceURI annotation to retrieve the full path of the script file in Groovy 2.3?

查看:153
本文介绍了如何使用@SourceURI批注在Groovy 2.3中检索脚本文件的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时检索Groovy 2.3中脚本文件的完整路径. 实际上,我遇到的问题与此处描述的问题相同:获取groovy的路径源文件在运行时.

I need to retrieve at runtime the full path of the script file in Groovy 2.3. Actually I have the same problem than the one described here: get path to groovy source file at runtime.

我的脚本可以通过GroovyShell执行,也可以不执行.

My script can be executed from GroovyShell or not.

我的脚本位于:

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

=>我很想在运行时检索此路径.

如果我使用被接受的解决方案:

If I use the solution that was accepted:

println new File(".").absolutePath

我得到的实际上是:

C:\groovy-2.3.7\.

这是普通的主文件夹.那是不对的. 另一个建议的答案是:

which is the groovy home folder. That is not correct. The other proposed answer ie :

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

仅当我的脚本在类路径中或使用groovy-starter.conf中的load指令在groovy启动时加载时才起作用.否则,它返回null.我可以使用此方法,但它并不令人满意,因为它就像传递参数一样,此处的目标是用户可以在任何地方执行脚本而无需修改或配置.

works only when my script is in the classpath, or when it is loaded at groovy startup using load directive in groovy-starter.conf. Otherwise it returns null. I could use this method but it is not satisfying because it is like passing a parameter and the goal here is that users to be able to execute the script from anywhere without modification or configuration.

我也把这个关注这个问题的JIRA变成了红色: JIRA-1642

I also red this JIRA who focus on this question: JIRA-1642

解决方案似乎使用了为此目的创建的 @SourceURI批注.问题是我无法使其正常工作.我尝试执行以下代码所示的代码用法: SourceURI

Solution seems to use the @SourceURI annotation that was created for this purpose. Problem is I am not able to make it work. I try to execute the code usage shown at: SourceURI

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

我得到的(groovy 2.3.7)不是路径,而是源代码:

What I get is (groovy 2.3.7) is not the path but the source code :

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批注来检索脚本文件的路径?

How to use the @SourceURI annotation to retrieve the path of the script file ?

推荐答案

我正在寻找与您相同的脚本路径信息.我注意到通过外壳运行以下命令时:

I was looking for the same script path information you were. I noticed that when running the following via the shell:

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

我得到以下有用的结果(注意路径中的值):

I get the following, useful results (note the value in path):

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中,则会得到以下信息:

But if I paste the same thing into the GroovyConsole, I get the following:

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

这是在Ubuntu上的Groovy 2.3.9上.

This is on Groovy 2.3.9 on Ubuntu.

因此,似乎GroovyConsole的启动可能是一个问题,但实际上注解确实产生了所需的路径.

So it seems like the GroovyConsole's startup might be a problem, but the annotation does in fact produce the desired path.

这篇关于如何使用@SourceURI批注在Groovy 2.3中检索脚本文件的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆