Search code examples
jrubywarbler

Using JRuby Warbler, is it possible to generate a WAR that can learn its RAILS_ENV from an environment variable?


Warbler wants you to specify a RAILS_ENV when creating the WAR. This is then put inside the web.xml of the generated WAR.

However, if you could create a wAR that learned its RAILS_ENV from the environment, you could create one WAR that could be used for staging or production - in other words, a superior management regime where a WAR can be tested and then deployed without being changed.


Solution

  • JRuby-Rack is already set up to read from RAILS_ENV before what gets put in web.xml, so that part is golden. The only thing you need to defeat is this rails.erb template that gets merged into a META-INF/init.rb inside the war file:

    ENV['RAILS_ENV'] = '<%= config.webxml.rails.env %>'
    

    There isn't a real good way to do this at the moment, but you can override the Warbler::Jar#add_init_file as follows at the top of your config/warble.rb to remove the Rails template:

    class Warbler::Jar
      alias_method :orig_add_init_file, :add_init_file
      def add_init_file(config)
        config.init_contents.delete("#{config.warbler_templates}/rails.erb") if config.init_contents
        orig_add_init_file(config)
      end
    end