Changing data in OpenApi info block - gradle

I'm trying to change my OpenApi info block properties.
More specifically I'm trying to change the value of the version tag in my OpenApi programmatically.
For example every new build I want a new version number.
I have tried using placeholders and giving them values in the build.gradle but haven't got it working.
openapi:
openapi: 3.1.0
info:
title: Dummy Bookshop
summary: A fictitious API demonstrating the OpenAPI Specification's features
version: ${apiVersion}
description: A fictius description.
termsOfService: https://www.dummy-book.shop/tos
contact:
name: Bookshop Support team
url: https://www.dummy-book.shop/support
email: support#dummy-book.shop
license:
name: Apache 2.0
identifier: Apache-2.0
paths: {}
build.gradle:
ext {
apiVersion = '1.0.1'
}
Does anyone have any ideas on how to get this working or is there a plugin that does this?

Create a script in your build.gradle to replace the version with the gradle version. This is easily done using the groovy YamlSlurper.
task updateYaml() {
def fileDir = "$projectDir/src/main/resources/static/"
def fileName = "myOpenApi.yml"
def reader = new FileReader(fileDir + fileName)
def slurper = new YamlSlurper()
def builder = new YamlBuilder()
builder slurper.parse(reader)
builder.content.info.version = project.version
doLast {
File newOASFile = new File(fileDir, "finalSpec.yml")
newOASFile.write(builder.toString())
}
}
Once you have the task working, set your dependencies properly. This assumes that your generation task is called generateMyOpenApi
generateMyOpenApi.dependsOn updateYaml
processResources.dependsOn generateMyOpenApi

Related

How to specify templateDir in Swagger Codegen (Gradle) for OpenAPI 3.0?

Anyone have any idea how to specify a templateDir for swagger codegen v3? I have attached a snippet of my build.gradle below
Also the setTemplateDir does not do anything so I am guessing thats not an existing method.
My class path is io.swagger.codegen.v3:swagger-codegen-maven-plugin:3.0.27
task generateServer {
doLast {
def openAPI = new OpenAPIV3Parser().read(rootProject.swaggerFile.toString(), null, null)
def clientOpts = new ClientOptInput().openAPI(openAPI)
def codegenConfig = CodegenConfigLoader.forName('spring')
codegenConfig.setOutputDir(project.buildDir.toString())
// codegenConfig.setTemplateDir('test');
clientOpts.setConfig(codegenConfig)
def clientOps = new ClientOpts()
clientOps.setProperties([
'dateLibrary' : 'java8', // Date library to use
'useTags' : 'true', // Use tags for the naming
'interfaceOnly' : 'true' // Generating the Controller API interface and the models only
])
clientOpts.setOpts(clientOps)
def generator = new DefaultGenerator().opts(clientOpts)
generator.generate() // Executing the generation
}
}
I figured this out. I would need to create a configurator and set the template directory there
Also so you know, if a mustache file is missing, then it will be pulled from one of dependency jars
task generateServer {
doLast {
CodegenConfigurator codegenConfigurator = new CodegenConfigurator()
codegenConfigurator
.setTemplateDir(rootProject.templateDir.toString())
.setLang('spring')
.setOutputDir(project.buildDir.toString())
.setInputSpec(rootProject.swaggerFile.toString())
OpenAPI openAPI = new OpenAPIV3Parser().read(rootProject.swaggerFile.toString(), null, null)
ClientOptInput clientOptInput = codegenConfigurator.toClientOptInput().openAPI(openAPI)
DefaultGenerator generator = new DefaultGenerator().opts(clientOptInput)
generator.generate() // Executing the generation*/
}
}

Could not resolve all dependencies for configuration [custom configuration]

