How can I access gradle home directory?

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?

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.

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.

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

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(BasicScript.java:70)

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

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)

at groovy.lang.MetaClassImpl.invokeMissingProperty(MetaClassImpl.java:745)

at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1589)

at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3308)

at groovy.lang.GroovyObjectSupport.getProperty(GroovyObjectSupport.java:36)

at groovy.lang.Script.getProperty(Script.java:56)

at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:47)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)

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