你如何在groovy中获得正在运行的脚本的路径?

时间:2021-10-01 07:13:28

I'm writing a groovy script that I want to be controlled via a properties file stored in the same folder. However, I want to be able to call this script from anywhere. When I run the script it always looks for the properties file based on where it is run from, not where the script is.

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

How can I access the path of the script file from within the script?

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

4 个解决方案

#1


As of Groovy 2.3.0 the @groovy.transform.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:

从Groovy 2.3.0开始,@ groovy.transform.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)

#2


You are correct that new File(".").getCanonicalPath() does not work. That returns the working directory.

你是正确的新文件(“。”)。getCanonicalPath()不起作用。返回工作目录。

To get the script directory

获取脚本目录

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

To get the script file path

获取脚本文件路径

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

#3


This makes sense if you are running the Groovy code as a script, otherwise the whole idea gets a little confusing, IMO. The workaround is here: https://issues.apache.org/jira/browse/GROOVY-1642

如果您将Groovy代码作为脚本运行,这是有道理的,否则整个想法会有点混乱,IMO。解决方法如下:https://issues.apache.org/jira/browse/GROOVY-1642

Basically this involves changing startGroovy.sh to pass in the location of the Groovy script as an environment variable.

基本上这涉及更改startGroovy.sh以将Groovy脚本的位置作为环境变量传递。

#4


For gradle user

对于gradle用户

I have same issue when I'm starting to work with gradle. I want to compile my thrift by remote thrift compiler (custom by my company).

当我开始使用gradle时,我遇到同样的问题。我想通过远程thrift编译器编译我的thrift(由我的公司定制)。

Below is how I solved my issue:

以下是我解决问题的方法:

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/"
            }
        }
    }
}

#1


As of Groovy 2.3.0 the @groovy.transform.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:

从Groovy 2.3.0开始,@ groovy.transform.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)

#2


You are correct that new File(".").getCanonicalPath() does not work. That returns the working directory.

你是正确的新文件(“。”)。getCanonicalPath()不起作用。返回工作目录。

To get the script directory

获取脚本目录

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

To get the script file path

获取脚本文件路径

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

#3


This makes sense if you are running the Groovy code as a script, otherwise the whole idea gets a little confusing, IMO. The workaround is here: https://issues.apache.org/jira/browse/GROOVY-1642

如果您将Groovy代码作为脚本运行,这是有道理的,否则整个想法会有点混乱,IMO。解决方法如下:https://issues.apache.org/jira/browse/GROOVY-1642

Basically this involves changing startGroovy.sh to pass in the location of the Groovy script as an environment variable.

基本上这涉及更改startGroovy.sh以将Groovy脚本的位置作为环境变量传递。

#4


For gradle user

对于gradle用户

I have same issue when I'm starting to work with gradle. I want to compile my thrift by remote thrift compiler (custom by my company).

当我开始使用gradle时,我遇到同样的问题。我想通过远程thrift编译器编译我的thrift(由我的公司定制)。

Below is how I solved my issue:

以下是我解决问题的方法:

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/"
            }
        }
    }
}