Gradle Swagger CodeGen DefaultGenerator CodegenConfigurator 添加 Lombok

Gradle Swagger CodeGen DefaultGenerator CodegenConfigurator Add Lombok

我有招摇和以下build.gradle文件

buildscript {
    ext {
        springBootVersion = '2.1.8.RELEASE'
    }
    repositories {
        maven {
            url "https://mavenrepo.schwab.com/nexus/content/groups/public"
        }
        maven {
            url "https://mavenrepo.schwab.com/nexus/content/repositories/releases/"
        }
    }
    dependencies {
        classpath("io.swagger:swagger-codegen:2.4.7")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE"
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
    }
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

repositories {
    maven {
        url "https://mavenrepo.schwab.com/nexus/content/groups/public"
    }
}


import io.swagger.codegen.DefaultGenerator
import io.swagger.codegen.config.CodegenConfigurator

def swaggerInput = "${rootDir}/api/POMOrchestrator.v1.json"
def swaggerOutputDir = file('application/')


task generateApi {
    inputs.file(swaggerInput)
    outputs.dir(swaggerOutputDir)
    doLast {
        def config = new CodegenConfigurator()
        config.setInputSpec(swaggerInput)
        config.setOutputDir(swaggerOutputDir.path)
        config.setIgnoreFileOverride(swaggerOutputDir.path)
        config.setLang('spring')
        config.setAdditionalProperties([
                'invokerPackage': 'com.schwab.brokerage.party.onborading',
                'modelPackage'  : 'com.schwab.brokerage.party.onborading.models.swagger',
                'apiPackage'    : 'com.schwab.brokerage.party.onborading.api.inbound.rest.controller',
                'dateLibrary'   : 'java8',
                'useTags'       : 'true',  // Use tags for the naming
                'interfaceOnly' : 'true'   // Generating the Controller API interface and the models only

        ])
        config.setImportMappings([
                'hello': 'com.schwab.cat.onbording.model.Hello'
        ])
        new DefaultGenerator().opts(config.toClientOptInput()).generate()
    }
}

clean.doFirst {
    delete(swaggerOutputDir)
}

configurations {
    swagger
}

sourceSets {
    swagger {
        compileClasspath = configurations.swaggerCompile
        java {
            srcDir file("${project.buildDir.path}/swagger/src/main/java")
        }
    }
    main {
        compileClasspath += swagger.output
        runtimeClasspath += swagger.output
    }
    test {
        compileClasspath += swagger.output
        runtimeClasspath += swagger.output
    }
}

compileSwaggerJava.dependsOn generateApi
classes.dependsOn swaggerClasses
compileJava.dependsOn compileSwaggerJava

ext {
    springBootVersion = '2.1.8.RELEASE'

    swaggerVersion = '2.7.0'
    springCloudServicesVersion = '2.1.2.RELEASE'
    springCloudVersion = 'Greenwich.RC1'
    cucumberVersion = '2.3.1'

    jackson_version = '2.4.2'
    jersey_version = '1.18'
    jodatime_version = '2.3'
    junit_version = '4.8.1'
}

dependencies {
    swaggerCompile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
    swaggerCompile 'io.swagger:swagger-annotations:1.5.16'
    swaggerCompile 'com.squareup.okhttp:okhttp:2.7.5'
    swaggerCompile 'com.squareup.okhttp:logging-interceptor:2.7.5'
    swaggerCompile 'com.google.code.gson:gson:2.8.1'

    compile sourceSets.swagger.output

    compile "com.sun.jersey:jersey-client:$jersey_version"
    compile "com.sun.jersey.contribs:jersey-multipart:$jersey_version"
    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 "com.fasterxml.jackson.datatype:jackson-datatype-joda:2.1.5"
    compile "joda-time:joda-time:$jodatime_version"
    compile 'io.swagger:swagger-codegen:2.2.3'

    testCompile "junit:junit:$junit_version"

    runtime 'com.squareup.okhttp:okhttp:2.7.5'
    runtime 'com.squareup.okhttp:logging-interceptor:2.7.5'
    runtime 'com.google.code.gson:gson:2.8.1'
}

这在创建模型方面做得很好,只是它们没有 lombok 注释,为了我的目的,能够在顶部添加 @Data 和 @Builder 真的很有帮助。有没有办法告诉代码生成器添加这些(也许是设置)?

解决方案是使用 Mustaches(参见 https://github.com/spullara/mustache.java)。这里是添加了lombok注解的pojo mustache

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Getter;

/**
 * {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}}
 */{{#description}}
@ApiModel(description = "{{{description}}}"){{/description}}
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}{{>additionalModelTypeAnnotations}}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}{{^parent}}{{#hateoas}}extends RepresentationModel<{{classname}}> {{/hateoas}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#serializableModel}}
  private static final long serialVersionUID = 1L;

{{/serializableModel}}
  {{#vars}}
    {{#isEnum}}
    {{^isContainer}}
{{>enumClass}}
    {{/isContainer}}
    {{#isContainer}}
    {{#mostInnerItems}}
{{>enumClass}}
    {{/mostInnerItems}}
    {{/isContainer}}
    {{/isEnum}}
  {{#jackson}}
  @JsonProperty("{{baseName}}"){{#withXml}}
  @JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}"){{/withXml}}
  {{/jackson}}
  {{#gson}}
  @SerializedName("{{baseName}}")
  {{/gson}}
  {{#isContainer}}
  {{#useBeanValidation}}@Valid{{/useBeanValidation}}
  private {{>nullableDataType}} {{name}};
  {{/isContainer}}
  {{^isContainer}}
  {{#isDate}}
  @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
  {{/isDate}}
  {{#isDateTime}}
  @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
  {{/isDateTime}}
  private {{>nullableDataType}} {{name}};
  {{/isContainer}}

  {{/vars}}

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}