JRuby project structure and jar creation - ruby

I have to create a JRuby jar file for my project. Below I provided details about my directory structure and files.
Top level directory - Project1
Under Project1 – I have bin, lib, src folders
Under Project1/bin – I have wrapper shell script from where I am calling jruby jar.
Under Project1/lib – I have jruby-complete-1.6.7.2.jar and ojdbc6.jar
Under Project1/src – I have lib and tool folders
Under Project1/src/lib – I have main.rb file and utilfolder
Under Project1/src/lib/util - I have 2-ruby scripts which are getting called in main.rb.
Under Project1/src/tool- I have Tool.java from where I call main.rb.
Now I have couple of questions -
Do I need to bundle all the gems which I used in my ruby scripts (for example: colorize, socket, net/ssh, etc)?
How do I create a JRuby jar? I saw the following posts on stackoverflow before posting my question but I got confused and kind of not able to figure out from where to start. Please provide some guidance on this.

Below are the steps which worked for me to bundle all the gems into jruby-complete.jar.
Download the jruby-complete-latest_version.jar from http://jruby.org/download.
Verify which gems are included in the downloaded jar, say java -jar jruby-complete-latest_version.jar -S gem list.
To push the gems into jruby-complete-latest_version.jar you need to check for all the required runtime dependecies for that gem. Example: for net-scp you need to download net-ssh gem before.
Download the gems under the same directory where you have jruby-complete-latest_version.jar by using the following command:
java -jar jruby-complete-latest_version.jar -S gem install -i ./net-ssh net-ssh --no-rdoc --no-ri
java -jar jruby-completelatest_version.jar -S gem install -i ./net-scp net-scp --no-rdoc --no-ri
Now add the gems inside ruby-complete-latest_version.jar by using update file (uf) option for the jar file. Example:
jar uf jruby-complete-latest_version.jar -C net-ssh .
jar uf jruby-complete-latest_version.jar -C net-scp .
Check the gem list for the jar file to make sure all the gems are added successfully
java -jar jruby-complete-latest_version.jar -S gem list
Last check to make sure gems are loading successfully, run require statements on irb.
java -jar jruby-complete-latest_version.jar -S irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'net/scp'
=> true
After I have the jruby-complete_latest_version.jar file under lib I used ANT to build the jar for my project.
Again this solution will work for the smaller projects. For big projects Warbler will the best choice as suggested by #joelparkerhenderson.

Related

How to install gem to local path and then require it

I want to setup gem from rubygems.org to some subfolder of my project like "/gems" and then use it from script via require. Please help me resolve it.
The preferred way is to go with a standard scenario: you build a gem, with binaries, pack it and when used it will download all the dependencies itself, according to gemspec. If the above is for some reason non-acceptable for you, one of the options would be:
• You create a gem for your script;
• You specify a path where to install dependencies:
bundle install --path=vendor/gems
• Instead of require you use require_relative in your script because on destination machines there won’t be local bundle’s config file, pointing to the right folder;
• As your script is finished, you pack everything including gems.
The normal way to go in a ruby project is to setup a Gemfile and use bundler (see link for more info) to handle the gems your project requires, without having to think of where they are stored
However, if there really is no way around shipping an own gem-directory, e.g. because your productive system has absolutely no way to access the internet, you could make it this way:
Create a directory in the root of your project (e.g. 'gems').
Download and unpack the gems to that directory (or use the appropriate options of gem install to redirect the installation into that directory)
Create a ruby file in the root directory of your project with following constants:
PROJECT_DIR = __dir__
GEMS_DIR = File.join(PROJECT_DIR, 'gems')
Now you can require your gems link require File.join(GEMS_DIR, <gem_name>)
Nonetheless, you should really think about using bundler, if possible in any way.
EDIT: to install gem via gem install
Delete the PG data from your project's gems directory
uninstall global gem gem uninstall pg
Install pg again, but into your project's directory: gem install -i <path_to_projects_gems_dir> pg
Run you script again with the require pointing to the gem stated above
I solved it like this:
gem install -i ./ pg
ROOT = File.expand_path('..', __FILE__)
ENV['GEM_PATH'] = File.join(ROOT, './')
require 'pg'
puts 'Version of libpg: ' + PG.library_version.to_s

