~Buildr Devs,

First I'd like to thank you all for such a cool project. As a Java developer
and Ruby lover I think this is the perfect project for me to contribute ;)

I know there's some interest in making buildr to run in JRuby, I think this
feature will be very important for the adoption of buildr, mainly because
some guys (like my clients) don't want/can install MRI on their machines.

Searching on the internet I couldn't find anything related to running on
JRuby, but I know there are other people interested on this, I'd like to
join that effort.

I've created a very simple prove-of-concept project to see what it takes to
get buildr running, and this is what i have up to now:
( You can download a tgz for this project from: )
  http://repo.or.cz/w/buildr.git?a=snapshot;h=908b11;sf=tgz

The tar is structured as follows:
  build              =>  a shell script to call jruby-complete and run
buildr
  hello-world     => A hello world application to be compiled/packaged
  lib/java           => a jruby-complete snapshot is provided here
  lib/ruby/gems => gems for buildr as jars.
     I know JRuby is able to install gems using
              --command gem install
     but doing this forces jruby to expand all the .rb on ~/.jruby, and
     I really like to get this running using jruby-complete.
  lib/ruby/bin     => The buildr launcher script is here.

To make it compile the hello-world project you must apply the attached patch
and copy/jar the content of buildr/lib into lib/ruby/gems/buildr-jruby.jar

The attached patch fixed JavaWrapper import when running on JRuby, this was
causing the javac tool not being run properly.

Although I was sucessful on making my hello-world proyect to compile, the
created package (jar) is corrupted. I guess this may be a bug on using
ruby-zip with JRuby.

So, this is what i've got. I'm planning to take a look at the package
issue next week or looking at running buildR specs from JRuby.

Best Regards,
-- 
vic

Quaerendo invenietis.
From cde9f11dd2cee383f0af6220cacb7b589faeb6db Mon Sep 17 00:00:00 2001
From: Victor Hugo Borja <[EMAIL PROTECTED]>
Date: Fri, 7 Dec 2007 14:02:12 -0600
Subject: [PATCH] Fix compilation when running under JRuby

---
 lib/java/java.rb |   34 ++++++++++++++++++++++++++++------
 1 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/lib/java/java.rb b/lib/java/java.rb
index 0ad9924..a6e3ecc 100644
--- a/lib/java/java.rb
+++ b/lib/java/java.rb
@@ -28,7 +28,7 @@ module Buildr
 
       def initialize() #:nodoc:
         @classpath = [Java.tools_jar].compact
-        if RUBY_PLATFORM == 'java'
+        if Java.jruby?
           # in order to get a complete picture, we need to add a few jars to 
the list.
           @classpath += 
java.lang.System.getProperty('java.class.path').split(':').compact
         end
@@ -39,7 +39,7 @@ module Buildr
             cp = Buildr.artifacts(@classpath).map(&:to_s)
             cp.each { |path| file(path).invoke }
 
-            if RUBY_PLATFORM == 'java'
+            if Java.jruby?
               cp.each do |jlib|
                 require jlib
               end
@@ -68,8 +68,8 @@ module Buildr
       end
 
       def import(jlib)
-        if RUBY_PLATFORM == 'java'
-          ::Java.send(jlib)
+        if Java.jruby?
+          ::Java.instance_eval(jlib)
         else
           ::Rjb.import jlib
         end
@@ -78,7 +78,7 @@ module Buildr
       def method_missing(sym, *args, &block) #:nodoc:
         # these aren't the same, but depending on method_missing while
         # supporting two unrelated systems is asking for trouble anyways.
-        if RUBY_PLATFORM == 'java'
+        if Java.jruby?
           ::Java.send sym, *args, &block
         else
           ::Rjb.send sym, *args, &block
@@ -184,6 +184,7 @@ module Buildr
           puts "Running apt" if verbose
           puts (["apt"] + cmd_args).join(" ") if Rake.application.options.trace
           Java.wrapper do |jw|
+            cmd_args = cmd_args.to_java_array(::Java.java.lang.String) if 
Java.jruby?
             jw.import("com.sun.tools.apt.Main").process(cmd_args) == 0 or
               fail "Failed to process annotations, see errors above"
           end
@@ -221,7 +222,8 @@ module Buildr
           puts "Compiling #{files.size} source files in #{name}" if verbose
           puts (["javac"] + cmd_args).join(" ") if 
Rake.application.options.trace
           Java.wrapper do |jw|
-            jw.import("com.sun.tools.javac.Main").compile(cmd_args) == 0 or
+            cmd_args = cmd_args.to_java_array(::Java.java.lang.String) if 
Java.jruby?
+            jw.import("com.sun.tools.javac.Main").compile(cmd_args) == 0 or 
               fail "Failed to compile, see errors above"
           end
         end
@@ -272,6 +274,7 @@ module Buildr
           puts "Generating Javadoc for #{name}" if verbose
           puts (["javadoc"] + cmd_args).join(" ") if 
Rake.application.options.trace
           Java.wrapper do |jw|
+            cmd_args = cmd_args.to_java_array(::Java.java.lang.String) if 
Java.jruby?
             jw.import("com.sun.tools.javadoc.Main").execute(cmd_args) == 0 or
               fail "Failed to generate Javadocs, see errors above"
           end
@@ -356,6 +359,11 @@ module Buildr
         File.join(home, "bin", name)
       end
 
+      # return true if we a running on c-ruby and must use rjb
+      def rjb?; RUBY_PLATFORM != "java"; end
+      # return true if we are running on jruby
+      def jruby?; RUBY_PLATFORM == "java"; end
+
   protected
 
       # :call-seq:
@@ -430,3 +438,17 @@ module Buildr
   end
 
 end
+
+
+if Buildr::Java.jruby?
+  
+  # Convert a RubyArray to a Java Object[] array of the specified element_type
+  class Array
+    def to_java_array(element_type)
+      java_array = ::Java.java.lang.reflect.Array.newInstance(element_type, 
self.size)
+      self.each_index { |i| java_array[i] = self[i] }
+      return java_array
+    end
+  end
+
+end
-- 
1.5.2.5

Reply via email to