How to bundle gems in a Java project?

Can anyone point me to a tutorial or guide on how to bundle up a set of
gems
into a Java project? For instance, in Maven, I can include the
jruby-complete.jar in my pom.xml, but since the wealth of the Ruby
community
is a ‘gem install’ away, I would like to include a gem or two in my
Java/Maven projects as well.

Anyone have any tips?

-Nick

On Mon, Jan 31, 2011 at 3:14 PM, Nick K. [email protected] wrote:

Can anyone point me to a tutorial or guide on how to bundle up a set of gems
into a Java project? For instance, in Maven, I can include the
jruby-complete.jar in my pom.xml, but since the wealth of the Ruby community
is a ‘gem install’ away, I would like to include a gem or two in my
Java/Maven projects as well.
Anyone have any tips?

You could try building a gem-jar 1 and including that in your
classpath or Maven project.

/Nick

I forgot about this feature. Man, that is slick!

jruby -S gem install -i ./http-gems httparty jruby-openssl --no-rdoc
–no-ri
jar uf jruby-complete-1.6.0.RC1.jar -C .\http-gems .
java -jar jruby-complete.1.6.0.RC1.jar -S irb

That is neat. To take it the last step, is there a way to make it so
that I
don’t have to pass the -S <parameter>, such that I could create an
executable jar that would just run the script by default?

-Nick (K)

So, if I create a .jar out of some gems, then install that to my Maven
repo
(and declare in my POM), that would be all I would need for those gems
to be
discoverable in a ScriptContainer?

-Nick (K)

Warbler 1.3.0 will support this. There’s a pre-release version on
Rubygems (install with --pre), and I hope to have 1.3.0 final released
soon.

There isn’t an easy way to do this in JRuby at this point, but you
could create your own main class that manipulates the String[] args
array and then calls org.jruby.Main and set your custom main class in
the jar’s manifest file.

/Nick

Theoretically, yes, though I haven’t tried it. Please report back if
you get it to work that way!

Yes! I had a bit of Maven trouble figuring out where to put my ruby
scripts, but it works!

-Nick (K)

Theoretically, yes, though I haven’t tried it. Please report back if
you get it to work that way!

/Nick

Could you describe what your solution looks like, Nick K? Thanks!

Sure. I created a quickstart maven
projecthttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
to
start. Then I used Nick S.'s blog post on bundling up gems in a
jarhttp://blog.nicksieger.com/articles/2009/01/10/jruby-1-1-6-gems-in-a-jar(mentioned
above). I placed that .jar into my src/main/resources folder and
specified
its dependency a bit differently:

... org.jruby jruby-complete 1.6.0.RC1 compile ruby http-gems 1.0.0 system

${basedir}/src/main/resources/http-gems-1.0.0.jar

For the Ruby classes, I put them in src/main/resources/ruby, and from
there
it was simply a matter of changing the App.java stub a bit to call the
script. This doesn’t use warbler, but it works simple enough without
it:

import java.net.URL;
import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;

/**

  • Hello world!

*/
public class App
{
public static void main( String[] args )
{
ScriptingContainer container = new ScriptingContainer();
URL ruby_http_app =
container.getClass().getClassLoader().getResource(“http-thing.rb”);
Object x = container.runScriptlet(PathType.CLASSPATH,
“ruby/http-thing.rb”);
System.out.println(x);
}
}

Now I have also stumbled on mkristian’s jruby maven plugins which I will
be
digging into a bit further. I wouldn’t be surprised if he didn’t figure
out
a much smoother way of doing all of this using a plugin:
https://github.com/mkristian/jruby-maven-plugins

-Nick K.

I would recommend starting a new thread to ask this question.

However, as I’m new to this as you appear to be, here are some resources
on
JRuby and Rails:

I also recommend the newly released book Using
JRubyhttp://pragprog.com/titles/jruby/using-jruby.
I’m only a few chapters into it, but this book is a serious goldmine of
information. Incredibly good stuff in there and absolutely worth
purchasing.

-Nick K.

Thanks, Nick. I didn’t know about systemPath in Maven, that’s handy :slight_smile:

Ultimately what we’re trying to do is create a jar that we can run our
rails
app out of. Something where we could do something like:

java -jar jruby-with-gems.jar -S rake test

or

java -jar jruby-complete.jar -rrails-gems.jar -S rake test

Ultimately we want to be able to host the app in a ScriptingContainer.
This
works perfectly fine with jruby-complete.jar in the classpath, and a
ScriptingContainer with jruby home set to a directory on disk will all
the
necessary gems but not when trying to pack the gems in a jar and run
without
a jruby home from the filesystem.

I tried both approaches (adding the gems to the jruby-complete jar and
using
a separate jar with just the gems in it) and neither seems to work when
trying to ‘-S rake test’ or just ‘-S rails MyApp’. Here’s a set of
commands
that doesn’t work that I would expect to work:

mkdir spike
cd spike
curl
http://jruby.org.s3.amazonaws.com/downloads/1.6.0.RC1/jruby-complete-1.6.0.RC1.jar>
jruby-complete.jar
mkdir -p META-INF/jruby.home/lib/ruby/gems/1.8
GEM_HOME=./META-INF/jruby.home/lib/ruby/gems/1.8 java -jar
jruby-complete.jar -S gem install rails --no-rdoc --no-ri
jar uf jruby-complete.jar META-INF
rm -rf META-INF
java -jar jruby-complete.jar -S gem list --local
jar tf jruby-complete.jar | grep /rails$
java -jar jruby-complete.jar -S rails MyApp

In particular, I don’t get why the last command (-S rails MyApp) fails
with
“No such file or directory – rails” when the next to last command
produces
output very similar to this:

$ find ~/javabin/jruby-1.5.6 ! -type d | grep /rails$
…/jruby-1.5.6/lib/ruby/gems/1.8/bin/rails
…/jruby-1.5.6/lib/ruby/gems/1.8/gems/rails-3.0.3/bin/rails
…/jruby-1.5.6/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/generators/rails/app/templates/script/rails

It looks like the -S flag doesn’t look inside gem bin directories in
jars
the same way it does in gem directories on the file system. I’ll
continue
investigating now but wanted to tap the group for knowledge.

Cheers!