How to configure a project's buildscript through a custom plugin

Viewed 76

I want to make a custom plugin that is able to configure buildscript level logic to any project it is applied to. The problem is, the build script closure can only be declared once and atop the original file.

Example:

// build.gradle (project applying the custom plugin)
plugins {
    id 'my-custom-plugin'
}

// MyCustomPlugin.groovy
class MyCustomPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.buildscript {
           // repos and dependencies
        }
    }
}

Because, obviously, I get the error: cannot change dependencies of dependency configuration 'blahblah' after it has been resolved. (which I assume is the reason as to why the buildscript closure can only be defined first atop the script file anyways)

How can I achieve this? Is there any way to setup build script dependencies via a plugin? Or a way to have them be in the scope of the void apply(Project project) method to be used there?

I realize I'm likely misunderstanding how things work and would gladly accept any sort of answer.

EDIT (My answer)

I've figured out the answer although more context by someone more expert may be useful.

So, the project that owns the MyCustomPlugin.groovy can be setup such that the dependencies that you would normally put in the buildscript of the project applying the custom plugin now become the dependencies of that project via say an implementation configuration.

This makes it so they are in scope to be applied during the apply method to the project passed.

0 Answers
Related