Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created July 6, 2011 09:51
Show Gist options
  • Select an option

  • Save mindscratch/1066937 to your computer and use it in GitHub Desktop.

Select an option

Save mindscratch/1066937 to your computer and use it in GitHub Desktop.
rake task to build a WAR using warbler. The idea being that a YAML file has multiple 'production' configurations but you need to select one when building a WAR for a certain production environment. You can do that by running: rake build_war[some_environme
%YAML 1.1
---
! "defaults":
! "cache": &7000
! "username": ""
! "password": ""
! "servers": ["localhost:11211"]
! "development":
! "cache": *7000
! "prod-server-a"
! "cache":
! "username": "prod-user"
! "password": "prod-password"
! "servers": ["prod-a.acme.com:11211"]
! "prod-server-b"
! "cache":
! "username": "prod-user"
! "password": "prod-password"
! "servers": ["prod-b.acme.com:56555"]
require 'yaml'
def set_production_environment(source_environment, config, output_yaml)
config['production'] = config[source_environment]
File.open(output_yaml, 'w') do |out|
YAML.dump(config, out)
end
end
def get_usage_msg(config)
"Usage: rake #{t}[#{config.keys.join('|')}]"
end
desc "Build the WAR file"
task :build_war, :environment do |t, args|
CONFIG_FILE = 'config/app-config.yml'
environment = args[:environment]
# load config
config = YAML.load_file CONFIG_FILE
# check given 'environment' exists in config
raise get_usage_msg if (environment.nil? or environment.empty?)
raise "Invalid option=#{environment}. #{get_usage_msg(config)}" unless config.has_key? environment
# backup the original config file
cp CONFIG_FILE, CONFIG_FILE + '.bak'
`warble`
raise "failed to build WAR: #{$?.inspect}" unless $?.success?
mv "web.war", "myapp-#{environment}.war"
# restore the config
mv CONFIG_FILE + '.bak', CONFIG_FILE
end
@mindscratch
Copy link
Author

Say you want to build a 'production' WAR file using warbler but you need the production rails environment to use prod-server-b. Using the rake task above you would do something like this:

rake build_war[prod-server-b]

You would end up with a WAR file named: myapp-prod-server-a.war (assuming you used the rake file "as is", you should tweak it as necessary for your own project of course).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment