How to add a task runtime dependency in a plugin?

I’m trying to write a plugin that adds a javadoc task that uses a custom doclet to a project.

This means I have to link against the doclet when configuring the task. When I do this directly in a project (not in a plugin) I do this:

javadocTask.options.docletPath = configurations.docletConfig.files.asType(List);

What I want is for when the user apply’s my plugin, it sets up these that it needs (so the user does not have to).

So the question: If you have a dependency needed by a task being created by a plugin, should it be defined in the plugin’s build.gradle file or should the plugin add it to the list of project dependencies?

If it should be in the plugin’s build.gradle, how would I access the dependnecy when setting up the task? Since there is no enclosing project object, there is no way to reference configurations.docletConfig

If I need to add it dynamically via code, how would I do that? So far I have something like this:

void appy(Project project) {
        project.getDependencies().add("docletConfig", ||||Something Here|||)
        // Set up Task

I’ve tried passing in the same list as I would in a build.gradle file (group: ‘’, name ‘doclet’, version:‘1’), that doesn’t work. I also tried doing it like the exact same as I would do it in build.gradle (with the addition of “project.”)

project.dependencies {
        docletConfig (group: '', name 'doclet', version:'1')

and that also doesn’t work. Complains that docletConfig is not a method :(.

Help? I feel like I’m missing something horribly obvious here, wouldn’t plugins having to add dependencies to a project be pretty critical functionality? But I haven’t been able to find any docs/examples on it :(.

I think your first example should work. An alternative is to try adding the dependency directly to the configuration.

def config = project.configurations.create(‘docletConfig’)


Using string syntax (“group:artifact:version”) instead of array syntax (group:‘group’,name:‘artifact’,version:‘version’) works!

Even Better, the original syntax i had works if you add ‘[]’ to it:

project.getDependencies().add("docletConfig", [group: '', name 'doclet', version:'1'])

It’s the small things that get ya.

This is a Groovy syntax issue. If a method takes a single argument, then you can ommit the square brackets around the map initializer. However, in this case, the method ‘add’ takes multiple arguments, and since commas separate method arguments, you are required to use the bracket notation.