Convert application.yml configuration file to application.groovy in Grails 3.x

I am trying to create a simple Grails 3 project and am stuck in something very simple. Therefore, I want my data source properties to come from the VM parameters that I set in my IntelliJ IDE. Earlier in Grails 2.x, I just did something like:

environments { development{ //Database connection properties def dbserver = System.properties.getProperty('dbserver') def dbport = System.properties.getProperty('dbport') ............ dataSource { url: "jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname} } } 

Now that I have application.yml, how can I access "System.properties" and paste it into yml? I read that we can use application.groovy instead, if YML doesn't support it, in this case it looks like application.groovy:

 grails { profile = 'web' codegen { defaultPackage = 'defPack' } } info { app { name = '@ info.app.name@ ' version = '@ info.app.version@ ' grailsVersion = '@ info.app.grailsVersion@ ' } } spring { groovy { template['check-template-location'] = false } } hibernate { naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy' cache { queries = false } } grails { mime { disable { accept { header { userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident'] } } } types { all = '*/*' atom = 'application/atom+xml' css = 'text/css' csv = 'text/csv' form = 'application/x-www-form-urlencoded' html = ['text/html', 'application/xhtml+xml'] js = 'text/javascript' json = ['application/json', 'text/json'] multipartForm = 'multipart/form-data' rss = 'application/rss+xml' text = 'text/plain' hal = ['application/hal+json', 'application/hal+xml'] xml = ['text/xml', 'application/xml'] } } urlmapping { cache { maxsize = 1000 } } controllers { defaultScope = 'singleton' } converters { encoding = 'UTF-8' } views { default { codec = 'html' } gsp { encoding = 'UTF-8' htmlcodec = 'xml' codecs { expression = 'html' scriptlets = 'html' taglib = 'none' staticparts = 'none' } } } } dataSource { pooled = true jmxExport = true driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' dbCreate = '' username = 'someUsername' password = 'somePass' } environments { development { dataSource { url = 'jdbc:sqlserver://localhost:1234;databaseName=someDbName;' } } } 

Thanks.

UPDATE:

application.groovy is not accepted by default, even when I deleted application.yml

+5
source share
2 answers

It turned out that I had a problem, I needed to put the default keyword in quotation marks. For instance:

 grails { profile = 'web' codegen { defaultPackage = 'defPack' } } info { app { name = '@ info.app.name@ ' version = '@ info.app.version@ ' grailsVersion = '@ info.app.grailsVersion@ ' } } spring { groovy { template['check-template-location'] = false } } hibernate { naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy' cache { queries = false } } grails { mime { disable { accept { header { userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident'] } } } types { all = '*/*' atom = 'application/atom+xml' css = 'text/css' csv = 'text/csv' form = 'application/x-www-form-urlencoded' html = ['text/html', 'application/xhtml+xml'] js = 'text/javascript' json = ['application/json', 'text/json'] multipartForm = 'multipart/form-data' rss = 'application/rss+xml' text = 'text/plain' hal = ['application/hal+json', 'application/hal+xml'] xml = ['text/xml', 'application/xml'] } } urlmapping { cache { maxsize = 1000 } } controllers { defaultScope = 'singleton' } converters { encoding = 'UTF-8' } views { 'default' { codec = 'html' }//THIS WAS THE SOURCE OF ERROR gsp { encoding = 'UTF-8' htmlcodec = 'xml' codecs { expression = 'html' scriptlets = 'html' taglib = 'none' staticparts = 'none' } } } } def dbserver = System.properties.getProperty('dbserver') def dbport = System.properties.getProperty('dbport') def dbusername = System.properties.getProperty('dbusername') def dbpassword = System.properties.getProperty('dbpassword') def dbname = System.properties.getProperty('dbname') dataSource { pooled = true jmxExport = true driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' dbCreate = '' username = dbusername password = dbpassword } environments { development { dataSource { url = 'jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname}' } } } 
+3
source

There can be no direct answer to your question. You can access the system properties in application.yml by adding below: build.gradle

 tasks.withType(org.springframework.boot.gradle.run.BootRunTask) { systemProperties = System.properties 

Link: https://github.com/grails/grails-core/issues/9086

+1
source

All Articles