英文:
How to add proto generated files in source sets of other modules?
问题
我有一个使用 Proto 生成 DTO 的多模块 Gradle 项目。根项目有两个不同的模块,inventory
和 inventory-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'
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论