更新时间:2023-12-05 14:13:52
从 Groovy 2.3.0 开始,@SourceURI
注释可用于使用脚本位置的 URI 填充变量.然后可以使用此 URI 获取脚本的路径:
As of Groovy 2.3.0 the @SourceURI
annotation can be used to populate a variable with the URI of the script's location. This URI can then be used to get the path to the script:
import groovy.transform.SourceURI
import java.nio.file.Path
import java.nio.file.Paths
@SourceURI
URI sourceUri
Path scriptLocation = Paths.get(sourceUri)
请注意,这仅在 URI 是 file:
URI(或安装了 FileSystemProvider),否则为 FileSystemNotFoundException 将由Paths.get(URI)
调用.特别是,某些 Groovy 运行时(例如 groovyshell 和 nextflow)会返回 data:
URI,该 URI 通常与已安装的 FileSystemProvider
不匹配.
Note that this will only work if the URI is a file:
URI (or another URI scheme type with an installed FileSystemProvider), otherwise a FileSystemNotFoundException will be thrown by the Paths.get(URI)
call. In particular, certain Groovy runtimes such as groovyshell and nextflow return a data:
URI, which will not typically match an installed FileSystemProvider
.