3

If I follow the instruction in the grpc-java readme and I use maven, the protobuf generated files appear in the target directory and are subsequently in the classpath for me to extend etc. However, when I use gradle, the generated classes appear in the build directory and are absent from the classpath. I'm fairly new to gradle so I'm not really sure why it's behaving so differently.

My build.gradle file

apply plugin: 'java'
apply plugin: 'com.google.protobuf'

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

group 'co.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'io.grpc:grpc-netty-shaded:1.15.1'
    compile 'io.grpc:grpc-protobuf:1.15.1'
    compile 'io.grpc:grpc-stub:1.15.1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.5.1-1"
    }
    //noinspection GroovyAssignabilityCheck
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.15.1'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

1 Answer 1

3

It looks like for gradle (if you want to use the generated stubs/server interfaces) in the project where your proto files live in (i.e. the project is not just for generating jars and publishing them) then you'll need to add generatedFilesBaseDir to your build.gradle file:

protobuf {
    generatedFilesBaseDir = "$projectDir/src/main/java/generated"
    ...
}

Once you've done this, the stubs should be in your classpath.

public class SomeServer extends MyProtoClassGrpc.PDFExtractImplBase {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.