Using protobuf with Gradle (IntelliJ)

Viewed 11493

I am trying to generate code for a simple protobuf example using the build instructions given here. I have been trying for awhile but I am not able to see any auto generated code in my source root.

The following is my build.gradle file

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.protobuf'

group = 'io.ai.vivid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.5.1-1"
    }

    generateProtoTasks.generatedFilesBaseDir = 'generated-sources'

    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

dependencies {
    compile 'io.grpc:grpc-netty-shaded:1.14.0'
    compile 'io.grpc:grpc-protobuf:1.14.0'
    compile 'io.grpc:grpc-stub:1.14.0'
}

Also in my build.gradle file IntelliJ complains that it cannot resolve name protobuf

Things I have tried

  • Sync gradle tool in IntelliJ. This is the most prominent solution
    given in SO

  • Setting Build tools -> Gradle -> Runner -> Delelgate IDE build/run actions on gradle to true

  • Clean rebuilding of the gradle project.

From my understanding of the GitHub post, when you use the protobuf plugin, the stub will be automatically generated for you. What am I missing?

3 Answers

You've applied idea plugin, but you didn't configure it. You need to tell idea plugin where to include the generated source code.

protobuf {
    generatedFilesBaseDir = "$projectDir/src/generated"
}
idea {
    module {
        sourceDirs += file("${projectDir}/src/generated/main/java");
        sourceDirs += file("${projectDir}/src/generated/main/grpc");
    }
}

You can take a look at a full example of a buildfile here: build.gradle

In case anyone else ends up here problems getting IntelliJ to recognise the generated sources (Red highlight imports , classes etc). Beware of the intellisense file size limit. If your generated protobuf code exceeds the default setitng of 2500KB then the file is ignored.

Got Help -> Edit custom properties and add an entry appropriate for your case e.g. idea.max.intellisense.filesize=4000

Spent half a day faffing over different source set source folder, generated sources, and include / exclude directories. Turned out I just need to increase this value

Alternatively, you can use sourceSets:

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