Skip to content

Instantly share code, notes, and snippets.

@steam0
Created July 4, 2017 11:17
Show Gist options
  • Save steam0/4fa11ad0660d33192ec01cd84050f226 to your computer and use it in GitHub Desktop.
Save steam0/4fa11ad0660d33192ec01cd84050f226 to your computer and use it in GitHub Desktop.
Gradle plugin workaround
/* Swagger (https://www.jcore.com/2017/05/22/automatically-generating-api-using-swagger-and-gradle/)*/
import io.swagger.codegen.config.CodegenConfigurator
import io.swagger.codegen.DefaultGenerator
def swaggerTargetFolder = 'build/generated-sources'
def generateFilesFromSwagger(swaggerSourceFile, packageName, swaggerTargetFolder) {
def config = new CodegenConfigurator()
config.setInputSpec("$swaggerSourceFile")
config.setOutputDir("$projectDir")
config.setLang('spring')
config.setAdditionalProperties([
'interfaceOnly' : 'true',
'apiPackage' : packageName,
'modelPackage' : packageName,
'sourceFolder' : swaggerTargetFolder,
'dateLibrary' : 'java8'
])
new DefaultGenerator().opts(config.toClientOptInput()).generate()
}
task swagger2java {
doLast {
// Order
generateFilesFromSwagger("file:///$projectDir/src/main/resources/swagger-spec/order.json", 'org.entur.order', swaggerTargetFolder)
// Payment
generateFilesFromSwagger("file:///$projectDir/src/main/resources/swagger-spec/payment.json", 'org.entur.payment', swaggerTargetFolder)
// Offers
// generateFilesFromSwagger("file:///$projectDir/src/main/resources/swagger-spec/offers.json", 'org.entur.offers', swaggerTargetFolder)
}
}
// Supporting gradle clean
clean.doFirst {
delete("${projectDir}/$swaggerTargetFolder")
}
swagger2java.dependsOn wsdl2java // NB! wsdl2java cleans generated sources directory, must happen before swagger gen
compileJava.dependsOn swagger2java
@dhafirnz
Copy link

dhafirnz commented Dec 8, 2017

Thanks for posting this configuration, it is what I wanted to achieve. However, it is creating he following error when I run Gradle build:

Could not get unknown property 'clean' for root project 'springboot-service-stencil' of type org.gradle.api.Project.

What I am missing here and why it doesn't recognize 'clean' ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment