'Replace placeholder in file with gradle
I have gradle task swagger-codegen
with the following configuration:
swaggerSources {
testProject {
inputFile = file("$buildDir/generated/input.json")
code {
language = 'csharp'
configFile = file('swaggergen-config.json')
}
}
}
The file swaggergen-config.json
contains:
{
"packageName": "Package.Test",
"packageVersion" : {version},
"netCoreProjectFile": true
}
How to properly replace {version}
placeholder with project.version
?
Solution 1:[1]
The swagger generator plugin looks for a file which makes this a little bit more complicated than just parsing the string and replacing the token.
A solution could be to treat the configuration file as a generated input to the generateSwaggerCode
task. This can be done via a copy task that copies your swaggergen-config.json
"template" and replaces the {version}
with token rootProject.version
using an ant ReplaceTokens
filter during copy. Note: You'll want to switch {version}
to the ant-style token format (e.g. @version@
), though.
The swaggerSources.code.configFile
closure could then be set to use the newly generated configuration.
This would be added to your build.gradle
:
task generateSwaggerGenConfig(type: Copy) {
from swaggergen-config.json
into $buildDir/generated/swaggergen-config.json
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens:['version:rootProject.version])
}
generateSwaggerCode.dependsOn generateSwaggerGenConfig
swaggerSources {
testProject {
inputFile = file("$buildDir/generated/input.json")
code {
language = 'csharp'
configFile = file("$buildDir/generated/swaggergen-config.json")
}
}
}
The generated swaggergen-config.json
would look like this
{
"packageName": "Package.Test",
"packageVersion" : @version@,
"netCoreProjectFile": true
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user3399000 |