3

I have a gradle multi-project hierarchy:

root
-buildSrc
 -src/main/groovy/com/CustomTask.groovy
-subproj1
-subproj1
-build.gradle
-settings.gradle

I am able to invoke custom task - com.CustomTask only from root build.gradle file but I get - Unable to resolve class CustomTask at line import com.CustomTask when called from subproj1/build.gradle

import com.CustomTask

task customTask(type: CustomTask) {
        setModuleRoot(project.projectDir)
    }

Can anyone please suggest me how can I invoke a custom task defined under root level buildSrc from subproject's build.gradle file? (I am on gradle Gradle 7.1.1 version)

Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40
  • Seems to be working fine for me with Gradle 7.2 (removing the extra bracket in your example of cause). Not sure what could cause something like that for you... – Bjørn Vester Oct 05 '21 at 21:36
  • @BjørnVester sorry for that, yes it was an extra bracket. Thanks for pointing it out. But do you have similar structure and from sub-project's build.gradle your able to access buildSrc custom tasks? – Xavier DSouza Oct 06 '21 at 06:15
  • Yes, I used "gradle init" to create a new multi-project using the Groovy DSL and added the task with the `com` package like you (just extending DefaultTask). Then used it in one of the subprojects. Are you on an older version of Gradle? You could also try the newer lazy API with `tasks.register("customTask", CustomTask) {...}` and see if that makes a difference. – Bjørn Vester Oct 06 '21 at 07:25
  • @BjørnVester i am on Gradle 7.1.1 are you using import before calling `tasks.register` ? Can you post the snippet what you tried as an answer here so that i can verify it and if working, then great..!! and i can accept your answer as well. Thanks for your time – Xavier DSouza Oct 06 '21 at 07:31
  • @BjørnVester also one more point i missed is, actually subproject's `build.gradle` fails for me at the `import com.CustomTask` line itself before task is defined. – Xavier DSouza Oct 06 '21 at 07:37
  • Just to be sure, do you have a `buildSrc/build.gradle` file with the groovy plugin applied? Maybe you are not actually compiling the code for the custom task. – Bjørn Vester Oct 06 '21 at 07:55
  • @BjørnVester i do have an empty build.gradle as I read that buildSrc as java, groovy plugins automatically applied. – Xavier DSouza Oct 06 '21 at 09:56
  • I did it too. I Ran `gradle init`, added a class with a static variable to be printed, added an empty `build.gradle` under root, and then registered a task into that `build.gradle` to print the static variable. The import was success, and the variable was printed successfully. – chehsunliu Oct 08 '21 at 19:01

0 Answers0