Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grails 3.0.2 - Add ability to use system properties / command line arguments in application.yml #9086

Closed
mhuebner opened this issue Jul 7, 2015 · 13 comments

Comments

@mhuebner
Copy link

mhuebner commented Jul 7, 2015

This would simplify the deployment process for container based hosting environments (like aws Beanstalk).

Sample setup:
System property JDBC_CONNECTION_STRING ist set to " 'jdbc:mysql://localhost/my_db?useUnicode=true&characterEncoding=UTF-8&username=xxx&password=xyz'"

(This property is attached with "-DJDBC_CONNECTION_STRING=")

    production:
        dataSource: 
            url: ${JDBC_CONNECTION_STRING}
@graemerocher
Copy link
Member

This is already possible with:

production:
    dataSource: 
        url: '${JDBC_CONNECTION_STRING}'

@mhuebner
Copy link
Author

mhuebner commented Jul 8, 2015

Thanks for adding this to the docs! I already tried this solution as suggested:

ERROR grails.boot.GrailsApp - Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'grailsConfigProperties' defined in null: Could not resolve placeholder 'JDBC_CONNECTION_STRING' in string value "${JDBC_CONNECTION_STRING}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'JDBC_CONNECTION_STRING' in string value "${JDBC_CONNECTION_STRING}"
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'grailsConfigProperties' defined in null: Could not resolve placeholder 'JDBC_CONNECTION_STRING' in string value "${JDBC_CONNECTION_STRING}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'JDBC_CONNECTION_STRING' in string value "${JDBC_CONNECTION_STRING}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:155) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:155)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:49)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:280)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:269)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:43)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:88)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)
    at de.pfc.Application.main(Application.groovy:8)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'JDBC_CONNECTION_STRING' in string value "${JDBC_CONNECTION_STRING}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:209)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitMap(BeanDefinitionVisitor.java:262)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:198)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitGenericArgumentValues(BeanDefinitionVisitor.java:159)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.2.4.RELEASE.jar:1.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) ~[spring-boot-1.2.4.RELEASE.jar:1.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) ~[spring-boot-1.2.4.RELEASE.jar:1.2.4.RELEASE]
    at grails.boot.GrailsApp.run(GrailsApp.groovy:49) [grails-core-3.0.2.jar:3.0.2]
    at grails.boot.GrailsApp.run(GrailsApp.groovy:280) [grails-core-3.0.2.jar:3.0.2]
    at grails.boot.GrailsApp.run(GrailsApp.groovy:269) [grails-core-3.0.2.jar:3.0.2]
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    ... 25 more
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270) [springloaded-1.2.3.RELEASE.jar:1.2.3.RELEASE]
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) [groovy-2.4.3.jar:2.4.3]
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324) [groovy-2.4.3.jar:2.4.3]
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:43) [groovy-2.4.3.jar:2.4.3]
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:88) [groovy-2.4.3.jar:2.4.3]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) [groovy-2.4.3.jar:2.4.3]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110) [groovy-2.4.3.jar:2.4.3]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122) [groovy-2.4.3.jar:2.4.3]
    at de.pfc.Application.main(Application.groovy:8) [main/:na]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'JDBC_CONNECTION_STRING' in string value "${JDBC_CONNECTION_STRING}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204) ~[spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178) ~[spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:209) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitMap(BeanDefinitionVisitor.java:262) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:198) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitGenericArgumentValues(BeanDefinitionVisitor.java:159) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    ... 25 common frames omitted
:run FAILED

Starting the app with param "-DJDBC_CONNECTION_STRING=jdbc:mysql://localhost"

@anshbansal
Copy link
Contributor

@mhuebner It's not merged yet. I added a comment to not merge if this is not working.

@graemerocher
Copy link
Member

How did you start the app? Grails doesn't automatically pass system properties on the command line. You will need to modify the bootRun target of your build.gradle to add system properties if you used grails run-app

@mhuebner
Copy link
Author

mhuebner commented Jul 8, 2015

Ok, now i see. I wasn't aware of that.
I tried to bind the System.properties but with no luck:

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

// or

tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
    systemProperty('JDBC_CONNECTION_STRING', 'test')
}

This gives me the same Result (Exception)

@mhuebner
Copy link
Author

mhuebner commented Jul 8, 2015

Followup: it's working now as expected - with the following syntax:

run {
    systemProperties = System.properties
}

@graemerocher @anshbansal thanks for your hint! Perhaps this could be documented anywhere??

@anshbansal
Copy link
Contributor

@gramerocher Suggestions? I am not sure where to add this in the docs.

@nobeans
Copy link
Contributor

nobeans commented Jul 8, 2015

At the head (d380bea) of 3.0.x, the place holder couldn't be resolved. It's just ignored. Should I create new issue?

@graemerocher
Copy link
Member

@nobeans Using the latest 3.0.x I added:

 environments:
development:
    dataSource:
        dbCreate: create-drop
        url: '${JDBC_CONNECTION_STRING}'

And then I changed build.gradle to:

  tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
      systemProperty('JDBC_CONNECTION_STRING', 'test')
  }

And it worked fine for me

@graemerocher
Copy link
Member

@anshbansal Probably somewhere under the Configuration section http://grails.github.io/grails-doc/latest/guide/conf.html

@nobeans
Copy link
Contributor

nobeans commented Jul 8, 2015

@graemerocher I tried your example. It actually worked well. But when I tried to get via Holders.config, it was still a expression of placeholder. Using the 3.0.2, both worked well.

@graemerocher
Copy link
Member

@nobeans Please raise a separate issue and I will have a look. Thanks.

@nobeans
Copy link
Contributor

nobeans commented Jul 8, 2015

Sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants