我在buildSrc
中有以下内容:
class MyClass {
def doSomething() {
final familyMembers = project.configurations['compile'].allDependencies.collect { dep ->
dep.name
}
}
但是当我尝试在build.gradle
中使用它时:
task 'do-something' << {
final myObject = new MyClass()
myObject.doSomething()
}
发出以下错误:
* What went wrong:
Execution failed for task ':my-project:do-something'.
> No such property: project for class: MyClass
如何在project
中看到MyClass
?
答案 0 :(得分:4)
您必须将项目作为参数传递给MyClass。
例如,声明构造函数和成员变量:
class MyClass {
private Project project
MyClass(Project project) {
this.project = project
}
def doSomething() {
final familyMembers = project.configurations['compile'].allDependencies.collect { dep ->
dep.name
}
}
然后从您的项目中使用它:
task 'do-something' << {
final myObject = new MyClass(project)
myObject.doSomething()
}