如何在 Groovy 中获得运行脚本的路径?

我正在编写一个 Groovy 脚本,希望通过存储在同一文件夹中的属性文件来控制它。但是,我希望能够从任何地方调用这个脚本。当我运行脚本时,它总是根据运行的位置来查找属性文件,而不是根据脚本的位置。

如何从脚本中访问脚本文件的路径?

59910 次浏览

你是正确的,new File(".").getCanonicalPath()不工作。返回的 工作目录

获取脚本目录

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

获取脚本文件路径

scriptFile = getClass().protectionDomain.codeSource.location.path

如果您运行的是 Groovy 代码 作为剧本,那么这是有意义的,否则整个想法就会变得有点混乱,IMO。解决方案在这里: https://issues.apache.org/jira/browse/GROOVY-1642

基本上,这需要改变 startGroovy.sh,使其作为一个环境变量传递到 Groovy 脚本的位置。

只要这些信息不是由 Groovy 直接提供的,就可以修改 Groovy。(sh | bat)启动程序脚本,使此属性作为系统属性可用: 对于 unix 框,只需更改 $GROOVY _ HOME/bin/GROOVY (sh 脚本)即可

export JAVA_OPTS="$JAVA_OPTS -Dscript.name=$0"

在调用 startGroovy 之前 视窗: 在 startGroovy.bat 中,在带有 Init label (就在参数发出声音之前) :

@rem get name of script to launch with full path
set GROOVY_SCRIPT_NAME=%~f1

在批处理文件中“ set”一行后面稍微往下一点 JAVA _ OPTS =% JAVA _ OPTS%-Dgroovy.STARTER.conf = “% STARTER _ CONF%”添加 台词

set JAVA_OPTS=%JAVA_OPTS% -Dscript.name="%GROOVY_SCRIPT_NAME%"

适用于级别使用者

当我开始和 gradle 一起工作时,我也有同样的问题。我想编译我的节约远程节约编译器(由我的公司定制)。

下面是我如何解决我的问题:

task compileThrift {
doLast {
def projectLocation = projectDir.getAbsolutePath(); // HERE is what you've been looking for.
ssh.run {
session(remotes.compilerServer) {
// Delete existing thrift file.
cleanGeneratedFiles()
new File("$projectLocation/thrift/").eachFile() { f ->
def fileName=f.getName()
if(f.absolutePath.endsWith(".thrift")){
put from: f, into: "$compilerLocation/$fileName"
}
}
execute "mkdir -p $compilerLocation/gen-java"
def compileResult = execute "bash $compilerLocation/genjar $serviceName", logging: 'stdout', pty: true
assert compileResult.contains('SUCCESSFUL')
get from: "$compilerLocation/$serviceName" + '.jar', into: "$projectLocation/libs/"
}
}
}
}

从 Groovy 2.3.0开始,@SourceURI注释可以用来用脚本位置的 URI 填充变量。这个 URI 可以用来获取脚本的路径:

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 文件系统提供程序的 URI 方案类型)时,这才会起作用,否则 Paths.get(URI)调用将抛出 FileSystemNotFoundException 异常。特别是,某些 Groovy 运行时(如 groovyshell 和 nextflow)返回一个 data: URI,它通常不会与已安装的 FileSystemProvider匹配。

还有一个解决方案,即使您使用 GrovyConsole 运行脚本,它也能正常工作

File getScriptFile(){
new File(this.class.classLoader.getResourceLoader().loadGroovySource(this.class.name).toURI())
}


println getScriptFile()

解决方案 : 对我们来说,它运行在 ANT 环境中,并在 Java 环境属性(System.setProperty( "dirAncestor", "/foo" ))中存储一些位置父节点(知道子路径) ,我们可以通过 Groovy 的 properties.get('dirAncestor')访问目录祖节点。
也许这会对这里提到的一些情况有所帮助。