How to build a custom environment in Grails - grails

I'm trying to add a new environment to our Grails WAR (let's call it "staging"). I can manage the configuration in Config.groovy and DataSource.groovy and access the right configuration at run-time with -Dgrails.env, but how do I build this WAR?
The Grails documentation does not cover this case and the links on the page seem to be outdated.

You are so very close to having the right combination in your question, this should work:
grails -Dgrails.env=staging war
Actually, the documentation for the war command even uses 'staging' as the environment used.
The same goes for any environment-specific command:
grails -Dgrails.env=<environment name> <command>

Related

Grails 2.4.4 - How to store config variable at war phase and get it in a Tomcat env?

When running "war" command from Grails, I want to store the build commit for use it in a GSP thereafter.
In my Config.groovy
def proc = 'git rev-parse HEAD'.execute()
proc.waitFor()
build_commit.number = proc.in.text
In my GSP
<p>${grailsApplication.config.build_commit.number}</p>
Everything work fine when I run my app with "run app" command but when I deploy my War on Tomcat the information is no longer available.
Is it because Config.groovy is reevaluate when the war is deploy ?
I also try to write programmatically the build commit to application.properties but I couldn't find a way to do that..
If it's possible, I also need to know how to detect a Tomcat env for avoid rewriting a blank value.
Any help will be appreciate.
If you need persistent data (like your build_commit.number) you should investigate integrating your application with a database, or writing this to a file on your filesystem. Either one will require access from both your development environment (where you run "run app") and your production environment (where you deploy the war to tomcat).

Grails + Tomcat6 + Multiple Instances + Shared Lib Folder

I've got a Tomcat6 server that runs multiple Instances for two separate grail apps.
When I compile my WAR file for deployment normally
run-app -Dgrails.env=production war test.war
It deploys correctly and everything works as it is suppose too.
The problem is, I don't want the JAR files included in my WAR.
So I use the following command line instead
run-app -Dgrails.env=production war test.war --nojars
Now when my grails app deploys (it doesn't) I get a java.lang.NoSuchMethodError
I have copied the lib folder (from my initial test.war) to the following locations
${catalina.base}/shared/lib
${catalina.home}/shared/lib
${catalina.home}/lib
None of these work.
My catalina.properties all point to the correct locations.
Any ideas?
A few ideas:
BuildConfig.groovy has inherits global, which has the app inherit all of the grails/plugins dependencies. If you change this, it may affect both your build and packaging - plus I have yet to encounter any documentation on what type of other things you can do with the inherits DSL
Grails deployment documentation suggests there is a way to customize which dependencies make it into the war file: http://grails.org/doc/latest/guide/17.%20Deployment.html
Event hooks give you access to provide a closure routine into various stages of the grails lifecycle. Can it strip out framework jars from the final war? Haven't tried that either - only using it to re-write various config files for additional envrionment configuration. However it does look like packaging events are exposed to this API:
http://grails.org/doc/latest/guide/4.%20The%20Command%20Line.html

Change Configurations after War Build

I have two configurations I need to change after building a prod war with grails 1.3.2. I know the project can be setup in ways to do this externally in the first place, but that's not an option at this moment.
1) Need to change environments.production.hibernate.default_schema defined in DataSource.groovy
2) Need to change environments.production.grails.serverURL defined in Config.groovy
Is there any way to edit the war or pass overriding arguments when running the war in JBoss?
If you want to change these properties in a production application with out redeployment your out of luck. Your only option is using external properties files which will require a rebuild and redeployment of the app. See the following link. http://www.comitservices.com/wp/?p=133

Grails war command defaults to development environment under netbeans

When I generate the war in my grails app via right-click / run grails / command / war, it shows "Environment set to development", having it run without parameters. Why is this happening when according to the docs it should default to production (which is what I need)?
BTW, is there a way having built the whole grails application with NetBeans to use the command line to generate the wars ?
Versions: NetBeans 6.7.1, Grails 1.2
OS: Ubuntu 9.1
Thanks
The netbeans plugin is probably running the command "grails dev war" which creates a war file based on the development settings. The normal "grails war" command uses production settings.
You should just be able to change to the directory containing your grails-app and src folders and run the command
grails war
This assumes that you have grails installed and on your path.
No, only that grails war may assume it to be a development. I suggest you specifically type as follow:
grails prod war
you can modify the proyect file gradle.properties and add
org.gradle.jvmargs=-Dgrails.env=dev //for development
org.gradle.jvmargs=-Dgrails.env=prod //for production enviroment
Check https://docs.grails.org/latest/guide/gettingStarted.html

Create grails war without version number

How do I create a grails war file so that it doesn't have the version number
(e.g. foo-0.1.war)
attached to the end when I execute the 'grails war' command?
In case anybody comes upon this article and is using Grails 1.3.x, the configuration option has changed from grails.war.destFile in Config.groovy to being grails.project.war.file in BuildConfig.groovy.
Also the file name is relative to the project workspace, so it should have a value like:
grails.project.war.file = "target/${appName}.war"
This is according to the latest Grails documentation.
I think you can specify the war name in the war command.
grails war foo.war
Also check the latest Grails documentation for where to set this as a configuration option. See the other answers for details.
From the Grails Documentation, Chapter 17, Deployment
There are also many ways in which you can customise the WAR file that is
created. For example, you can specify
a path (either absolute or relative)
to the command that instructs it where
to place the file and what name to
give it:
grails war /opt/java/tomcat-5.5.24/foobar.war
Alternatively, you can add a line to
Config.groovy that changes the default
location and filename:
grails.war.destFile = "foobar-prod.war"
Of course, any command line argument
that you provide overrides this
setting.
Rolling up the other excellent answers. There are several options:
Explicitly set it on the command line: grails war foo.war
Set the app.version property to empty in application.properties will cause the war to be named foo.war.
Explicitly set the name of the war using the grails.war.destFile property in Config.groovy
Grails 3.x has switched to gradle and uses the war plugin. You can just specify name like this in the build.gradle file:
war {
archiveName 'foo.war'
}
Another way to generate war files without version number is to keep the property, app.version, empty in the application.properties
I am kind of late to the party... but anyway:
I think the reason behind removing the version number is to eliminate the need to rename the war file so it deploys on "correct" context path /appName. If that's the case then a better option is to use a versioned war filename so you can deploy multiple versions at the same time on tomcat using the following naming pattern in grails-app/conf/BuildConfig.groovy:
grails.project.war.file = "target/${appName}##${appVersion}.war"
As explained in http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Parallel_deployment
This method applies to wars in general, not only grails' wars.

Resources