我对Gradle和Swagger代码生成器插件都很新(具体是从Swagger的网站链接的那个,即https://github.com/thebignet/swagger-codegen-gradle-plugin),所以我不确定我的问题是Gradle一般还是使用那个特定的插件 .

我已经创建了一个简单的多模块Spring Boot应用程序(但我使用Spring Boot甚至Spring的事实并不重要) . 这是一个控制台应用程序;即它不启动网络服务器 . 实际上,它实际上是一个消费其他人界面的REST客户端 .

该应用程序包含四个模块: spc-parent (其余部分只是一个信封),包含 spc-bootspc-servicespc-integration-model . Spc-boot 只包含应用程序的起点, spc-service 现在包含一个Spring服务, spc-integration-model 意味着包含使用REST接口所需的类 . 由此产生的结构会复杂得多,但我试图创建一个最小的例子 .

问题出在 spc-integration-model 模块中 . 它由一个源文件 petstore.json 和从https://github.com/thebignet/swagger-codegen-gradle-plugin复制的 build.gradle 组成(并且只是稍加修改) . 实际上有两个问题(但它们可能有相同的根本原因) .

  • 第一次运行 gradle build (来自 spc-parent )时,它失败了 . Java源代码是从 petstore.json 生成的,但它们不会被编译,这就是 spc-service 中的服务看不到所需类的原因 . 但是,第二次运行 gradle build 修复此问题(生成的Java源代码已编译,这使得编译 spc-service 成为可能) .

  • spc-integration-model 创建的JAR从不包含除Manife之外的任何内容 .

我的目标是说服Gradle在第一次构建期间立即编译生成的类,并将它们放入JAR中 .

现在进行一些具体的Gradle任务 . 最有趣的是 spc-integration-model 的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-service

dependencies {
    compile "org.springframework:spring-context:$springVersion"
    compile "org.springframework:spring-web:$springVersion"

    compile project (":spc-integration-model")
}

spc-boot

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-parent

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-parent 的settings.gradle:

rootProject.name = 'spc-parent'
include 'spc-boot'
include 'spc-service'
include 'spc-integration-model'

我还将整个应用程序放在一个存档中:https://drive.google.com/open?id=1cOYIcaxnhik548w0wEGswgD2g4udATdD