1

我有两个 Gradle 子项目,它们基本上是哑 protobuf 容器。一个子项目需要从另一个导入定义,但我似乎无法弄清楚如何让 protobuf Gradle 插件正常工作(我对 Gradle 还很陌生)。

这是一个例子。

目录布局:

build.gradle
settings.gradle
gradle.properties
dependency/
|
 - build.gradle
 - src/main/proto/dependency.proto
 main/
|
 - build.gradle
 - src/main/proto/main.proto

build.gradle(顶级)

plugins {
  id "com.google.protobuf" version "0.8.12"
  id "java"
}

configure (allprojects) {
    apply plugin: 'java'
    apply plugin: 'com.google.protobuf'
}

设置.gradle

rootProject.name = 'sample'

include 'main'
include 'dependency'

gradle.properties

group=sample
version=0.1.0

依赖/build.gradle

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

dependencies {
    compile 'com.google.protobuf:protobuf-java:3.11.4'
    compile 'com.google.protobuf:protobuf-java-util:3.11.4'
}

依赖/src/main/proto/dependency.proto

syntax = "proto3";

package dependency;

option java_package = "dependency";

message DependencyRequest {
        string foo = 1;
}

message DependencyResponse {
        string bar = 1;
}

主/build.gradle

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.11.0"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.27.2'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

dependencies {
    compile 'com.google.protobuf:protobuf-java:3.11.4'
    compile 'com.google.protobuf:protobuf-java-util:3.11.4'

    protobuf project(':dependency')
}

主/src/main/proto/main.proto

syntax = "proto3";

package main;

option java_package = "main";

import "dependency/dependency.proto";

service MainService {

    rpc CallDependency(DependencyRequest) returns (DependencyResponse) {}

}

当我尝试构建时,我收到此错误:

$ ./gradlew :main:compileJava
> Task :main:generateProto FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':main:generateProto'.
> protoc: stdout: . stderr: dependency/dependency.proto: File not found.
  main.proto:7:1: Import "dependency/dependency.proto" was not found or had errors.
  main.proto:11:24: "DependencyRequest" is not defined.
  main.proto:11:52: "DependencyResponse" is not defined.

我阅读了插件的文档这个答案,但我无法让它工作。知道为什么 protobuf 任务没有获取 dependency.proto 吗?

4

1 回答 1

0

显然 protobuf 文件设置正确,但包名称不匹配导致 main.proto 无法找到 dependency.proto。我可以通过删除 protobuf 定义中的包名称来完成这项工作。

如果将来有人遇到此问题,请检查您的包名称和导入以确保它们是正确的。

于 2020-03-06T23:01:51.497 回答