WAR name based on Environment in BuildConfig.groovy - grails

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.

Related

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

Grails/aNimble on Windows : Getting cannot find the specified path

I am trying to setup aNimble on Windows following this article.
I have setup the Java and MySQL (xampp) on Windows and both are working properly.
Now, as the final step I need to execute
grails prod execute-database-scripts-all
but I am getting cannot find the specified path. Even when I type grails only I get the same error. I have zero experience in Grails, but it seems that I need to install Grails first on the PC. Until I do this, I want to know is there anything else that I need to do this to successfully execute this command.
What are the three portions of this command grails, prod and execute-database-scripts-all
This error means that Windows is unable to find grails executable anywhere it looks (in any paths specified in PATH system property) when you typing grails command. You may have to follow this guide in order to install grails application framework properly in your system (also, make sure you are installing a correct version of grails compatible with your distribution of aNimble) and then retry.
Grails is a web application framework and which provides a set of tools to develop, build and run web applications like aNimble, prod is a command line option for grails telling it to run in a production mode and execute-database-scripts-all is aNimble-specific command to initialize it's database.

grails v3.0.0.M1 - I can create a script, but not sure how to run it

I started trying to have a peek at grails v3.0.0.M1.
I unpacked and setup and created an app
When creating a script by using folowing command
grails create-script my-script
it creates a script and drops the hyphen - so script is myscript.groovy. Thought it might camel case that. More importantly - how do you run it?
In the grails command line prompt when I go help - the new script is not visible. If you try grails> my-script, it fails with
Error command not found my-script...
Having created a script - how do you run it?
Given a script named myscript.groovy, run it as
grails myscript
I haven't looked at the code, but it seems like a bug that it's dropping the hyphen. You should create an issue at https://jira.grails.org/browse/GRAILS/. It's easy to fix though, just rename the file to my-script.groovy and then you can run
grails my-script

Setting up Rails project on TeamCity hosted on a Windows server

I'm setting up my first Ruby project on Team City, which is hosted on a Windows Server, but I'm having a problem. Now, because the server may not have the required gems installed, I've added a command line build step:
bundle install
Now I thought this would be enough, but apparently bundle is not recognized as an internal or external command. Except, if I RDP into the server, if I run bundle install from anywhere, it is fine, and just notifies me that no gemfile was found.
Any ideas on if I've missed a step, or I'm going about this the wrong way?
Most likely this is a problem with TeamCity not finding the path to ruby executables.
You can address this by overriding the value to the PATH environment variable in your build configuration in the Build Parameters section.
env.PATH=/path/to/ruby;%env.PATH%
See this answer for the proper links to documentation, etc.
EDIT #1
I noticed when updating one of my configurations that TeamCity is supposed to take care of appending values so you DO NOT need to set path equal to itself. The post mentioned above is a workaround for a bug where TeamCity was overwriting the values, but that has been corrected. See the help at the mouse-over for more information:
EDIT #2
I tested edit #1 and found that is not the case. You do need to
create an environment variable env.Path
and set it's value to itself plus your new path; in my example, C:\Program Files\MySQL\MySQL Server 5.6\bin\;%env.Path%
you do NOT need to say env.Path=... as listed above; that is what the configuration file will look like.
I tested this out by doing the following:
Created a new project with no repository
Added a command line build step to `echo %env.Path%
Added a command step to call MySql mysql --help This will fail if it cannot find MySql
I then ran it for each of the following settings for the env.Path variable:
Not added / changed; TeamCity reports out the environment variable for the build agent as is.
Added as just C:\Program Files\MySQL\MySQL Server 5.6\bin\. TeamCity reports out only that entry.
Added as C:\Program Files\MySQL\MySQL Server 5.6\bin\;%env.Path%. TeamCity prepends C:\Program Files\MySQL\MySQL Server 5.6\bin\ to the build agent's values shown in #1. The result is what we want, #1 + #2

grails is not recognized as an internal or external command

Hi i installed grails at
c:/grails
directory on my windows 7
after that i created GRAILS_HOME environment variable as mentioned on grails website
but now when i run this command
grails integrate-with --intellij
on command prompt it throws an error :
grails is not recognized as an internal or external command
any thoughts what i am doing wrong here
I have faced the same problem and did as follows to get rid off.
Although you have already mentioned that, you set Environment variables. I am assuring again:
In environment variables dialogue: (user variables section)
1. Set GRAILS_HOME to your extracted grails version as GRAILS_HOME=D:\GrailsEnvironment\grails-2.1.1
2. Set JAVA_HOME to your installed jdk version as JAVA_HOME=C:\Program Files\Java\jdk1.7.0_04
In environment variables dialogue: (System variables section)
1. Edit path and add D:\GrailsEnvironment\grails-2.1.1\bin
2. Edit path and add C:\Program Files\Java\jdk1.7.0_04\bin
If needed reboot your pc again. and in command prompt run 'grails -version' and 'java -version' command to check whether grails and java were perfectly installed.
If error shows then may be your grails package is corrupted, Extract your grails package newly in the specified path again. run the command to check. if necessary reboot and run command.
Thanks

Resources