如何生成、编译、打包Gradle模块并让其他模块依赖OpenAPI生成模块
How to Make Other Modules Depend on Your Generated OpenAPI Module
First, let's adjust your api/build.gradle to integrate the generated sources directly into the existing api module, rather than generating a separate standalone module. This turns the api module into a standard Gradle subproject that other modules can easily depend on.
Here's the revised configuration:
plugins { id 'java' id "org.openapi.generator" version "5.0.0" id 'maven-publish' // Optional, if you want to publish to a Maven repo } // Set the coordinates your other modules will use to reference this module group = "com.example" version = "1.0" description = "Generated API Module" repositories { mavenCentral() } // Add generated sources to the main source set so Gradle compiles them alongside any custom code sourceSets { main { java { srcDirs += file("$buildDir/generated/src/main/java") } resources { srcDirs += file("$buildDir/generated/src/main/resources") } } } // Include dependencies required by the generated code (matches what the standalone generated module would use) dependencies { implementation 'io.swagger:swagger-annotations:1.6.2' implementation 'com.google.code.findbugs:jsr305:3.0.2' implementation 'javax.annotation:javax.annotation-api:1.3.2' implementation 'org.threeten:threetenbp:1.4.4' // For Java 8 date library support testImplementation group: 'junit', name: 'junit', version: '4.12' } // Ensure code generation runs before compilation compileJava.dependsOn "openApiGenerate" openApiGenerate { generatorName = "java" inputSpec = "$projectDir/src/main/openapi/spec.yaml".toString() outputDir = "$buildDir/generated" apiPackage = "com.example.api" invokerPackage = "com.example.api.invoker" modelPackage = "com.example.api.model" configOptions = [ dateLibrary: "java8", library : "native" ] // Remove groupId and id here—we've set these at the project level instead } // Optional: Configure publishing to local Maven repo for coordinate-based dependencies publishing { publications { mavenJava(MavenPublication) { from components.java } } }
How to Depend on the API Module
Option 1: Multi-Project Dependency (Simplest)
If your other modules are part of the same Gradle multi-project build, add this to their build.gradle:
dependencies { implementation project(":api") }
Option 2: Coordinate-Based Dependency (Like com.example:api:1.0)
If you want to use the full coordinate string (e.g., for modules outside the multi-project build):
- Publish the
apimodule to your local Maven repo by running:./gradlew api:publishToMavenLocal - Add
mavenLocal()to the repositories of the dependent module:repositories { mavenCentral() mavenLocal() } - Add the dependency using the coordinates:
dependencies { implementation 'com.example:api:1.0' }
Why This Works
- By integrating generated sources into the existing
apimodule, we avoid the complexity of managing a separate generated build within your project. - Explicitly adding required dependencies eliminates the need to copy dependencies from the generated module's build file.
- Setting
groupandversionat the project level ensures the module has the exact coordinates you want other modules to reference.
Notes
- When you run
./gradlew clean, generated sources will be deleted, butcompileJavaautomatically triggersopenApiGenerateto regenerate them on the next build. - Custom code in the
apimodule will coexist with generated code as long as package names don't conflict.
内容的提问来源于stack exchange,提问作者Nick Breen
相关产品推荐
相关产品推荐

