使用Gradle将Java类从一个招摇过市的定义生成到一个独立的JAR中

使用Gradle将Java类从一个招摇过市的定义生成到一个独立的JAR中,gradle,swagger-codegen,Gradle,Swagger Codegen,我对Gradle和Swagger的代码生成器插件(具体来说是从Swagger的网站链接的插件)都是相当陌生的,所以我不确定我的问题是一般的Gradle还是那个特定的插件 我创建了一个简单的多模块Spring Boot应用程序(但我使用Spring Boot甚至Spring这一事实并不重要)。它是一个控制台应用程序;i、 它不会启动Web服务器。事实上,它实际上是一个REST客户机在使用其他人的接口 该应用程序由四个模块组成:spc-parent(这只是其余模块的一个信封),其中包含spc-boo

我对Gradle和Swagger的代码生成器插件(具体来说是从Swagger的网站链接的插件)都是相当陌生的,所以我不确定我的问题是一般的Gradle还是那个特定的插件

我创建了一个简单的多模块Spring Boot应用程序(但我使用Spring Boot甚至Spring这一事实并不重要)。它是一个控制台应用程序;i、 它不会启动Web服务器。事实上,它实际上是一个REST客户机在使用其他人的接口

该应用程序由四个模块组成:
spc-parent
(这只是其余模块的一个信封),其中包含
spc-boot
spc-service
spc-integration-model
Spc启动
只包含应用程序的起点,
Spc服务
现在包含一个Spring服务,
Spc集成模型
包含使用REST接口所需的类。生成的结构将更加复杂,但我已经尝试创建了一种最小的示例

问题在于
spc集成模型
模块中。它包含一个源文件,
petstore.json
,以及一个从中复制的
build.gradle
。实际上有两个问题(但它们可能有相同的根本原因)

  • 第一次运行
    gradle build
    (从
    spc parent
    )时,它会失败。Java源代码是从
    petstore.json
    生成的,但它们不会被编译,这就是
    spc服务中的服务看不到所需类的原因。但是,第二次运行
    gradlebuild
    可以解决这个问题(生成的Java源代码被编译,这也使得编译
    spc服务成为可能)
  • 创建的
    spc集成模型的JAR
    除了清单之外,从不包含任何内容
  • 我的目标是说服Gradle在第一次构建时立即编译生成的类,并将它们放入JAR中

    现在来看一些具体的梯度任务。最有趣的是spc集成模型的build.gradle:

    plugins {
        id 'org.detoeuf.swagger-codegen' version '1.7.4'
        id 'java'
    }
    
    apply plugin: 'org.detoeuf.swagger-codegen'
    
    repositories {
        mavenCentral()
        jcenter()
    }
    
    swagger {
        inputSpec = 'http://petstore.swagger.io/v2/swagger.json'
        outputDir = file('build/swagger')
        lang = 'java'
    
        additionalProperties = [
                'apiPackage' : 'ondra.spc.integration.client.api',
                'dateLibrary' : 'java8',
                'hideGenerationTimestamp': 'true',
                'invokerPackage' : 'ondra.spc.integration.client',
                'library' : 'resttemplate',
                'modelNameSuffix' : 'Dto',
                'modelPackage' : 'ondra.spc.integration.client.model'
        ]
        importMappings = [
                'Dog': 'io.swagger.petstore.client.model.Dog'
        ]
    }
    
    sourceSets {
        swagger {
            java {
                srcDir file("${project.buildDir.path}/swagger/src/main/java")
            }
        }
    }
    
    classes.dependsOn('swagger')
    
    ext {
        spring_boot_version = springBootVersion
        jackson_version = jacksonVersion
        junit_version = jUnitVersion
        swagger_annotations_version = swaggerAnnotationsVersion
        swagger_codegen_version = swaggerCodegenVersion
    }
    
    dependencies {
        swaggerCompile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version"
        swaggerCompile "io.swagger:swagger-annotations:$swagger_annotations_version"
    
        compile sourceSets.swagger.output
    
        compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
        compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
        compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
        compile "io.swagger:swagger-codegen:$swagger_codegen_version"
    
        testCompile "junit:junit:$junit_version"
    }
    
    (现在我正在阅读我的问题,我看到实际上没有使用本地版本的petstore.json,而是使用了在线版本,但让我们把它放在一边。)

    剩下的应该很简单<代码>spc服务

    dependencies {
        compile "org.springframework:spring-context:$springVersion"
        compile "org.springframework:spring-web:$springVersion"
    
        compile project (":spc-integration-model")
    }
    
    spc启动

    dependencies {
        compile "org.springframework.boot:spring-boot:$springBootVersion"
        compile "org.springframework.boot:spring-boot-autoconfigure:$springBootVersion"
    
        compile "org.springframework:spring-web:$springVersion"
    
        runtime "org.hibernate.validator:hibernate-validator:$hibernateVersion"
    
        runtime "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    
        compile project (":spc-service")
    
        testCompile("org.junit.jupiter:junit-jupiter-api:$jUnitVersion")
        testRuntime("org.junit.jupiter:junit-jupiter-engine:$jUnitVersion")
    }
    
    test {
        useJUnitPlatform()
    }
    
    spc父级

    subprojects {
        apply plugin: 'java'
    
        group 'ondra'
        version '1.0-SNAPSHOT'
    
        buildscript {
            ext {
                hibernateVersion = '6.0.9.Final'
                jacksonVersion = '2.9.4'
                springBootVersion = '2.0.0.RELEASE'
                springVersion = '5.0.5.RELEASE'
                swaggerAnnotationsVersion = '1.5.16'
                swaggerCodegenVersion = '2.2.3'
    
                jUnitVersion = '5.1.1'
            }
            repositories {
                mavenCentral()
            }
        }
    
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    
        repositories {
            mavenCentral()
        }
    
    }
    
    和spc父项的设置。梯度:

    rootProject.name = 'spc-parent'
    include 'spc-boot'
    include 'spc-service'
    include 'spc-integration-model'
    
    我还将整个应用程序放在一个归档中: