How to add project dependency from plugin

plugins

(Andrey Makeev) #1

Hi,

I’m working on a gradle plugin and at some point i need to add some dependencies to active project: an artifact from maven and a local lib project.
The problem is that method project.dependencies.add() doesn’t work for projects. And project.dependencies.project() requires Map as an argument (wasn’t able to find what exactly should i put in that Map). So my question is: how can i add project by name specified in settings.gradle ?

Here is an example:

class LocalLibsPlugin implements Plugin , DependencyResolutionListener {
    Project project

    @Override
    void apply(Project project) {
        this.project = project
        project.gradle.addListener(this)
    }

    @Override
    void beforeResolve(ResolvableDependencies resolvableDependencies) {
        def compileDeps = project.getConfigurations().getByName("compile").getDependencies()
        println "adding maven lib:" // this one works fine:
       compileDeps.add(project.getDependencies().create("org:myFirstLib:2.1.3", { artifact { name = "myFirstLib-release"; type = 'aar' }}))
        println "adding local project(\":mySecondLib\")"  // this one doesn't:
        project.dependencies.add 'compile', project.dependencies.project(':mySecondLib') 
        project.gradle.removeListener(this)
    }

    @Override
    void afterResolve(ResolvableDependencies resolvableDependencies) {}
}

Thanks in advance!


(James Justinic) #2

The map notation for project dependencies can be found in the documentation for DependencyHandler.

The link directly to the section didn’t seem to work at time of posting, so here’s the full address:
http://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html#N1615D