Created
March 15, 2017 02:33
-
-
Save darylteo/5ac64d77ad7bc4875de2e9901dbae576 to your computer and use it in GitHub Desktop.
JavaExec Swagger-CodeGen in Gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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