How do you get the path of the running script in groovy?

后端 未结 6 1700
悲哀的现实
悲哀的现实 2020-12-02 11:54

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. Wh

相关标签:
6条回答
  • 2020-12-02 12:24

    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

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

    0 讨论(0)
  • 2020-12-02 12:24

    One more solution. It works perfect even you run the script using GrovyConsole

    File getScriptFile(){
        new File(this.class.classLoader.getResourceLoader().loadGroovySource(this.class.name).toURI())
    }
    
    println getScriptFile()
    
    0 讨论(0)
  • 2020-12-02 12:25

    workaround: for us it was running in an ANT environment and storing some location parent (knowing the subpath) in the Java environment properties (System.setProperty( "dirAncestor", "/foo" )) we could access the dir ancestor via Groovy's properties.get('dirAncestor').
    maybe this will help for some scenarios mentioned here.

    0 讨论(0)
  • 2020-12-02 12:38

    For gradle user

    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).

    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/"
                }
            }
        }
    }
    
    0 讨论(0)
  • 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:

    import groovy.transform.SourceURI
    import java.nio.file.Path
    import java.nio.file.Paths
    
    @SourceURI
    URI sourceUri
    
    Path scriptLocation = Paths.get(sourceUri)
    

    Note that this will only work 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. Certain Groovy runtimes such as groovyshell and nextflow will return a data: URI, which will not typically match an installed FileSystemProvider.

    0 讨论(0)
  • 2020-12-02 12:47

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

    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
    
    0 讨论(0)
提交回复
热议问题