Warbler can package a Rails app as a .war.
But If I distribute my Rails app as a .war, will I be able to run my
rake tasks?
How will I do a rake db:migrate from a Rails .war?
Cheers,
rad
It's possible that someone somewhere has addressed this, but going only on what I know about JRuby and Java...
The war file is a special package suitable for a Java web server. Rake, not even the JRuby version of it to my knowledge, is designed to know anything about its content. (Even JRuby doesn't; it's the warbler gem that does.)
Since war and jar files are simply zip files with specially defined content, you could always have a script that unzipped it (even to a temporary directory you subsequently delete, if the unzip time isn't prohibitive), and then ran the rake task.
Linux and Mac OS systems have convenient zip/unzip command line utiltiies built-in or easily available. The jar command, packaged with Java, may work too, using the "-x" option. There may be an unzip rake task available, I don't know -- if so, that would be nice so that you would have better handling of error conditions than merely the exit code and console output.
The more difficult question, I think, is what to do if you can't install ruby, rake, and all the gems necessary to do the migration on the Java web server machine (for political/bureaucratic reasons, for example). Warbler packs the jruby.jar file into the war file, I believe, so you could always run JRuby as:
java -jar (location of jruby.jar file in exploded dir) (script to run) (script's args)
...but that's just to run JRuby at all, before addressing rake or the gems.
Is this an issue for you, or will you have Ruby or JRuby installed on this machine?
You could always copy the war file to any other machine that has Ruby, rake, and gems, unzip the war file, and run the migration on that other machine.
- Keith
My objective is to distribute just the .war or .jar file to the
end-user, as a form of code obfuscation.
Looks like it's not (yet) supported:
http://stackoverflow.com/questions/7493232/execute-rake-task-from-war
https://github.com/jruby/warbler/issues/10
rad
Interesting. I tried compiling a JRuby source file, and running
JD-GUI (a disassembler on it), to see just how obfuscated it was. It
was pretty incomprehensible. (The source is at
https://raw.github.com/keithrbennett/multilanguage-swing/5744fde82696f1ed1ede8396301a225868f8e2bd/src/FrameInRuby.rb,
from a JRuby article I wrote a while back
(http://krbtech.wordpress.com/2009/02/26/jruby-a-better-language-for-the-java-virtual-machine/).
I ran jrubyc on it, then ran the class file through JD-GUI
(http://java.decompiler.free.fr/?q=jdgui). I posted the decompiled
file at https://gist.github.com/1498963.
Having that, you could find the location of the rakefile's in the war
file, or even have code that writes them to a temporary directory for
use by the rake class. (Or, rake may support providing the script as a
string rather than having to read it from a file.)
I was hoping that one could call rake in Ruby rather than as an
executable. I looked at the rake source code, and was initially
encouraged. The rake executable is at
https://github.com/jimweirich/rake/blob/master/bin/rake and shows that
it is merely running:
require 'rake'
Rake.application.run
However, it relies on optparse for its parameters, and optparse gets
the values from the command line. So I guess rake is really designed
for being run as a standalone executable.
If you can run the executable from within the webapp, it might work.
There may be security permissions issues though -- the webapp may be
restricted in how it can interact with the OS. You might be able to
locate the rake files in the war file (resources such as image files
are accessible, so you could probably make rakefiles accessible too --
or have the rakefiles generated in a temp directory at runtime).
By the way, there are sometimes differences in byte codes for the
various Java versions. I'd recommend running the same version of Java
when you compile the Ruby code as the customer will be running in
their web server.
Cheers,
Keith