grpc / grpc-java

The Java gRPC implementation. HTTP/2 based RPC
https://grpc.io/docs/languages/java/
Apache License 2.0
11.48k stars 3.86k forks source link

How to compile .proto files to project folder? #11711

Open AlexInCube opened 5 days ago

AlexInCube commented 5 days ago

I want to compile just the files into the root of the project, but I'm having trouble. I tried to make this code, but it gives an error. My proto files are above the Java project in the hierarchy, because the Java project is one of the projects in the monorepository.

Illegal char <:> at index 81: C:\webprojects\micro-keynadi\apps\eximia-auth\build\generated\source\proto\main\C:\webprojects\micro-keynadi\apps\eximia-auth\src\main\generatedProto
plugins {
    id 'java'
    id 'eclipse'
    id 'org.jetbrains.gradle.plugin.idea-ext' version '1.1.8'
    id 'com.google.protobuf' version '0.9.4'
}

sourceSets {
    main {
        proto {
            srcDir '../../proto'
        }
        java {
            srcDir "${projectDir}/src/main/generatedProto"
        }
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.25.5"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.68.1'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    outputSubDir = "${projectDir}/src/main/generatedProto"
                }
            }
            task.plugins {
                grpc {
                    outputSubDir = "${projectDir}/src/main/generatedProto"
                }
            }
        }
    }
}
ejona86 commented 4 days ago

outputSubDir can't move it to another part of the tree. It is a subdirectory. That's why there's the random broken C: in the middle of the string "...proto\main\C:\webprojects...". I suggest deleting the outputSubDir lines

AlexInCube commented 4 days ago

then how do i do the compilation to the folder i want? i have spent hours trying to figure it out but nothing works.

ejona86 commented 4 days ago

You can use a separate Sync or Copy task into the folder of your choosing. https://github.com/google/protobuf-gradle-plugin/issues/722 tracks letting it be configured.

kannanjgithub commented 4 days ago

The generateProtoTasks plugin uses the default outputSubDir of build/generated/source/proto/main under which it will create a "java" and "grpc" subdirs for the generated sources for the protos and the service respectively. So in your source sets you need to also change the paths for Java (for IDEs to pick up) from

    java {
        srcDir "${projectDir}/src/main/generatedProto"
    }

to

    java {
        srcDirs 'build/generated/source/proto/main/grpc'
        srcDirs 'build/generated/source/proto/main/java'
    }

Refer (example).