Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to compile python protobuf using gradle on windows?

I am trying to use gradle to compile proto files into python, a task that appears trivial for java, but for some reason does not work for python using anything I've tried so far.

previously I've compiled .proto files into java using this tutorial: https://github.com/google/protobuf-gradle-plugin but for some reason there seems to be no similar solution for compiling python. I can still compile it manually using the CLI, but I would really like to create a solution which does not requite doing anything manually, nor the installation of protoc on the user's computer. I have found a lead in this thread: https://github.com/google/protobuf-gradle-plugin/issues/52 but it does not seem to be working for me.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.9'
    }
}

plugins {
    id 'java'
    id "com.google.protobuf" version "0.8.8"
    id 'application'
}

group 'foo.bar'
mainClassName = 'some.class.name'
version '1.0.2'

apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'


repositories {
    mavenCentral()
}

dependencies {
    compile "com.google.protobuf:protobuf-java:3.6.0"
    compile("io.grpc:grpc-netty:1.7.0")
    compile("io.grpc:grpc-protobuf:1.7.0")
    compile("io.grpc:grpc-stub:1.7.0")
}

buildDir = "$rootProject.buildDir"

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.6.0"
    }
    plugins {
        grpc_java {
            artifact = "io.grpc:protoc-gen-grpc-java:1.17.1"
        }
        grpc_python {
            path = "python -m grpc_tools.protoc"
        }
    }


    generateProtoTasks {
        all()*.builtins {
            java {}
            python {}
        }
        all()*.plugins {
            grpc_java {
                outputSubDir = "java"
            }
            grpc_python {
                outputSubDir = "python"
            }
        }
    }
    generatedFilesBaseDir = "$buildDir/generated/src"
}
like image 247
Lex Avatar asked Oct 22 '25 09:10

Lex


1 Answers

There is a way, in later 5.8++ version of gradle using Kotlin language (so build.gradle.kts) If using groovy slight modification is needed.

this works for us

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:$protobufVersion" }
    plugins {
        id("grpc") { artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion" }
        id("grpckt") { artifact = "io.grpc:protoc-gen-grpc-kotlin:$grpcKotlinVersion:jdk7@jar" }
    }
    generateProtoTasks {
        all().forEach {
            it.plugins {
                id("grpc")
                id("grpckt")
            }
            it.builtins {
                id("kotlin")
                id("python")
            }
        }
}
like image 168
Sam Marvasti Avatar answered Oct 27 '25 03:10

Sam Marvasti