我的插件中有一个需要mysql或postgres驱动程序的任务。
目前我用FooPlugin::apply
方法硬编码:
configuration.dependencies.add(project.dependencies.create('mysql:mysql-connector-java:5.1.34'))
但我想让用户选择他们的司机。 所以为此,我想从gradle构建脚本(build.gradle)获取所有依赖项,它们应用我的插件将动态注入这些依赖项。
已解决:添加一段代码
我试过了:
class FooPlugin implements Plugin<Project>{
@Override
void apply(Project project) {
project.afterEvaluate {
def configuration = project.configurations.create('bar')
configuration.extendsFrom(project.configurations.findByName('compile'))
…
}
}
}
如果您未进入project.afterEvaluate
阻止,则会引发错误:
Cannot change dependencies of configuration ':bar' after it has been resolved.
答案 0 :(得分:1)
我不确定你想要完成什么,所以我会猜几件事情。
看起来您尝试添加依赖项或根据添加的依赖项做出反应。我认为你可以通过resolutionStrategy
project.configurations {
compile.resolutionStrategy {
// adds a dependency to a project, always.
force 'commons-io:commons-io:2.5'
// loop through all the dependencies to modify before resolution
eachDependency { DependencyResolveDetails details ->
// here you can change details about a dependency or respond however needed
if (details.requested.group == 'mysql' && details.requested.name == 'mysql-connector-java') {
// for example we can force a specific version
details.useVersion '5.1.34'
}
// you could also add a if block for postgres if needed
}
}
}