Java gradle生成protobuf类,但显示编译错误

Java gradle生成protobuf类,但显示编译错误,java,gradle,protocol-buffers,Java,Gradle,Protocol Buffers,生成的protobuf类低于预期的生成源 但它引用了com.google.protobuf,例如下面的代码。我得到编译错误,说com.google.protobuf找不到 public static void registerAllExtensions( com.google.protobuf.ExtensionRegistryLite registry) { } 下面是我的build.gradle文件 plugins { id 'org.springframewo

生成的protobuf类低于预期的生成源

但它引用了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')

    }
}

工作梯度文件:

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')

    }
}


我认为com.google.protobuf.ExtensionRegistryLite类需要一个protobuf依赖项,试试com.google.protobu:protobuf-javai,我把它放在gradle文件中了吗?试试实现“org.springframework.boot:spring-boot-starter-web”下的dependencies部分别忘了包括这个版本,看这里,它工作得很好。谢谢你能在下面回答吗,所以我可以把它标记为答案?你能把你正在工作的gradle文件作为一个答案发布到下面来帮助任何遇到同样问题的人吗?我想你需要一个protobuf依赖于com.google.protobuf.ExtensionRegistryLite类试试com.google.protobu:protobuf-javao,我把它放在gradle文件中了吗?试试下面的dependencies部分实现“org.springframework.boot:springbootstarterweb”别忘了在这里包含一个非常有效的版本。谢谢你能回答下面的问题吗,这样我就可以把它标记为答案了?你能把你的工作档案作为答案贴在下面,帮助遇到同样问题的人吗?