How to access rootProject from an applied buildscript?

Hi, All,

I am developing a set of gradle plugins for us to use inside the firm, in order to make it easy for my users to consume the plugins, I provide a buildscript.gradle:

def installDir = buildscript.sourceFile.parentFile.parentFile
dependencies {
    classpath files(
        [installDir, 'lib', 'my-plugins1.jar'].join(File.separator)
        [installDir, 'lib', 'my-plugins2.jar'].join(File.separator)
    )
}

So user only need to do apply this buildscript.gradle to consume it:

buildscript {
    project.ext.gradle_plugin_path = '/the/gradle/plugin/path'
    apply from: "${project.ext.gradle_plugin_path }/etc/buildinfra/buildscript.gradle", to: buildscript
}
  apply plugin: "plugin-provided-by-me''

The thing is the project.ext.gradle_plugin_path is also used in the implementation of my plugins, which requires user to do exactly above to define a variable named project.ext.gradle_plugin_path first ,and then apply the buildscript.gradle.

What I want to achieve is user only need to: buildscript {

apply from: “/the/gradle/plugin/path/etc/buildinfra/buildscript.gradle”, to: buildscript }

And inside the implementation of my buildscript.gradle, I could parse and store the value of gradle_plugin_path, which could be used late in my plugins - thus reduce the burden from our users.

But, it seems neither project nor rootProect is accessible from within buildscript.gradle, with an error like: > * What went wrong: > A problem occurred evaluating script. > > No such property: rootProject for class: org.gradle.api.internal.initialization.DefaultScriptHandler

How should I acheive this? it is not necessary to be rootProject, any global accessible variable that I can save the value and then retrieved would be fine. (I would only use systemproperties or environment variables as a last resort )

Thanks very much.

Change this:

apply from: “${project.ext.gradle_plugin_path }/etc/buildinfra/buildscript.gradle”, to: buildscript

To:

apply from: “${project.ext.gradle_plugin_path }/etc/buildinfra/buildscript.gradle”, to: project

Then you’ll have to wrap your buildscript.gradle content in ‘buildscript {}’.

That’s the only way to get access to the project object in your external script.