Ok so, the sales pitch. You have created the worlds greatest application, (not difficult with Grails) and you are on top of the world! Then suddenly, the emails you have configured to send out every minute to remind you how great you are, have stopped! What the???? The mail configuration settings have changed???? Uh oh, we are going to have to redeploy the application?? I am in the airport! Uploading my 30MB war file is going to take hours... weeks.... DAYS! I haven't tagged the code and the war might contain untested code!!!!
Relax... breathe... there is an alternative!
External configuration files in Grails allow you to extract the tidbits from Config.groovy that you feel are important. Things like the mail configuration that can change at any time. Others like your datasource password which you don't really want checked into git, subversion or whatever version control you may be using!
Enabling external configuration files in your Grails application is as easy as uncommenting a few lines of code in the top of your Config.groovy, the one's that look like this :
// grails.config.locations = [ "classpath:${appName}-config.properties",
// "classpath:${appName}-config.groovy",
// "file:${userHome}/.grails/${appName}-config.properties",
// "file:${userHome}/.grails/${appName}-config.groovy"]
For this example, let's work with this simple case :
grails.config.locations = ["file:${userHome}/.grails/${appName}Config.groovy"]
Here we have defined an external config that will sit in a hidden grails folder in the user's home directory. userHome is a variable added to the binding of Config.groovy at runtime and works on windows and unix based boxes alike. Similarly, appName is also a variable that is added to the binding at runtime containing the name of your Grails app.
This is all well and good but how does it work in the real world??? A very real scenario is that you will want to use external Config files in production and use the regular Config.groovy during development. No one wants to navigate outside their Grails application directory during development, we are not savages after all!
So..... how do we configure grails to look at an external configuration file in production and still have the flexibility of using Config.groovy on development?? Well you can use the environments closure in Config.groovy like so :
environments {
production {
//all my production configs are externalized
grails.config.locations = ["file:${userHome}/.grails/${appName}Config.groovy"]
}
development {
//the dev environment is none the wiser!
grails {
mail {
host = "mail.server.com"
'default' {
from = "mail@me.net"
}
port = 25
username = "mail@me.com"
password = "password"
props = ["mail.smtp.auth": "true",
"mail.smtp.socketFactory.port": "25",
//"mail.smtp.socketFactory.class":"java.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback": "false"]
}
}
}
test {
grails.gorm.failOnError = true
}
}
You can also declare settings that apply to all environments outside of this closure. This has the added bonus of reducing the 'noise' in your production config which should be kept as clean and as crisp as possible.
Now thinking back to our imaginary scenario at the beginning, our jet set developer can now just log into the server, change the offending setting and bounce the server! (Another step would be to database the settings to prevent bouncing but this is slightly off topic.)
Huzzah! Now if you need to change db password, you do not need to redeploy a new war!
It is also possible to refactor your dataSource config options to your external file... something along these lines will work :
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = "password"
url = "jdbc:mysql://localhost/pretty_db"
}
Huzzah! Now if you need to change db password, you do not need to redeploy a new war!
Honestly, one of the more convenient advantages of doing this is the ability to work with the logging of your application in production. This can be a pain when you first deploy to an app server on a production server away from the cushy dev environment that Grails provides!
It is worth noting too that you should set your external files up with permissions of 600, the owner of the file being the user that you run tomcat or your other container with.
So one simple change has got us :
- Ability to change Config options without having to build a war
- Separated our Datasource options from our application... I assume the DBA's will thank you!
- Tuning and changing logging has now become a lot easier
- Your war can now be transfered to different servers with different file systems and all you have to do is change a config file...
In these recessionary times, you cannot argue with that value! Thanks to Tim Berglund for showing how easy this was at his talk at SpringOne2Gx this year :)
This is a great post on using environment variables to achieve this :
UPDATE : It looks like someone has created a plugin that allows you to reload the external config without a restart (I have not used this but should be reliable :) ):
Hope this saves someone the hassle of re-warring for silly reason... it is quite the pain!
John :)
Spectacular. Best blog ever.
ReplyDeleteMy ex-boyfriend dumped me one week ago after I accused him of seeing someone else and insulting him. I want him back in my life but he refuse to have any contact with me. I was so confuse and don't know what to do, so I reach to the Internet for help and I saw a testimony of how a spell caster help them to get their ex back so I contact the spell caster and explain my problem to him and he cast a spell for me and assure me of 2 days that my ex will return to me and to my greatest surprise the third day my ex came knocking on my door and beg for forgiveness. I am so happy that my love is back again and not only that, we are about to get married. Once again thank you Dr Ojoka. You are truly talented and gifted. Email; dr.ojokspelltemple@gmail.com or Whats App +2348144172934 is the only answer. He can be of great help and I will not stop publishing him because he is a wonderful man
DeleteMy ex-boyfriend dumped me one week ago after I accused him of seeing someone else and insulting him. I want him back in my life but he refuse to have any contact with me. I was so confuse and don't know what to do, so I reach to the Internet for help and I saw a testimony of how a spell caster help them to get their ex back so I contact the spell caster and explain my problem to him and he cast a spell for me and assure me of 2 days that my ex will return to me and to my greatest surprise the third day my ex came knocking on my door and beg for forgiveness. I am so happy that my love is back again and not only that, we are about to get married. Once again thank you Dr Ojoka. You are truly talented and gifted. Email; dr.ojokspelltemple@gmail.com or Whats App +2348144172934 is the only answer. He can be of great help and I will not stop publishing him because he is a wonderful man
Great blog, but is their a way to protect the database username and password?
ReplyDeleteGreat and useful man
ReplyDeleteHi, where do you recommend I should put that external config file location? I think it should be inside the app's folder but how do I do that?
ReplyDeleteYou seem to reccommend using the internal config for development, but doing so does place passwords in the git repository. It is admittedly inconvenient to place development config info in an external file, especially when working with a team, but I see no other alternative. Is there a better way?
ReplyDeletegood example
ReplyDeleteThanks, Great Blog. Reloadable config plugin is not updated for a long time, so I found this one https://grails.org/plugin/external-config-reload
ReplyDeleteHello, i am from USA, I want to share this great testimony about how Dr.Agbazara helped me bring back my ex lover, During my search for solution i came in contact with Dr.Agbazara details and through his help my lover came back to me within 48 hours. So with these i am so bold to advise anyone seeking for a way to get there lover back to contact Dr.Agbazara on WhatsApp: { +234 810 410 2662 } or via email at: { agbazara@gmail. com } I am so happy at least myself and my lover are back to each other again and going to spend the New Year celebration together Thank to Dr.Agbazara once again....
ReplyDelete
ReplyDeleteI'm so excited my broken Marriage has been restored my ex husband is back after he left me and our 2kids for another woman. i was so happy to met Dr. ben how he help many people to bring there Lover back so i contact him to help me too. that was how Dr. ben help me to bring my husband back.. A big thank to you Dr. ben because I never thought my ex Husband will be back to me so quickly with your spell. You are the best and world greatest. if you are here and you need your Ex Lover back or your husband moved to another woman, do not cry anymore, contact this powerful spell caster now. Here’s his contact:
Email him at: drbenspellcaster@gmail.com You can also Call/WhatsApp:+2348151642717.