Please, I'm working on converting a gradle 2.1 project to 6.0, but I get this error.
Could not resolve all dependencies for configuration ':driver'.
> Cannot convert the provided notation to a File or URI: classesDirs.
The following types/formats are supported:
- A String or CharSequence path, for example 'src/main/java' or '/usr/include'.
- A String or CharSequence URI, for example 'file:/usr/include'.
- A File instance.
- A Path instance.
- A Directory instance.
- A RegularFile instance.
- A URI or URL instance.
When running
configurations.driver.each {File file ->
loader.addURL(file.toURL())
}
driver is a custom configuration define as
configurations {
driver
}
dependencies {
driver 'org.drizzle.jdbc:drizzle-jdbc:1.3'
}
Please any ideas how to fix?
Fixed by using
sourceSets.main.output.resourcesDir = sourceSets.main.java.outputDir
sourceSets.test.output.resourcesDir = sourceSets.test.java.outputDir
Instead of
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDirs
sourceSets.test.output.resourcesDir= sourceSets.test.output.classesDirs

how do i configure static versioning (digest) for the asset-pipeline (bertramlabs) in my spring boot 2 application?

spring boot version: 2.0.4.RELEASE
asset-pipeline version: 3.0.3
Hi
we're using this plugin, because we know it from our grails applications.
We liked it, because it has a simple configuration (for our requirements)
Now we're developing a spring boot application and we used this plugin too and we're (almost) happy with it.
But when we run the application in the development mode the assets don't have a digest like /assets/my-styles-b5d2d7380a49af2d7ca7943a9aa74f62s.css
How do i configure the plugin to create a digest for all our resources?
currently we're using this configuration:
assets {
minifyJs = true
minifyCss = true
enableSourceMaps = false
includes = ["application.js", "application.scss"]
}
And we're using thymeleaf for our templates:
<link th:href="#{/assets/application.css}" rel="stylesheet">
I found a solution...
when you use the asset-pipeline, you get a gradle task assetCompile.
when creating a .war file, you can add this gradle task and replace all the assets with the versioned files.
when you want to use the versioned files in your production mode you have to use this configuration (build.gradle)
assets {
minifyJs = true
minifyCss = true
skipNonDigests = true
packagePlugin = true
includes = ["application.js", "application.scss"]
}
...
war {
dependsOn 'assetCompile'
from( "${buildDir}/assets", {
into "/WEB-INF/classes/META-INF/assets"
})
baseName = '<your project>'
enabled = true
}
that's all.
When running the assetCompile task, a manifest.properties file is created. This file contains the mapping of the original filename and the versioned one.
This file is used by the application to find the correct resource, e.g. application.css=application-79a3c8a2f085ecefadgfca3cda6fe3d12.css
I created a plugin which enables the url replacement for assets with digest in the production mode:
Dependency
compile 'ch.itds.taglib:asset-pipeline-thymeleaf-taglib:1.0.0'
Configuration
#Configuration
public class ThymeleafConfig {
#Bean
public AssetDialect assetDialect() {
return new AssetDialect();
}
}
Usage
<html xmlns:asset="https://www.itds.ch/taglib/asset">
<script asset:src="#{/assets/main.js}"></script>
</html>
The asset:src="#{/assets/main.js}" will be replaced with src="/assets/main-DIGEST.js".
The replacement happens only if the developmentRuntime of the asset pipeline is disabled.
A little bit more details are available on my blog post: https://kobelnet.ch/Blog/2019/03/12/assetpipelinethymeleaftaglib

SonarQube - specify location of sonar.properties

