creating a config file in grails - grails

I am attempting to get a Grails project working but need help setting it up. I have an Ubuntu server running on a VM that has Redis installed. The project won't run unless I create a config file that can use Redis on the Ubuntu server. This is the settings I pulled down from GitHub located in the grails-app/conf/Config.groovy file.
http://snag.gy/eYhUY.jpg
I was told I need to create a separate config file that will override these parameters so my project will talk to the ubuntu server on my machine. This is a noob question but where do I create a config file? I can't seem to find a .grails folder. I know I'm suppose to reference my config file, once i've created one, in the grails-app/conf/Config.groovy file
http://snag.gy/SpGGt.jpg

Look at the grails.config.locations specified in your Config.grooy and you can create any of those locations for creating the external Config file.
I prefer using the classpath route. Here is what I would do.
Create a folder (say appConfig) and place it in the tomcat/conf folder.
Add the application config file (proghorn-config.groovy in your case) to the folder, with the required configurations in the file.
Add the folder to the Tomcat classpath by updating either the tomcat/conf/catalina.properties or by creating the tomcat/bin/setenv.sh

The location of the .grails folder depends upon the user account running the container (Tomcat, Jetty, etc.) which hosts your Grails application.
For example on Debian 6 running Tomcat 6.x the location is:
/usr/share/tomcat6/.grails/
You can also use static paths as well:
file:/usr/local/tomcat/conf/myspecific-config.groovy

Related

Grails standalone JAR as linux service configure memory parameters

I have a Grails application that I run as a Linux service. Basically I create a symlink from /etc/init.d/mygrailsservice to mygrailsservice.jar.
I want to increase the amount of OS memory allocated to the service.
How do I configure this?
Have a look at the spring-boot docs for executable jars and using them as system services - https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
Specifically https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-script-customization-when-it-runs
With the exception of JARFILE and APP_NAME, the above settings can be
configured using a .conf file. The file is expected next to the jar
file and have the same name but suffixed with .conf rather than .jar.
For example, a jar named /var/myapp/myapp.jar will use the
configuration file named /var/myapp/myapp.conf.
myapp.conf.
JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder

Grails 3 plugins configuration

New to Grails 3- starting to port.
Have tried placing config values in application.groovy and application.yml within plugin conf dir to no avail - trying to read values from within plugin service fails. Adding values to the main application's application.groovy works.
What is the secret here? Previously I could load specific conf files via config.locations simply by naming them which was nice and simple. I've seen some resolutions that include needing to setup env vars with paths to config files which I'd like to avoid. Then they set up file URIs for dev and classpath URIs for other envs that will be war packaged - don't really want to do any of this.
Do we no longer have the ability to place config within a plugin and have that automatically merged with the applications config?
The plugin may provide config settings in grails-app/conf/plugin.yml.

Troubleshooting Grails merged configuration during application startup

Suppose I have a Grails application named myCoolApp.
Furthermore, suppose I have defined some basic properties in grails-app/conf/Config.groovy and grails-app/conf/DataSource.groovy under the test and production profiles.
Config.groovy is set to merge with the following .groovy external configuration file, if found at all:
grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]
Finally, I have also defined, inside an external testing server, such a file, under a local *NIX user path:
/home/appServerTestUser/.grails/myCoolApp-config.groovy
Sadly, I find that for some reason, the external .groovy file's properties are not being merged when a Tomcat instance starts up.
What options do I have to make Grails tell me: "Oh, yes, I found your external config file at: /home/appServerTestUser/.grails/myCoolApp-config.groovy" and these are the properties I merged into Config.groovy?
Thanks!
File-based paths don't make much sense in a deployed app. It could work when deploying on your dev machine for testing, but will likely fail on any other machine. And it'll probably fail locally even if configured with the correct path since the process will like run as a different low-priviledge user.
You can use absolute paths, e.g. /etc/myapp/path/to/file, but this tends to couple deployment to filesystem structure, so if you develop in Windows and deploy on Linux it'd be tricky to get them both working.
So the best bet for Tomcat deployment is to use the classpath syntax. You can specify multiple files and it will load all that it finds, so I usually have one entry for local dev and one for the deployed app:
grails.config.locations = [
"classpath:${appName}-config.groovy",
"file:./${appName}-config.groovy"
]
I delete the entries for .properties files because Groovy syntax is so much more flexible, but use them if you like. I also keep the dev files in the project root (and exclude from source control).
So when deploying, name the file correctly (different apps will have different files, so no clashing there) and put them in Tomcat's lib directory. This is in the classpath, and so the classpath: entry will see it.

