Is it possible to create a task in buildSrc?

I am developing my first gradle plugin. It creates a task. I published to Gradle portal. In my the other project, I apply that plugin to one of module of the project, I can see that task gets created. But when I apply the plugin to my buildSrc’s build.gradle.kts, I could not see the task.
image

It looks we cannot create a task in buildSrc?

You can, it is just now shown in IntelliJ integration.
buildSrc is a bit special and when requesting the tasks for the parent project, Gradle does not deliver the buildSrc tasks to IntelliJ.
If you switch from buildSrc to an included build, it should work like you expect it to.

Thank you.

Is it possible to do it via command line? I tried ./gradlew :buildSrc:sayhi does not work.
build.gradle.kts (buildSrc)

tasks.register("sayhi") {
    println("hi")
}

I did try included build and can see the created task. But I have to use buildSrc for a particular reason. Sorry belos is a bit off topic but I have to explain.

I defined some classes that I want to access in all build.gradle.kts files including the root project’s build.gradle.kts. But there is a problem accessing those classes in plugins block when using included build. I followed this How to use Composite builds as a replacement of buildSrc in Gradle | by Yury | Bumble Tech | Medium to create an empty plugin and applied to modules so they can access classess in included build. But in plugins block I can’t access those classess. With bulidSrc it works just fine, and I don’t need to create the plugin and apply to all modules.
A snippet of code in root project’s build.gradle.kts:

plugins {
    kotlin("jvm") version "1.7.21"   

    // from buildSrc's classes, works fine.
    println(AndroidX.compose)

    // apply plugin from included build, cannot access the class in included build.
    id("empty-just-for-class-loader-plugin")
    println(AndroidX2.compose)
}

And my custom gradle plugin is to scan those files and process.

Just found some results…
In Gradle 8.0, this is fixed. Some changes to make buildSrc more like an included build by adammurdoch · Pull Request #22540 · gradle/gradle · GitHub
Not sure if the fix will make Intellij show the buildSrc tasks in Gradle panel??

Before Gradle 8.0, you can run buildSrc command: ./gradlew -p buildSrc taskName

Well, you found out most facts already, so I have not much to add.

Maybe that the plugin-applying trick might work if you apply the plugin in the settings script as the settings script class loader is parent of the build script class loaders so the classes should be available.

And that you should probably consider to neither use an included build nor buildSrc to define versions or plugins, but use a version catalog instead. :slight_smile: