Programmatically access script plugin references

I’m curious how to inspect a build to programmatically access script plugins defined in a build.gradle? This is useful for us trying to extract the build definition when setting up external programs to watch gradle. It looks like the IDEA plugin is figuring this out but searching through the codebase is giving me a giant headache, and I can’t find any real documentation for this.

build.gradle

...
apply from: "somescript.gradle"
...

how can I access this somescript.gradle reference from the org.gradle.api.Project object?

Try something like the following, possibly once you know the Plugin class implementation you might be able to get the script path

apply from: 'somescript.gradle'

plugins.all { plugin ->
   println plugin.toString()
   println plugin.class.name
} 

See PluginContainer

Yeah I already tried looking through the plugin container, but I did not get anything :frowning:

What’s the output from my suggestion?

I’m really looking for an answer here, I’ve already tested a significant amount of the API. Anyway, since I already have a test project, the output is:

for the simple build.gradle:

apply from: 'script.gradle'

plugins.all { plugin ->
  println plugin.toString()
  println plugin.class.name
}

output:

$ gradle --console=plain
org.gradle.api.plugins.HelpTasksPlugin@5b12bf18
org.gradle.api.plugins.HelpTasksPlugin
:help

I’m really looking for an answer here

If course you are, and I’m trying to help but don’t have a laptop in front of me. I was hoping that gradle would add some sort of ScriptPlugin to the plugin container. It seems that’s not the case so I’m not sure you can do it.

Perhaps if you explain a bit more your end goal, there might be a workaround

I need a task that can output all the gradle files I need to watch so I can trigger a re-evaluation of the project from an external program.

task whatever {
 print all the files (build.gradle, some-script.gradle, gradle.properties etc) that define the build
}

file watcher on output of whatever, if anything changes, re-evaluate the build.

It’s basically the cycle that the gradle-idea plugin uses on gradle builds: change to build definition -> resync the build. Since intellij is able to do it with script plugins, I’m assuming there is some programmatic access to this list of script plugin dependencies, I just can’t find them.

It’s a bit of a hack, but perhaps everything that’s not source and not a build result can be considered as a file to watch

Eg:

task whatever {
   doLast {
      def buildFiles = fileTree(rootDir).matching {
         exclude '**/build/**'
         exclude '**/src/**'
      }
      println "buildFiles = $buildFiles.files"
   }
}