Locally install a gem built with platform=java | JRuby 1.7.18

It seems that the workflow that I use to build and install gems locally does not translate when the gemspec has platform = "java".
I am using Jruby 1.7.18, installed with rvm
$ which gem
/Users/USERNAME/.rvm/rubies/jruby-1.7.18/bin/gem
When I build a gem with the default platform, everything goes according to plan:
$ grep platform batching_core.gemspec
# spec.platform = "java"
$ gem build batching_core.gemspec
Successfully built RubyGem
Name: batching_core
Version: 0.1.0
File: batching_core-0.1.0.gem
$ gem install batching_core-0.1.0.gem
jar dependencies found on non-java platform gem - do not install jars
Successfully installed batching_core-0.1.0
1 gem installed
Note the success message of "Successfully installed batching_core-0.1.0"
But when my project requires me to use platform=java so that I can require some jar files. I can build the gem, but I cannot install it. The installation ends without the success message, and I cannot require the gem in irb:
$ grep platform batching_core.gemspec
spec.platform = "java"
$ gem build batching_core.gemspec
Successfully built RubyGem
Name: batching_core
Version: 0.1.0
File: batching_core-0.1.0-java.gem
$ gem install batching_core-0.1.0-java.gem
$
^^ Note the lack of output. No success message, no output at all.
$ irb
jruby-1.7.18 :001 > require 'batching_core'
LoadError: no such file to load -- batching_core
from org/jruby/RubyKernel.java:1071:in `require'
from /Users/USERNAME/.rvm/rubies/jruby-1.7.18/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55:in `require'
I tried the solution suggested here, but got the same result.
Creating a gem using Jruby and not Ruby
EDIT: Here's the project that causes the issue:
https://github.com/ValerieAnne563/so34754484
It looks to me like the gem does not contain the file you're trying to require. Can you check the contents to see if "batching_core.rb" is in the gem's lib dir?
As for the warning...I'm confused about your environment. This is a gem with a jar in it being installed on JRuby? Maybe it would clarify some things if you link to the project or post the gemspec.

Ruby script cannot load a gem installed via bundler

