Hakanai
Hakanai

Reputation: 12680

How can I decompose a Gradle build into multiple files?

I'm writing a Gradle plugin which contains a collection of multiple chunks of independent configuration which will be applied to any project applying the plugin.

I want to keep the fragments very separate to discourage other people from adding unrelated logic to an existing place, and to improve visibility of what the plugin is actually configuring.

So I thought I could do this:

class CommonChecksPlugin implements Plugin<Project> {
  @Override
  void apply(Project project) {
    project.apply plugin: 'base'

    def resolveFile = { filename ->
      new URL(buildscript.sourceURI.toURL(), filename)
    }

    project.apply from: resolveFile('configuration1.gradle')
    project.apply from: resolveFile('configuration2.gradle')
  }
}

Example configurationN.gradle:

task 'checkSomething', type: CheckSomething

Problem is, this Java class CheckSomething cannot be resolved.

Is there a sensible way to do this other than just giving up and moving all the sub-scripts in as full Groovy classes? I'm reluctant to move them to classes, because I want to apply the same checks to the plugin project itself, and it seems difficult to apply them if they require compilation.

Upvotes: 0

Views: 220

Answers (1)

lance-java
lance-java

Reputation: 27994

The applied script has a different classloader to the plugin, it doesn't inherit the buildscript classloader so therefore the task isn't on the classpath. You can do the following:

project.ext.CheckSomething = CheckSomething
project.apply from: resolveFile('configuration1.gradle')

See https://discuss.gradle.org/t/buildscript-configurations-classpath-not-available-in-apply-from-scripts/1391

Upvotes: 1

Related Questions