How can I access gradle home directory?

(kiran_anantha) #1

In my GRADLE_HOME/init.d, I have some init scripts. One of them needs to use a groovy script also located in GRADLE_HOME/init.d.

I tried this

GroovyScriptEngine gse = new GroovyScriptEngine(gradle.gradleHomeDir + “/init.d/”)

Class UtilClass = gse.loadScriptByName(“some.groovy”)

But it looks like gradle.gradleHomeDir is not available in init scripts (I get a property missing exception).

I cannot use a environment variable

GroovyScriptEngine gse = new GroovyScriptEngine(System.env.‘GRADLE_HOME’ + “/init.d/”)

When I use the STS plugin on a Mac, it doesn’t workif I use env variables. Something to do with the way Mac launches the apps (it does not source the .bashrc files).

Any other way I can refer to the groovy script from my init scripts?

(Luke Daley) #2

You can get a handle on a file object for the init script, so you can load the file relatively that way…

def parentFile = initscript.sourceFile.parentFile

I didn’t test that, but it should work.

(kiran_anantha) #3

I tried this but it didn’t work. Is this what you meant?

@Field GroovyScriptEngine gse = new GroovyScriptEngine(gradle.initscript.sourceFile.parentFile)

and this

@Field GroovyScriptEngine gse = new GroovyScriptEngine(initscript.sourceFile.parentFile)

But couldn’t get it to work. Thanks.

(Luke Daley) #4

Please provide details on what happened (i.e. how it didn’t work).

(kiran_anantha) #5

I get a property missing exception. But if I use a ‘def’ instead of the @Field then it works. So I can use the ‘def’ to solve this issue. Here is the exception (line 84 corresponds to the line with GroovyScriptEngine)

Caused by: java.lang.NullPointerException

at org.gradle.groovy.scripts.BasicScript.propertyMissing(

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(

at sun.reflect.DelegatingMethodAccessorImpl.invoke(

at java.lang.reflect.Method.invoke(

at org.codehaus.groovy.reflection.CachedMethod.invoke(

at groovy.lang.MetaClassImpl.invokeMissingProperty(

at groovy.lang.MetaClassImpl.getProperty(

at groovy.lang.MetaClassImpl.getProperty(

at groovy.lang.GroovyObjectSupport.getProperty(

at groovy.lang.Script.getProperty(

at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(

at some_6gt618fki1h5o6mqe6j6cfpcot.(C:\build-tools\gradle-1.2\init.d\some.gradle:84)