I'm trying to deploy SonarQube on Kubernetes using configMaps.
The latest 7.1 image I use has a config in sonar.properties embedded in $SONARQUBE_HOME/conf/ . The directory is not empty and contain also a wrapper.conf file.
I would like to mount the configMap inside my container in a other location than /opt/sonar/conf/ and specify to sonarQube the new path to read the properties.
Is there a way to do that ? (environment variable ? JVM argument ? ...)
It is not recommended to modify this standard configuration in any way. But we can have a look at the SonarQube sourcecode. In this file you can find this code for reading the configuration file:
private static Properties loadPropertiesFile(File homeDir) {
Properties p = new Properties();
File propsFile = new File(homeDir, "conf/sonar.properties");
if (propsFile.exists()) {
...
} else {
LoggerFactory.getLogger(AppSettingsLoaderImpl.class).warn("Configuration file not found: {}", propsFile);
}
return p;
}
So the conf-path and filename is hard coded and you get a warning if the file does not exist. The home directory is found this way:
private static File detectHomeDir() {
try {
File appJar = new File(Class.forName("org.sonar.application.App").getProtectionDomain().getCodeSource().getLocation().toURI());
return appJar.getParentFile().getParentFile();
} catch (...) {
...
}
So this can also not be changed. The code above is used here:
#Override
public AppSettings load() {
Properties p = loadPropertiesFile(homeDir);
p.putAll(CommandLineParser.parseArguments(cliArguments));
p.setProperty(PATH_HOME.getKey(), homeDir.getAbsolutePath());
p = ConfigurationUtils.interpolateVariables(p, System.getenv());
....
}
This suggests that you can use commandline parameters or environment variables in order to change your settings.
For my problem, I defined environment variable to configure database settings in my Kubernetes deployment :
env:
- name: SONARQUBE_JDBC_URL
value: jdbc:sqlserver://mydb:1433;databaseName=sonarqube
- name: SONARQUBE_JDBC_USERNAME
value: sonarqube
- name: SONARQUBE_JDBC_PASSWORD
valueFrom:
secretKeyRef:
name: sonarsecret
key: dbpassword
I needed to use also ldap plugin but it was not possible to configure environment variable in this case. As /opt/sonarqube/conf/ is not empty, I can't use configMap to decouple configuration from image content. So, I build my own sonarqube image adding the ldap jar plugin and ldap setting in sonar.properties :
# General Configuration
sonar.security.realm=LDAP
ldap.url=ldap://myldap:389
ldap.bindDn=CN=mysa=_ServicesAccounts,OU=Users,OU=SVC,DC=net
ldap.bindPassword=****
# User Configuration
ldap.user.baseDn=OU=Users,OU=SVC,DC=net
ldap.user.request=(&(sAMAccountName={0})(objectclass=user))
ldap.user.realNameAttribute=cn
ldap.user.emailAttribute=mail
# Group Configuration
ldap.group.baseDn=OU=Users,OU=SVC,DC=net
ldap.group.request=(&(objectClass=group)(member={dn}))

Configure swagger in gradle build script with kotlin dsl

I'm trying to switch my simple project from Groovy to Kotlin in build scripts.
I'm using this plugin:
https://github.com/gigaSproule/swagger-gradle-plugin
I have this configuration in my build script:
swagger{
apiSource {
springmvc = false
locations = ['my.location']
schemes = ['https']
host = 'test.com:8080'
info {
title = 'My Service'
version = 'v1'
}
swaggerDirectory = "$buildDir/swagger"
}
To where shall I refer to in this situations?
Shall I do something like?
task( "swagger" ) {
...
}
It is not quite familiar for me.
Thanks.
In case anyone is still looking for this information, this is how you would do it using Gradle Kotlin DSL:
import com.benjaminsproule.swagger.gradleplugin.model.*
plugins {
id("com.benjaminsproule.swagger") version "1.0.0"
}
swagger {
apiSource(closureOf<ApiSourceExtension> {
springmvc = false
schemes = mutableListOf("https")
host = "test.com:8080"
info(closureOf<InfoExtension> {
title = "My Service"
version = "v1"
description = "My Service Description"
termsOfService = "http://www.example.com/termsOfService"
contact(closureOf<ContactExtension> {
email = "email#internet.com"
name = "A Developer"
url = "http://www.internet.com"
})
license(closureOf<LicenseExtension> {
url = "http://www.apache.org/licenses/LICENSE-2.0.html"
name = "Apache 2.0"
})
})
locations = mutableListOf("com.foo.fighting")
swaggerDirectory = "$buildDir/swagger"
})
}
I've tested it using Gradle v4.6.

Resources