I am trying to write a plugin that adds dependencies project.dependenciesaccording to the information collected in the plugin extension object. But that seems impossible.
Indeed, data from the extension object is available only in a new task or in closure project.afterEvaluate, but dependencies added in these places are ignored.
The following code tries to add the dependency to afterEvaluate, but the dependency is ignored:
apply plugin: MyPlugin
myplugin {
version '1.0'
}
class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create('myplugin', MyPluginExtension)
project.afterEvaluate {
def version = project.myplugin.version
project.dependencies.add("compile", "org.foo:bar:$version")
}
}
}
class MyPluginExtension {
def version
}
In the following code, dependency injection works, but I don't have access to the extension object:
apply plugin: MyPlugin
myplugin {
version '1.0'
}
class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create('myplugin', MyPluginExtension)
def version = project.myplugin.version
project.dependencies.add("compile", "org.foo:bar:$version")
}
}
class MyPluginExtension {
def version
}
Is there a solution?
source
share