I am trying to include the ruby-mysql gem in my ruby script. I have installed the gem using bundler but when I run bundle exec ./mysql_connector, I receive the error ./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError). Can you please help me troubleshoot what the problem is?
What I did
Installed rails in my home directory.
I do not have root access to the server so I have installed rails in my local directory by following the instructions here:
http://www.r-bloggers.com/installing-ruby-on-linux-as-a-user-other-than-root/
Created a directory for my application.
My application resides in my home directory in a folder called connector. It has a Gemfile that looks like this:
source 'https://rubygems.org'
gem 'ruby-mysql'
Call bundle install.
Using ruby-mysql 2.9.14
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Add dependencies to my script. My script is in connector/mysql_connector and it reads:
#!/home/dcox/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'ruby-mysql'
Make script executable. I saw that you need to run bundle exec using an executable file, so I followed the instructions here to make my script executable: http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
Run the script. I execute using bundle exec mysql_connector and see:
/home/dcox/bin/mysql_connector:4:in `require': cannot load such file -- ruby-mysql (LoadError)
from /home/dcox/bin/mysql_connector:4:in `<main>'
Is it the $LOAD_PATH? After searching around for answers, I discovered a lot of SO answers as well as a blog post (https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-load-such-file/) that seem to suggest the problem is that the gem is not installed in a directory on the $LOAD_PATH. Here is what I see when I run $LOAD_PATH from IRB:
irb(main):002:0> $LOAD_PATH
=> ["/home/dcox/lib/ruby/site_ruby/2.1.0",
"/home/dcox/lib/ruby/site_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/site_ruby", "/home/dcox/lib/ruby/vendor_ruby/2.1.0",
"/home/dcox/lib/ruby/vendor_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/vendor_ruby", "/home/dcox/lib/ruby/2.1.0",
"/home/dcox/lib/ruby/2.1.0/x86_64-linux"]
Next I checked to see the location of ruby-mysql:
dcox#analytics1:~/connector$ bundle show ruby-mysql
/data/home/dcox/connector/vendor/bundle/ruby/2.1.0/gems/ruby-mysql-2.9.14
Obviously my connector/vendor/bundle path is not on the $LOAD_PATH. I could add it, but I have a feeling I am missing something simple here because bundler is supposed to work as long as you follow the instructions, right?
Any advice or help is very much appreciated! Thanks!!
If you just want to require this specific gem, require 'mysql' should work (e.g., https://github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10).
Your file should call Bundler.setup http://bundler.io/bundler_setup.html
Better yet, if you instead call Bundler.require(:default) it will setup and require all the gems in your Gemfile for you.

jruby/warbler: NoClassDefFound: Could not initialize org.jruby.ext.openssl.ASN1

I tried to use Warbler to make a war of a redmine installation.
I first installed JRuby (1.7.4), then download all gems with ''jruby -S gem install ...'', ''jruby -S bundle''.
I then installed warbler, and tried with or without specifying jruby-openssl in config.gems
org.jruby.exceptions.RaiseException: (LoadError) load error: openssl -- java.lang.NoClassDefFoundError: Could not initialize class org.jruby.ext.openssl.ASN1
I also tried with JRuby 1.7.3 and JRuby 1.7.1, although the .war file that is generated is almost the same size.
I understand that jruby-openssl is part of jruby now.
How can I make this work?
UPDATE: it works with jruby, without making a war. So, I guess warbler must do the packaging wrong.

How to bundle local gem dependancies in IronWorker

I have a Ruby IronWorker which depends on a private gem that isn't published to RubyGems.
Is there a way to merge this local mygemname-0.0.1.gem into my IronWorker in the .worker file?
I'm hoping to be able to specify something the following in myruby.worker:
gem 'mygemname', '>=0.0.1', :path=> 'vendor/bundle'
Currently this give the following error
.rvm/gems/ruby-1.9.3-p0/gems/iron_worker_ng-0.12.2/lib/iron_worker_ng/code/base.rb:79 :in `eval':
wrong number of arguments (3 for 2) (ArgumentError)
Hoping for defaults gives:
gem 'mygemname', '>=0.0.1'
Gives the following error
Could not find gem 'mygemname (>= 0.0.1) ruby' in the gems available on this machine.
Am I on the right track trying to get this to work via the .worker file? Or should I be looking into specifying a custom build step?
If your unpublished gem itself has dependancies, you need to do a little massaging to get things going. Here is a technique that works for me:
mygem.worker
runtime "ruby"
#Merge in an unpublished local gem
dir '../opensource-cli-tools/facebook_exporter', '__gems__/gems'
file '../opensource-cli-tools/facebook_exporter/mygem.gemspec', '__gems__/specifications'
#Merge in a custom build script to fetch the unpublished gem's dependancies
file "Gemfile"
file "install_dependancies.sh"
remote_build_command 'chmod +x install_dependancies.sh && ./install_dependancies.sh'
#Run the puppy!
exec "run.rb"
install_dependancies.sh
echo "Installing dependancies to __gems__/"
gem install bundler --install-dir ./__gems__ --no-ri --no-rdoc
bundle install --standalone --path ./__gems__
cp -R ./__gems__/ruby/*/* ./__gems__
rm -rf ./__gems__/ruby
echo "Fixing install location of mygem"
mv ./__gems__/gems/mygem ./__gems__/gems/mygem-0.0.1
As far as i know, git and local paths unsupported right now.
Here is way to manually include local gem:
Add these lines to .worker file:
dir '../vendor/bundle/mygemname', '__gems__/gems'
file '../vendor/bundle/mygemname/mygemname.gemspec', '__gems__/specifications'

Resources