Generate Code in and for Plugin

I am developing a plugin.
This plugin, makes use of a parser generated with protoc.
The protoc is executed with the com.google.protobuf plugin.

import com.google.protobuf.gradle.builtins
import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.protoc

plugins {
    id ("com.google.protobuf") version "0.8.19"
    id ("java")
}

sourceSets {
    this.named("main") {
        proto {
            setSrcDirs(listOf("Foo"))
        }
    }
}

dependencies {
    implementation("com.google.protobuf:protobuf-javalite:3.21.4")
}

protobuf {
    var configurator = this.protobuf
    configurator.protoc {
        artifact = "com.google.protobuf:protoc:3.21.4"
    }
    configurator.generatedFilesBaseDir = project.layout.
        projectDirectory.dir("build/generated").toString()

    configurator.generateProtoTasks {
        this.all().forEach { task ->
            task.builtins {
                java { }
            }
        }
    }
}

This works generates the Foo.java source for the parser in my main project but I want to use the parser in a custom plugin.
Is there a recommended way to do this?

I suspect this would be a fairly common thing e.g. antlr, but I have not found a good example yet.

Make another project, in that project generate the protoc sources and build them.
Then use that project both, in your plugin project and your production project.

I will leave the original build.gradle.kts alone.
Here is a version of this script which works.

import com.google.protobuf.gradle.builtins
import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.protoc

/**
 * https://docs.gradle.org/current/userguide/custom_plugins.html#sec:precompiled_plugins
 */
plugins {
    `kotlin-dsl`
    id("org.jetbrains.kotlin.jvm") version "1.6.21"
    id ("com.google.protobuf") version "0.8.19"
}

dependencies {
    implementation(platform(project(":plugins-platform")))

    implementation(project(":base-plugins"))
    implementation(project(":java-plugins"))

    implementation("com.google.protobuf:protobuf-java:3.21.4")
    implementation("com.google.protobuf:protobuf-java-util:3.21.4")
}

sourceSets {
    named("main") {
        java {
            srcDirs("build/protoc/main/java")
        }
    }
}

tasks {
    named<JavaCompile>("compileJava") {
        dependsOn("generateProto")
    }
}

protobuf {
    var configurator = this.protobuf
    configurator.protoc {
        artifact = "com.google.protobuf:protoc:3.21.4"
    }
    configurator.generatedFilesBaseDir = project.layout
        .projectDirectory.dir("build/protoc").toString()

    configurator.generateProtoTasks {
        this.all().forEach { task ->
            task.builtins {
                java { }
            }
        }
    }
}

Some of the issues were replated to ‘com.google.protobuf’.
One big issue was related to not having a plugin generator, here I choose kotlin-dsl.
I also, moved the *.proto file into the project rather than trying to build a path to it.
There are some other details but those are the main points.

I created a plugin sub-project as outlined here…