To explain my problem in the simplest way, I’ll first show you a Groovy script that does work and then add some Gradle sauce which breaks it.
The working script:
class MyMixin {
String myname = 'max'
}
@Mixin(MyMixin)
class MyClass {}
MyClass c = new MyClass()
println 'hello ' + c.myname
This prints out “hello max” as expected. Now let’s replace that MyClass with a Gradle task class. For this we extend DefaultTask and use the @TaskAction annotation.
@Mixin(MyMixin)
class MyTask extends DefaultTask {
@TaskAction
void sayHello() {
println 'hello ' + myname
}
}
If we now install this Gradle task and execute it, we get the following runtime error:
Execution failed for task ':myproject:mytask'.
> MyTask.getMyname()Ljava/lang/String;
What’s funny is that this only happens with methods returning something. void methods execute just fine.
So the question is simple: how can I get my mixin to cooperate with my Gradle task? (FYI: it doesn’t seem to be related to inheritance; I checked that, but didn’t include it in the sample script to keep it simple. I have also tested with a runtime mixin instead of compile-time: the result is the same).
I posted the same question on the Gradle forum and according to the Gradle developers it is an incompatibility between Groovy’s
@Mixinand Gradle’s own meta-programming facilities. They suggest to use Gradle’s extra properties and/or extension objects instead.For reference: http://forums.gradle.org/gradle/topics/groovy_mixin_combined_with_gradle_defaulttask_results_in_runtime_error