问题
I have a swagger and the following build.gradle file
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'
}
This does a fine job of creating the models only they do not have lombok annotations and for my purposes it would be really helpful to be able to add @Data and @Builder at the top. Is there a way to tell the code gen to add these (perhaps a setting)?
回答1:
The solution was to use Mustaches (see https://github.com/spullara/mustache.java). Here is the pojo mustache that added the lombok annotation.
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 ");
}
}
来源:https://stackoverflow.com/questions/61628717/gradle-swagger-codegen-defaultgenerator-codegenconfigurator-add-lombok