Can I specify that a gem is for JRuby only?

Is there something I can put in my .gemspec to indicate that the gem
will
only work with JRuby? I’m working on a gem that wraps Java libraries,
and so
requires JRuby. I couldn’t find any information on this. It seems like
you
can only specify platform and (numeric) ruby version in the gemspec.

I’ll of course document this requirement, but it would be nice if the
gem
simply fails to install with a reasonable error (or is not even
available
for install) with MRI Ruby. If there’s nothing I can do in .gemspec,
maybe I
can do something else to make the installation fail?

-Adam

you can “use” the gem-extension mechanism to execute ext/extconf.rb
(defined in gemspec) on installation where you can do your checking
and failing if needed.

  • Kristian

Hi,

On Jun 11, 2011, at 7:05 PM, Adam M. wrote:

Is there something I can put in my .gemspec to indicate that the gem will only
work with JRuby? I’m working on a gem that wraps Java libraries, and so requires
JRuby. I couldn’t find any information on this. It seems like you can only specify
platform and (numeric) ruby version in the gemspec.

spec.platform = ‘java’ means JRuby only.

Rhett

While we’re on the subject of gems, does anyone have any suggestions for
how to build a gem that has custom JVM options for the binary?

E.g., redcar must be run with -J-XstartOnFirstThread, and it’s
impossible to set that option from inside a running JVM, so I’d like to
replace the default Rubygem bin/redcar file with a custom one that is a
shell script that runs “jruby -J-XstartOnFirstThread”…

Any ideas?

Thanks Rhett. I think that’s exactly what I’m looking for. The
rubygems.orgfolks suggested the same thing.

I’ll keep Kristian’s extconf.rb suggestion in mind if I need to do
something
more complicated.

Adam

Daniel, I never tried this, but I’d imagine you can follow the
instructions
to add custom executables to your gem:

And make that a script that launches jruby with the desired JVM options.

Regarding my original question, I tried setting
spec.platform = “java”
in my gemspec. Now my gem installs with “java” as a suffix after the gem
version. But it continues to happily install into MRI ruby.

I suspect this setting more for handling differences between a JRuby and
an
MRI version of a single gem, when it supports both platforms. I wonder
if I
could intentionally release a broken non-java platform version of the
gem
that fails with a useful error message about the JRuby requirement…

I tried the gem-extension mechanism Kristian suggested but I couldn’t
get
that to work. It always says “failed to build native dependencies” even
when
I’m not doing anything in the ext script! I guess it’s expecting a
Makefile
to be outputted or something, but I don’t really know since it’s not
documented very well.

This “problem” isn’t a big deal, but I’m just curious if there’s a way
to do
this. I can’t be the only person building a JRuby-only gem that depends
on
Java libraries.

Adam

extconf.rb hack is the same as (I guess) the mkrf_conf that creates an
empty rakefile :slight_smile:

Anyway @Daniel I don’t think rubygems currently lets you specify the
command options used to launch your script, though it would be an
interesting feature request for it sometime. At least AFAIK.

On Thu, Jun 16, 2011 at 1:06 AM, Adam M. [email protected]
wrote:

Regarding my original question, I tried setting
spec.platform = “java”
in my gemspec. Now my gem installs with “java” as a suffix after the gem
version. But it continues to happily install into MRI ruby.

Actually, this worked the way I was hoping. There was a glitch that let
MRI
ruby install it on my dev machine, probably due to me publishing and
installing an earlier version of the gem that didn’t specify the java
platform, and maybe some local gem caching issue.

I had deleted the non-java versions of the gem from rubygems.org.
Tonight I
happened to try to install it with MRI ruby on another computer, and it
gave
this error:

ERROR: Could not find a valid gem ‘jsound’ (>= 0), here is why:
Found jsound (0.1.3), but was for platform java

So, cool. That’s exactly what I wanted.

So the lesson is, if you are making a java-only gem, set the platform to
java right away to avoid some confusion.

Adam

Thanks Adam, I’ll try that!

For the extconf.rb hack, which I have used before myself, you can use
the emulate_extension_install method from this guys code to fool
Rubygems into thinking that an extension has been generated:
http://rails-pwnage.rubyforge.org/svn/trunk/zerg_support/lib/zerg_support/gems.rb