如何将生成的 proto 文件添加到其他模块的源集中?

huangapple go评论60阅读模式
英文:

How to add proto generated files in source sets of other modules?

问题

我有一个使用 Proto 生成 DTO 的多模块 Gradle 项目。根项目有两个不同的模块,inventoryinventory-api。*-api 模块是拥有所有 .proto 定义的模块。inventory 模块依赖于 inventory-api

以下是根 Gradle 文件:

plugins {
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}

subprojects {
    group = 'com.yasinbee.apifirst'
    version = '0.0.1-SNAPSHOT'

    apply plugin: 'idea'
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'java-library'

    repositories {
        jcenter()
    }

    dependencyManagement {
        imports {
            mavenBom("org.springframework.boot:spring-boot-dependencies:2.1.9.RELEASE")
        }
    }

    compileJava {
        sourceCompatibility = 11
        targetCompatibility = 11
    }
}

以下是 inventory 模块的 Gradle 文件:

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
}

dependencies {

    implementation project(':inventory-api')

    implementation "org.springframework.boot:spring-boot-starter-data-jpa"
    implementation "org.springframework.boot:spring-boot-starter-jdbc"
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.mapstruct:mapstruct:1.4.0.Final"

    annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.0.Final'


    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    runtimeOnly 'com.h2database:h2'

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

test {
    useJUnitPlatform()
}

还有一个拥有所有 Proto 相关内容的 inventory-api 模块的 Gradle 文件:

apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'com.google.protobuf'

dependencies {
    implementation 'com.google.protobuf:protobuf-java:3.11.0'
}

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


protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.10.1'
    }
}

我可以在 inventory-api 模块中使用生成的 Proto 文件。然而,当我试图从 inventory 模块访问生成的文件时,我得到了编译错误。

似乎生成的 Proto 文件未添加到 inventory 应用程序的源集中。如何将 *-api 模块的生成源文件添加到依赖于它们的其他模块中呢?

英文:

I have a multi-module gradle project using proto to generate the DTOs. The root project has two different modules, inventory, and inventory-api. The *-api module is the one that has all the .proto definitions. inventory has a dependency on inventory-api

Here is the root gradle file

plugins {
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}

subprojects {
    group = 'com.yasinbee.apifirst'
    version = '0.0.1-SNAPSHOT'

    apply plugin: 'idea'
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'java-library'

    repositories {
        jcenter()
    }

    dependencyManagement {
        imports {
            mavenBom("org.springframework.boot:spring-boot-dependencies:2.1.9.RELEASE")
        }
    }

    compileJava {
        sourceCompatibility = 11
        targetCompatibility = 11
    }
}

Here is the inventory gradle file

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
}

dependencies {

    implementation project (':inventory-api')

    implementation "org.springframework.boot:spring-boot-starter-data-jpa"
    implementation "org.springframework.boot:spring-boot-starter-jdbc"
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.mapstruct:mapstruct:1.4.0.Final"

    annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.0.Final'


    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    runtimeOnly 'com.h2database:h2'

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

test {
    useJUnitPlatform()
}

And the inventory-api gradle file that has all the proto stuff

apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'com.google.protobuf'

dependencies {
    implementation 'com.google.protobuf:protobuf-java:3.11.0'
}

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


protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.10.1'
    }
}

I am able to use the generated proto files within the inventory-api module. However, when I am trying to access the generated files from the inventory module, I am getting a compilation error.

It seems that the generated proto files are not added to the source sets of the inventory app. How can I add the generated source files of *-api modules into other modules that depends on them?

答案1

得分: 1

问题已通过将inventory-api模块的build.gradle文件更改解决,从

dependencies {
    implementation 'com.google.protobuf:protobuf-java:3.11.0'
}

更改为

dependencies {
    api 'com.google.protobuf:protobuf-java:3.11.0'
}
英文:

The issue was resolved by changing the build.gradle file for inventory-api module from

dependencies {
    implementation 'com.google.protobuf:protobuf-java:3.11.0'
}

to

dependencies {
    api 'com.google.protobuf:protobuf-java:3.11.0'
}

huangapple
  • 本文由 发表于 2020年10月11日 22:52:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64305382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定