Skip to content

Instantly share code, notes, and snippets.

@darylteo
Created March 15, 2017 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darylteo/5ac64d77ad7bc4875de2e9901dbae576 to your computer and use it in GitHub Desktop.
Save darylteo/5ac64d77ad7bc4875de2e9901dbae576 to your computer and use it in GitHub Desktop.
JavaExec Swagger-CodeGen in Gradle
configurations {
swagger
}
repositories {
mavenCentral()
}
dependencies {
swagger 'io.swagger:swagger-codegen-cli:2.2.0'
}
jaxrsAnalyzerDoc {
// custom task that analyzes jaxrs api and generates swagger definition json.
// output of this task is used in the swagger-codegen task below.
}
tasks.addRule('Pattern: swagger-<language>: Generates api library in the given language') { taskName ->
if (taskName.startsWith('swagger-')) {
def parts = taskName.split('-', 2)
def language = parts[1]
def outputDir = "${project.buildDir}/swagger/${language}/"
def inputFile = jaxrsAnalyzerDoc.outputs.files[0]
project.task(taskName, type: JavaExec) {
main = '-jar'
inputs.file(inputFile)
outputs.dir(outputDir)
dependsOn(jaxrsAnalyzerDoc)
args = [
configurations.swagger[0],
'generate',
'-l', parts[1],
'-i', inputFile,
'-o', outputDir,
'--invoker-package', 'Listcorp/Api',
'--api-package', 'Client',
'--model-package', 'Models',
'--additional-properties', 'usePromises=true,projectDescription=Listcorp Api',
]
doLast {
fileTree(outputDir, {
include '**/*.js'
exclude '**/*.spec.js'
}).each({ file ->
def name = file.name.substring(0, file.name.length() - 3)
name = name != 'index' ? name : 'Api'
file.write(file.text
.replaceAll(/([^\.])exports/, '$1' + name)
)
})
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment