google / protobuf-gradle-plugin

Protobuf Plugin for Gradle
Other
1.77k stars 273 forks source link

Option to disable/customize `sourceSets` for protoc plugins #759

Open safeuq-xflowpay opened 3 months ago

safeuq-xflowpay commented 3 months ago

Hi,

I am trying to migrate our codebase from Maven to Gradle. We are using the protoc-gen-jsonschema plugin to generate JSON Schema from the defined proto files. The configuration file looks like this:

protobuf {  
  protoc {
    artifact = "com.google.protobuf:protoc:3.18.1"
  }

  generateProtoTasks {
    ofSourceSet(sourceSets["main"].name).all {
      val jsonSchema by plugins.register("jsonschema") {
        outputSubDir = "jsonschema-res"

        option("prefix_schema_files_with_package")
        option("disallow_additional_properties")
        option("disallow_bigints_as_strings")
      }
  }
}

The above code seems to work, but the generated JSON schema files are included in the JAR generated by the jar task. Apparently, this plugin seems to add the protoc plugin's outputSubDir to sourceSets.java regardless of the content generated by the protoc plugin. So the workaround I had to do was to move all the generated JSON schemas into a separate directory and add it to the resources sourceSet.

protobuf {  
  generateProtoTasks {
    ofSourceSet(sourceSets["main"].name).all {
      // ...
      val outputBaseDir = file(outputBaseDir)
      val jsonSchemaDir = outputBaseDir.resolve(jsonSchema.outputSubDir)
      val jsonSchemaResDir = outputBaseDir.resolve("jsonschema")
      val jsonSchemaResSubDir = jsonSchemaResDir.resolve("jsonschema")
      sourceSets["main"].resources.srcDir(jsonSchemaResDir)

      doLast {
        copy { from(jsonSchemaDir); into(jsonSchemaResSubDir) }
        delete(jsonSchemaDir)
      }
      // ...
    }
  }
}

I was wondering if there is an option to disable/customize files added to the sourceSet on a per-plugin basis. I went through the plugin code and was not able to find anything that would allow me to do this. I was also wondering if there are any other better ways to achieve the same.

Thanks in advance.