如何使用 Open API 3 gradle 插件

How to use the Open API 3 gradle plugin

我正在尝试为 gradle 使用 Open API 3 生成器插件。 添加行

就足够了
plugins {
    id "org.openapi.generator" version "5.0.1"
}

在项目的build.gradle中得到错误:

com.google.common.base.Suppliers$NonSerializableMemoizingSupplier cannot be cast to java.util.function.Supplier

我正在使用 gradle 6.4

我现在就是这样实现的。请注意这一行:

classpath("com.google.guava:guava:30.1-jre")

这就是问题的解决方法。

在项目build.gradle中添加:

buildscript {
    dependecies {
        classpath("com.google.guava:guava:30.1-jre")
}

plugins {
    id "org.openapi.generator" version "5.0.1"
}

task generateCode(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = "kotlin"
    inputSpec = sampleApiSpec
    outputDir = "$projectDir/generatedapi"

    groupId = "$project.group"
    id = "$project.name"
    version = "$project.version"
    apiPackage = "com.package.name.generatedapi.apis"
    invokerPackage = "com.package.name.generatedapi.apis.invoker"
    modelPackage = "com.package.name.generatedapi.apis.model"
    enablePostProcessFile = true
    skipOverwrite = false
    modelNamePrefix = "Raw"
    configOptions = [
            java8               : "true",
            dateLibrary         : "java8",
            serializationLibrary: "moshi",
            library             : "resttemplate",
            useBeanValidation   : "true",
            enableBuilderSupport: "true",
    ]
}

android.sourceSets.main.java.srcDirs += ['generatedapi']

那么你可以运行:

./gradlew generateCode

或者您可以将 generateCode 设置为构建任务的依赖项

请检查: https://github.com/GoogleCloudPlatform/artifact-registry-maven-tools/issues/27

编辑 更好:

buildscript {
    dependecies {
        classpath('org.openapitools:openapi-generator-gradle-plugin:5.0.1') {
            exclude group: 'com.google.guava'
        }
}

configurations {
    compile.exclude module: 'guava-jdk5'
}

因此无需覆盖番石榴库,只需排除生成器导入的库即可

https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-gradle-plugin