Search code examples
rubyscalaclojurebuildr

How can I use Ruby code in a buildr project?


How can use Ruby in a buildr project?

I've used Ruby, JRuby, Java, and Clojure in quite a few separate projects. I'm currently working on a simulation app in my standard Ruby that I wanted to try to do with a Clojure backend (I do enjoy the functional code), and JRuby gui and test suite. I could also see using say, Scala, for the backend in a different project in the future.

I think I'm going to give buildr (http://buildr.apache.org/) a try for my project, but I noticed that buildr doesn't seem to be set up to use JRuby code inside the project itself! This seems a little silly, seeing as the tool is meant to unify the common JVM languages and is built in ruby.

Does anyone know how you would accomplish this, short of including the outputted jar in a distinct, ruby only project?


Solution

  • You're not very specific about how you want to use your (J)Ruby code so I'll give you one example out of many possible approaches.

    Let's create a directory structure and put Java and Ruby files in it,

    .
    ├── buildfile
    └── src
        └── main
            ├── java
            │   └── Foo.java
            └── ruby
                └── app.rb
    

    with the content of Foo.java as follows,

    public class Foo {
      public int bar() {
        return 42;
      }
    }
    

    and a simple app.rb startup script,

    puts "Foo.bar is #{Java::Foo.new.bar}"
    

    Your buildfile would look somewhat like,

    VERSION_NUMBER = "1.0.0"
    
    repositories.remote << "http://www.ibiblio.org/maven2/"
    
    JRUBY = "org.jruby:jruby-complete:jar:1.6.3"
    
    define "ruby-example" do
      project.version = VERSION_NUMBER
      project.group = "org.example"
    
      run.with JRUBY
      run.using :main => ['org.jruby.Main', _(:src, :main, :ruby, "app.rb")]
    end
    

    (the run task is documented at http://buildr.apache.org/more_stuff.html#run)

    and you can now run your application by typing,

    $ buildr run
    

    and get the following output,

    $ buildr run
    (in /home/boisvert/tmp/ruby-example, development)
    Foo.bar is 42
    Completed in 1.884s