2016-03-24 12 views

答えて

6

現在のスクリプト名は、次のように取得できます。

def scriptName = this.class.getName() 
println "Script FQCN : " + scriptName 

これは、そのパッケージ名に(クラス以外の何ものでもない)スクリプトの名前をプリントアウトします - FQCN(完全修飾クラス名)。

あなただけのスクリプト名およびNOTパッケージをしたい場合、あなたは

println "Script Simple Name : " + this.class.getSimpleName() 
1

を使用することができ、私はサポートクラスでのスクリプトの多くのためにこれを行うので、私のためにitsraghzの答えdoesntの仕事。

this.classまたはgetClass()をスクリプトによって呼び出されるサポートクラスから使用すると、スクリプトの名前ではなくサポートクラスの名前が取得されます。私が使用するたびにスクリプトの名前を得るには:

// Get the executed script as a (java) File 
File scriptFile = new File(getClass().protectionDomain.codeSource.location.path) 

// This holds the file name like "myscript.groovy" 
def scriptName = scriptFile.getName() 

// strip the extension to get "myscript" 
def withoutExt = scriptName.take(scriptName.lastIndexOf('.')) 
関連する問題