我正在尝试使用gradle
编写kotlin
插件。它应该像这样工作。在提供配置时
apply plugin: 'appenv'
app {
srcLink ""
downloadPath ""
}
应该允许从srcLink
下载文件并使用任务downloadPath
存储在downloadAll
我创建了扩展类
open class ApplicationEnvironmentExtension {
val project: Any?
constructor(project: Any?) {
this.project = project
}
var srcLink: String = ""
var downloadPath: String = ""
}
创建插件类
class ApplicationEnvironmentPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.extensions.create("app", ApplicationEnvironmentExtension::class.java)
project.task(mapOf ("type" to Download::class.java), "downloadAll")
}
}
这是他的任务
class Download : DefaultTask() {
@TaskAction
fun download() {
val extension = this.project.extensions.getByName("app") as ApplicationEnvironmentExtension
DownloadUtils.download(extension.srcLink, extension.downloadPath)
}
}
我构建它并将其作为jar
依赖项添加到我的项目中。
要检查是否添加了任务,请运行gradle tasks
。但在此期间我收到了错误
Could not create an instance of type com.lapots.gradle.plugins.appenv.ApplicationEnvironmentExtension_Decorated.
在stacktrace中我得到了
Caused by: org.gradle.internal.reflect.ObjectInstantiationException: Could not create an instance of type com.lapots.gradle.plugins.appenv.ApplicationEnvironmentExtension_Decorated.
at org.gradle.internal.reflect.DirectInstantiator.newInstance(DirectInstantiator.java:53)
at org.gradle.api.internal.ClassGeneratorBackedInstantiator.newInstance(ClassGeneratorBackedInstantiator.java:36)
at org.gradle.api.internal.plugins.DefaultConvention.create(DefaultConvention.java:106)
at com.lapots.gradle.plugins.appenv.ApplicationEnvironmentPlugin.apply(ApplicationEnvironmentPlugin.kt:11)
at com.lapots.gradle.plugins.appenv.ApplicationEnvironmentPlugin.apply(ApplicationEnvironmentPlugin.kt:8)
at org.gradle.api.internal.plugins.ImperativeOnlyPluginApplicator.applyImperative(ImperativeOnlyPluginApplicator.java:35)
at org.gradle.api.internal.plugins.RuleBasedPluginApplicator.applyImperative(RuleBasedPluginApplicator.java:43)
at org.gradle.api.internal.plugins.DefaultPluginManager.doApply(DefaultPluginManager.java:144)
... 58 more
Caused by: java.lang.IllegalArgumentException: Could not find any public constructor for class com.lapots.gradle.plugins.appenv.ApplicationEnvironmentExtension_Decorated which accepts parameters [].
at org.gradle.internal.reflect.DirectInstantiator.newInstance(DirectInstantiator.java:44)
... 65 more
有什么问题?我应该为扩展提供一些附加构造函数吗?
答案 0 :(得分:4)
扩展需要一个默认(no-arg)构造函数。如果您确实需要为扩展程序中的值指定字段,则可以使用C = A[~A["fake_index_col"].isin(B["fake_index_col"])]
del C["fake_index_col"]
print C
Name Surname Age
1 Glenn Quagmire 35
2 Joe Swanson 38
3 Cleveland Brown 45
并在插件中创建扩展程序后对其进行分配。
在这种情况下,看起来你不需要你的lateinit var
字段,所以你可以摆脱它和构造函数。否则,如果您确实需要该字段,仍然会删除构造函数,但是您需要使project
变量的默认值为project
。
答案 1 :(得分:0)
我不确定ApplicationEnvironmentExtension
在构建过程中会发生什么,因此我将假定它是Gradle Project
的一个实例。试试这个
project.extensions.create("app", ApplicationEnvironmentExtension::class.java, project)
插件在调用Project.create()
时需要传递构造函数参数。如果它是Gradle Project
的实例,则ApplicationEnvironmentExtension.project
的类型应为Project
,而不是模糊的类型Any
。