Running rake task from within war file
Finally found something that works.... i first tried
java -jar lib/jruby-complete-1.6.7.jar -S rake db_migrate[1]
which was working fine on my personal machine but giving me something like the message below on production
rake aborted!
cannot load Java class com.mysql.jdbc.Driver
Tasks: TOP => db_migrate
(See full trace by running task with --trace)
this was because i was using gems like sequel, logger etc inside my rake task.... i head those installed on my machine but not on production machine.... installing those gems on production was not an option.... so i installed the gems required in the rake task in a separate directory and converted it into a jar file( http://blog.nicksieger.com/articles/2009/01/10/jruby-1-1-6-gems-in-a-jar)... this command finally worked...
java -jar lib/jruby-complete-1.6.7.jar -rlib/mygems.jar -S rake db_migrate[1]
point to note: no matter where you place the jar file, warbler 'll always send this to lib directory although you 'll still see a dummy jar file at the original location...
i think the solution can be a bit neater if worked out in a couple of ways, although haven't tried this....
i>by including the gem files in jruby-complete-1.6.7.jar itself as mentioned in the blog mentioned above...
java -jar lib/jruby-complete-1.6.7.jar -S rake db_migrate[1]
should work then...
ii>by writing some kind of a manifest file and include it in the mygems.jar to make this run independently... if this happens
java -jar myapp.jar -S rake db_migrate[1]
should work
How to run rake tasks with warbler with -cp option
java -jar
does look for the main-class in the specified archive
java -cp
does only add a .jar to class-path
... it doesn't know what main-class to run you will need to set it manually
e.g. java -cp app.war WarMain -S ...
Running Rake Tasks under Tomcat and JRuby
Turns out this command works
java -jar /var/lib/jruby/jruby-complete-1.5.6.jar -S rake db:migrate RAILS_ENV=staging
as long as I download the jruby-complete jar and make sure I have the ENV:
RUBYLIB=$JRUBY_HOME/lib/ruby/site_ruby/1.8
GEM_HOME=$JRUBY_HOME/lib/ruby/gems/1.8
Executing rake tasks on an exploded war on tomcat without jruby being installed
Stay tuned. I hope to have this capability in Warbler 1.4. Jake Goulding, a community member, has been doing some great work on this.
Until then, a typical approach would be to ensure all your Rake and database scripts are present in the war file, then just unpack it somewhere, cd to WEB-INF
inside the unpacked war, and run something like java -cp lib/jruby-core*.jar:lib/jruby-stdlib*.jar org.jruby.Main -S rake -T
.
Load rake files and run tasks from other files
You can either put your tasks into rakelib/
folder which rake
loads by default or add a specific folder in your Rakefile
via:
Rake.add_rakelib 'lib/tasks'
A General Method to be invoked before every rake execution
This seems to be a bit awkward, But it may help others.
Rake.application.top_level_tasks
will return an array of information including Rake name and its arguments.
Reference attached below.
pry(main)> a = Rake.application.top_level_tasks
=> ["import_data[client1,", "data.txt]"]
Related Topics
Replacing an Element in Nested Array Ruby
How to Strip Commas from Float Input
Converting Colors (Not Images) with Imagemagick
"Private Method 'Gets' Called for "Parker":String
How to Make the Say Command Echo a Variable Value in a Script
Ruby: Calling Class Method from Instance
How to Initialize an Array in One Step Using Ruby
How to Create Activerecord Style Validations Outside of Activerecord
How to Convert a Large Gem to Standalone Rails App
How to Set Text Area Properties Inside Controller Page Using Ror
Split String into Equal Slices/Chunks