Hi,
now i’m writting a custom plugin with kotlin, but when i try to receive a list of parameter pass by command line, there is an error like below:
PS D:\WORK\code\Java\AndroidDependencyAnalyzer> ./gradlew jycTest --url=123 --url=234
FAILURE: Build failed with an exception.
* What went wrong:
Index: 0
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
And this is my code:
abstract class TestTask : DefaultTask() {
@get:Input
@get:Option(option = "url", description = "set url of this")
@get:Optional
abstract val url: MutableList<String>
@TaskAction
fun action () {
url.forEach {
println(it)
}
}
}
class TestPlugins @Inject constructor() : Plugin<Project> {
override fun apply(target: Project) {
target.tasks.register("jycTest", TestTask::class.java)
}
}
May i ask where is the problem and how to fix this?
Please do not use that fancy toy for real work.
I’ve not yet seen any helpful or correct code it produced.
Just horribly wrong nonsense with totally misusing things, calling methods an APIs he just invented that never existed, and so on.
The only situation where it could be helpful is, when you could actually write the code yourself but are just too lazy, so that you can see and fix the non-sense it generated into the generated code.
In your orignal code, I don’t get “Index: 0” but “Cannot have abstract method TestTask.getUrl().”, so you probably miss vital information in your example.
But other than that, your original code was almost correct.
You just need to make the property var and have the Option annotation on the setter, not the getter.
So for example:
abstract class TestTask : DefaultTask() {
@get:Input
@set:Option(option = "url", description = "set url of this")
@get:Optional
abstract var urls: List<String>
@TaskAction
fun action() {
urls.forEach {
println(it)
}
}
}
class TestPlugins : Plugin<Project> {
override fun apply(target: Project) {
target.tasks.register("jycTest", TestTask::class.java)
}
}