how to access grails application on Tomcat

I'm trying to deploy a simple grails application on my test-server which is tomcat. I've built the WAR with grails war command so I've got a war file named: simplgrailsapp-0.1.war
When I deploy this to tomcat I'm getting a lot of errors which I'm going to resolve one-by-one. But the URL for the app from tomcat manager (mytestserver:8080/manager) is http://mytestserver:8080/simplegrailsapp-0.1/ which doesn't seem correct. Because when I run the app locally the url is: localhost:8080/simplegrailsapp
I've also tried to access the app by http://mytestserver:8080/simplegrailsapp but that doesn't work either.
How can I fix this?
By default, when you deploy a WAR to tomcat it will fall under the context of the name of the WAR file. So in your case, the context becomes simplegrailsapp-0.1. You can:
rename your war file to whatever you want the context to be before you deploy it
define a Context for the application in Tomcat
If you go with defining a context this can be added to the WAR file by creating a META-INF folder in the web-app folder of your project and creating the context.xml file there.

Grails Config: include another config file

I have in my main config something like:
grails.config.locations = ["file:grails-app/config/Jawr.groovy"].
When running the application with grails run-app, everything is OK.
But, on deployment (creating the war archive) this does not work anymore, as the file "Jawr.groovy" is not kept anymore on the filesystem (it should be only in the war).
Do you have a solution for that? Hw do you include external files into the grails main configuration file?
Thanks.
Okay, a few things here.
First, because you don't have a leading slash on your config path, that is a path relative to who knows where. I played with this in Tomcat, and that path ends up being relative to the working directory you were in when starting the Tomcat server. If you start Tomcat, shut it down, change directories, then start it again, you are going to get two different config paths.
Second, the grails-app directory only exists within the source tree of your Grails project. The structure of an unpacked WAR file is more like the web-app folder of your Grails source tree, with folders like WEB-INF, META-INF, js, images, etc.
Third, you probably want to avoid putting your externalized config file inside the folder of your webapp. The next time you deploy your app, that configuration is going to get wiped away with the old version of the app. One of the points of the externalized config is so that you can redeploy without having to reconfigure.
A simple, but less than ideal, solution would be to use a static, fully qualified path, like /etc/yourApp/conf.groovy, then put that in the documentation. There is also a plug-in that handles this.
http://www.grails.org/plugin/external-config
I haven't used it, but the description makes it sound like it does sensible things.
see this: https://stackoverflow.com/questions/6341117/is-it-possible-that-grails-xxconfig-groovy-as-a-script-no-compile
Then I put it into /shared, and
modify:
//Config.groovy
grails.config.locations =
["file:shared/TZLibConfig.groovy"]
//BuildConfig.groovy
grails.war.resources = { stagingDir, args ->
copy(todir: "${stagingDir}/WEB-INF/shared"){
fileset(dir:"shared",includes:"**")
}
}
In my work, our team often use a system properties to save the path to the config file (often in home folder of the user running the app - for privilege sake). Then we manually copy the config file into that path
To identify that it's the production environment, we use the following code in Config.groovy:
if (System.properties["${appName}.config.location"]) {
grails.config.locations = ["file:" + System.properties["${appName}.config.location"]]
}
This article suggests allowing the user to specify the location of the config file as an environment variable or as a java property --- meaning you can simply specify it with -D on the command-line. This can be used in addition to all the other methods.

Resources