How to extend per environment configuration in grails - grails

It seems that only grails.serverURL and grails.path are recognized as per environment configrautions. bla and foo are ignored and could not be used in application
Anyone could solves this and provide a way to get bla and foo configured per environment?
environments {
production {
grails.serverURL = "http://alpha.foo.de"
grails.path = ""
bla = "text"
foo= "word"
}
test {
grails.serverURL = "http://test.foo.de"
grails.path = ""
bla = "othertext"
foo= "otherword"
}
}

Since Grails 3.0, both Groovy or YAML syntax can be used. The new main application configuration file is /conf/application.yml but you can continue to use your existing groovy configuration defining a /conf/application.groovy file.
This is an example of datasource definition per environment (in YAML):
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb
production:
dataSource:
dbCreate: update
url: jdbc:h2:prodDb
myenv:
dataSource:
dbCreate: update
url: jdbc:h2:myenvDb
To run a grails command in a specific environment you can use:
grails [environment] [command name]
To target an environment different from dev, test and prod you can use:
grails -Dgrails.env=myenv run-app
See the grails environment documentation or this example application for more information.

All the ConfigSlurper variables are scoped by environment. What you've shown above should work fine.
When you use grails run-app, you are running in the development environment by default. Could that be your issue?

Try...
> grails prod run-app
This should give you bla (text) and foo (word)
> grails test run-app
This should give you bla (othertext) and foo (otherword)

Config.groovy setting
environments {
development {
grails.serverURL = "http://alpha.foo.de"
grails.path = "/bar"
staticServerURL = "http://static.foo.de"
staticPath = "/static"
}
}
source code index.gsp
${grailsApplication.config.staticServerURL}a
${grailsApplication.config.staticPath}b
${grailsApplication.config.grails.serverURL}c
${grailsApplication.config.grails.path}d
what is printed out when started with grails run-app
a b http://alpha.foo.dec /bard

Check in your Config.groovy that you have the import for Environment
import grails.util.Environment
Otherwise the Environment.current is empty.

Related

Grails 3: Integration tests run at a development environment, not at a test environment

I have separated dataSourceConfig.yml database config file:
environments:
development:
dataSource:
dbCreate: none
url: jdbc:oracle:thin:xxxxxx
driverClassName: oracle.jdbc.OracleDriver
dialect: org.hibernate.dialect.Oracle10gDialect
username: xxxx
password: xxxx
test:
dataSource:
dbCreate: none
url: jdbc:oracle:thin:xxxxx
driverClassName: oracle.jdbc.OracleDriver
dialect: org.hibernate.dialect.Oracle10gDialect
username: xxxxx
password: xxxxx
Which I connect to the project in the Application.java:
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
#Override
void setEnvironment(Environment environment) {
String configPath = environment.getProperty("local.config.location")
Resource resourceConfig = new FileSystemResource(configPath)
YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean()
ypfb.setResources([resourceConfig] as Resource[])
ypfb.afterPropertiesSet()
Properties properties = ypfb.getObject()
environment.propertySources.addFirst(new PropertiesPropertySource("local.config.location", properties))
}
}
When i run integration tests via Intellij IDEA 15, it runs tests at a development environment, but the YAML config file has test section.
Is anyone knows how to fix this?
The command bellow doesn't help.
grails test test-app -integration
If you are going to run tests from the IDE you need to modify the run config to include -Dgrails.env=test. You will want to do that for the default JUnit run config so you don't have to edit every single test run config. Be aware that editing the default JUnit run config will affect all configs that are created in the future but will not update any existing configs. You may want to remove all of the existing run configs so they will be recreated with the new settings the next time you run those tests.

Grails - How can I get serverURL in config.groovy?

I want to use ServerURL in config.groovy.
But, I need configure url.setting both development and production if I cannot use serverURL in config.groovy.
I try to get serverURL like below, it makes error.
config.groovy
def grailsApplication
environments {
development {
grails.logging.jul.usebridge = true
grails.serverURL = "http://localhost:8080"
}
production {
grails.logging.jul.usebridge = false
grails.serverURL = "http://myapp.com"
}
}
url.setting = "${grailsApplication.config.grails.serverURL}"
the error when I run app is like below
Error Error packaging application: Error loading Config.groovy: Cannot get property 'config' on null object (Use --stacktrace to see the full trace)
You can get the server URL like-
url.setting="${grails.serverURL}"

grails logfile does not created in production server

In my Grails project I've created and implemented log4j logging system.
In local environment file is correctly created, but in production server it is not.
here is the log4j configuration inside config.groovy file:
log4j = {
appenders {
file name: 'file', file:"/home/file.log"
}
root{
info 'stdout', 'file'
}
}
I've tried with other paths but it does not work.
How can I solve this issue?

Grails deploy: no such warName for class: Tomcat

When I try to deploy to a remote Tomcat server using grails prod deploy tomcat I get the error:
Has anybody encountered that?
P.S. On the contrary, the command mvn tomcat7:deploy works.
Problem was resolved by adding def warName = configureWarName() to Tomcat.groovy
...
switch (cmd) {
case 'deploy':
war()
def warName = configureWarName()
println "Deploying application $serverContextPath to Tomcat"
deploy(war: warName, url: url, path: serverContextPath, username: user, password: pass)
break
....

externalizing configurations for test.datasource

I have been able externalize DataSource configurations for development environment by updating grails.config.locations in Config.groovy and then setting specific configurations in .grails/${appName}-config.properties as following:
dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.url = jdbc:mysql://localhost/db-dev
dataSource.username = root
dataSource.password = pass
For the 'test' environment, i have different Database so i would like to updated test.dataSource.url accordingly but this doesn't work. It still takes the 'dev' dataSource.url when running 'grails test' mode(i.e. grails test dgm-update). How to externalize test environment DataSource configuration?
Thank You for helping
grails.config.locations = ["file:${userHome}/.grails/${appName}-config-${grails.util.Environment.current.name}.properties"]
and then put nested files for each env:
/home/me/.grails/myapp-config-dev.properties
/home/me/.grails/myapp-config-test.properties
/home/me/.grails/myapp-config-production.properties
Try to use Groovy external configuration, i.e. ".grails/${appName}-config.groovy" with environment specific DSL:
environments {
test {
dataSource {
...
}
}
}

Resources