You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何生成、编译、打包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):

  1. Publish the api module to your local Maven repo by running:
    ./gradlew api:publishToMavenLocal
    
  2. Add mavenLocal() to the repositories of the dependent module:
    repositories {
        mavenCentral()
        mavenLocal()
    }
    
  3. Add the dependency using the coordinates:
    dependencies {
        implementation 'com.example:api:1.0'
    }
    

Why This Works

  • By integrating generated sources into the existing api module, 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 group and version at 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, but compileJava automatically triggers openApiGenerate to regenerate them on the next build.
  • Custom code in the api module will coexist with generated code as long as package names don't conflict.

内容的提问来源于stack exchange,提问作者Nick Breen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:37:09