How to get run-time arguments programatically in grails - grails

I'm developing a sample grails application which runs in different environment. I will pass the environment name as runtime argument like below,
grail run-app –Denv=dev1
grail run-app –Denv=dev2
I need to get the environment name dev1 or dev2 programatically. I'll be using the values in GSPs. Kindly help. Thanks.

You can use grails.util.Environment :
println Environment.currentEnvironment.name

Related

WAR name based on Environment in BuildConfig.groovy

I want to name my WAR files automatically depending on appName, appVersion and the short name of Environment.current. I have the following option setup in BuildConfig.groovy:
String currentEnvShortName = Environment.envNameMappings.find{it.value == Environment.current.name}.key
def f = new File("grails-app/conf/config.properties")
f.text = ""
f << "Current Env: ${currentEnvShortName}"
grails.project.war.file = "target/war/${appName}-${appVersion.toString().replaceAll(/\./,' ').split().join("-")}${currentEnvShortName}.war"
I write the environment to a file so that I don't have to wait for the war command to execute. EDIT: USING GGTS COMMAND LINE. First, I thought everything works as expected. My usual deployment process is as follows:
run grails clean
run war command for the desired environment
deploy WAR on application server
After clean, however, and possibly also after other grails commands, the value of Environment.current changes in a way I do not understand. It is always dev the first time I run the war command. After this first time, the environment name is retrieved correctly again. So what I do as a workaround is:
run grails clean
run war command for the desired environment, but break right after starting it
run war command for the desired environment
deploy WAR on application server
Am I doing anything wrong or is this a bug?
Environment to reproduce:
GGTS 3.6.4 RELEASE
Grails 2.4.2
Groovy 2.3.10
JDK 1.6.0_07
EDIT: I could not reproduce using Grails from regular command line. This behaviour only occurs with Groovy Grails Tool Suite's built in command line. The Environment seems to be switching randomly.
EDIT 2: OS: Windows 7
Well, I tried your scenario multiple times but couldn't able to reproduce that. But, if it is not working for your for some reason, you can use the another approach as follows:
Modify your code in your BuildConfig.groovy like:
// Define a custom mapping so that you can easily extend for custom environments (which will not be possible via "Environment.current.envNameMappings"
Map envNameMappings = [test: "test", development: "dev", production: "prod"]
// Read the currrent environment from System property
String currentEnv = System.getProperty("grails.env")
println currentEnv
// And this is the short name of environment you want
println envNameMappings[currentEnv]
The issue only occurs with the built in command line of Groovy Grails Tool Suite on Windows. If the war command is executed from Windows CMD, then the environment is used correctly.
However, when executing from Windows CMD, encoding breaks for me. I have addressed this specific issue in another question.

grails -Dgrails.env=xxx always says "Script Xxx" not found,did you mean

Grails 2.5, Java 7 64bit, windows 8.1
This is very odd. We have a large app, and I am the only windows user (everyone else on macs). For some reason, I cannot do anything with environments on the command line.
E.g. I can do this:
grails run-app
But I cannot do this:
grails -Dgrails.env=myenv run-app
It always says:
Script 'Myenv' not found, did you mean...
In my DataSource.groovy, I have:
environments {
development {
dataSource {
// stuff
}
myenv {
// same stuff as the above stuff
}
}
It works for other users, just not me.
Any ideas how I debug this? It means I have not been able to do development, as I need multiple envs to do liquibase diffs etc.
On Windows you have to pass the params in quotes, like this:
grails "-Dgrails.env=myenv" ...
The docs mention only the way it can be used with the shell script. Either windows or the used grails.bat behave differently.
The issue is that you are passing the parameters to the run-app script before the script name. You should use:
grails run-app -Dgrails.env=myenv

new grails project won't start

I'm trying to get my feet wet with grails, so I'm following a tutorial to get going with a sample project. I downloaded grails 2.3.2, added the environment variables for the command prompt commands, and successfully created a project by using >grails create-app teamwork. Calling >grails run-app after changing to the project directory successfully downloads all requisites, but then I get . I know that this exception doesn't reveal much, but do you guys have any ideas on why? I am running command prompt in admin mode, if that means anything.
Try not forking the JVM in BuildConfig.groovy, if I recall correctly. If that works, then check the JIRA issues for Grails 2.3.2.

Grails: server crashes when I recompile a file (when using datasources)

I'm using datasources in my Grails project.
When I edit a groovy file with server running I get the following error:
Running Grails application.. java.lang.IllegalArgumentException: Must
supply a resource type for JNDI configuration
How can I configure grails.naming.entries in Config.groovy to fix this?
Note: I'm using grails 1.3.6
Tks
I'm using 1.3.7 and hit the same issue. There's a couple bugs in the JIRA about this, looks like they were finally fixed in 2.0-M1. There's a patch for 1.3.1+ versions here, but I haven't tested that yet.
Link to possible patch: http://jira.grails.org/browse/GRAILS-7021

How to pass arguments to Grails in command line?

I'm trying (Grails 1.3.7):
grails tomcat deploy -Dtomcat.deploy.username=abc
With no effect. Is it a correct syntax? I can't find it in documentation...
Have you tried moving the -Dtomcat.deploy.username=abc to be right after grails in the command? i.e.:
grails -Dtomcat.deploy.username=abc tomcat deploy
Two reasons why I suggest this:
In the past, I know I've had trouble declaring -D properties at the end of the command (like you're observing).
I know that grails -Dserver.port=xxxx run-app does work, since I use it often, and that's using the -D before any other arguments to grails.

Resources