Use multiple Gradle files

I have to following setup which is not usual or defined by Gradle. Usually this should be done via multi-project but I cannot afford it.

build.gradle
  \-- folder1
      \-- datafile1.gradle
           datafile2.gradle

When I apply a plugin (custom one) over build.gradle it is valid only for main file. Files under folder1 does’t seems to have access to objects defined by the plugin.

How can I import object defined in my plugin?
From my testing I found only exposed properties are visible but the rest is not. By this I mean classes. Trying to import something and I end up instantly with NoClassDefinitionException. Not sure how to expose (make visible) classes over all Gradle files.

My knowledge says:

  1. Loading the plugin should be done from buildscript{}
  2. Plugin is loaded from cache/repo
  3. Loading external files (datafile1.gradle and datafile2)
  4. Loading build.gradle
  5. DAG is built

I’ve read lot forums and discussions but nothing similar. Most of the topics suggests multi-project build.

Please correct me if I am wrong or such feature is still missing in Gradle.
Any help will be appreciated.

I’m assuming datafile1.gradle and datafile2.gradle are script plugins (i.e. apply from: 'folder1/datafile1.gradle')?

If this is the case then they each use their own classloader. The simplest solution is simply to define a buildscript { } block in each of them.

Yes, they are script plugins but controlled and used from plugin itself. Plugin can work without them but they are worthless without it.

Is there an approach to avoid second buildscript {} because initialization opens connection?

I’m not sure what you mean by “initialization opens connection”. Downloaded dependencies are cached if that’s what you are referring to. It shouldn’t incur a performance penalty.

By “initialization opens connection” I mean - opening new database connection. Perhaps I should have to re-work my design because when applied next step is to connect.