java - gradle生成protobuf类但显示编译错误

标签 java gradle protocol-buffers

生成的 protobuf 类如预期位于 generated-sources 下。

但它引用了 com.google.protobuf,例如下面的代码。我收到编译错误,指出未找到 com.google.protobuf。

  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistryLite registry) {
  }

下面是我的 build.gradle 文件。

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'com.google.protobuf' version '0.8.10'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

sourceSets {
    main {
        proto {
            srcDir 'src/main/proto'
        }
        java {
            // include self written and generated code
            srcDirs 'src/main/java', 'generated-sources/main/java'
        }
    }
    // remove the test configuration - at least in your example you don't have a special test proto file
}

protobuf {
    // Configure the protoc executable
    protoc {
        // Download from repositories
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }

    generateProtoTasks.generatedFilesBaseDir = 'generated-sources'

    generateProtoTasks {
        // all() returns the collection of all protoc tasks
        all().each { task ->
            // Here you can configure the task
        }

        // In addition to all(), you may get the task collection by various
        // criteria:

        // (Java only) returns tasks for a sourceSet
        ofSourceSet('main')

    }
}

我认为问题是 protobuf 库没有显示在我的 intellij 项目的外部库中。有没有办法让它与gradle一起工作?

最佳答案

工作 gradle 文件:

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'com.google.protobuf' version '0.8.10'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.google.protobuf:protobuf-java:3.11.1'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

sourceSets {
    main {
        proto {
            srcDir 'src/main/proto'
        }
        java {
            // include self written and generated code
            srcDirs 'src/main/java', 'generated-sources/main/java'
        }
    }
    // remove the test configuration - at least in your example you don't have a special test proto file
}

protobuf {
    // Configure the protoc executable
    protoc {
        // Download from repositories
        artifact = 'com.google.protobuf:protoc:3.6.0'
    }

    generateProtoTasks.generatedFilesBaseDir = 'generated-sources'

    generateProtoTasks {
        // all() returns the collection of all protoc tasks
        all().each { task ->
            // Here you can configure the task
        }

        // In addition to all(), you may get the task collection by various
        // criteria:

        // (Java only) returns tasks for a sourceSet
        ofSourceSet('main')

    }
}

关于java - gradle生成protobuf类但显示编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59430169/

相关文章:

java - 数组字符串中无法访问的语句

java - 如何记录 Gradle 构建任务的结果文件

protocol-buffers - Protobuf 模式的模式注册表

java - Hibernate 很难使用 MySQL 创建表

java - 实例化一个类的对象,该类是通过泛型给出的

java - 如何使用 Horton 架构注册表修复 : "Unrecognized token ' Unrecognized': was expecting ('true' , 'false' 或 'null' )"

hadoop - 使用Hive反序列化protobuf列

c++ - Protobuf 默认值

java - 如何将 pom.xml 添加到现有的 Eclipse 项目?

gradle - 如何根据子文件夹复制文件并重命名