# Generate Code in and for Plugin

**URL:** <https://discuss.gradle.org/t/generate-code-in-and-for-plugin/43551>\
**Category:** Help/Discuss\
**Created:** [August 1, 2022, 10:56pm UTC](https://discuss.gradle.org/t/generate-code-in-and-for-plugin/43551 "2022-08-01T22:56:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phreed](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@phreed](https://discuss.gradle.org/u/phreed)\
**Post date:** [August 1, 2022, 10:56pm UTC](https://discuss.gradle.org/t/generate-code-in-and-for-plugin/43551/1 "2022-08-01T22:56:27Z")

</div>

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.

```gradle
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.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [August 17, 2022, 11:38pm UTC](https://discuss.gradle.org/t/generate-code-in-and-for-plugin/43551/2 "2022-08-17T23:38:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![phreed](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@phreed](https://discuss.gradle.org/u/phreed)\
**Post date:** [August 19, 2022, 5:47pm UTC](https://discuss.gradle.org/t/generate-code-in-and-for-plugin/43551/3 "2022-08-19T17:47:19Z")

</div>

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

```gradle
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…

> **[GitHub - jjohannes/gradle-project-setup-howto: How to structure a growing Gradle...](https://github.com/jjohannes/gradle-project-setup-howto/tree/main)**
>
> main
