How can I use Ruby code in a buildr project?
Asked Answered
B

2

5

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?

Bezel answered 1/8, 2011 at 9:34 Comment(0)
V
7

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
Vipul answered 1/8, 2011 at 17:27 Comment(2)
Okay, that does work for running it in buildr. But when I package as a jar and try to run, it says that it fails to load the Main class attribute from the manifest file.Bezel
This doesn't work for me. Does this example pertain to a specific version of jruby?Brochure
E
1

You can install buildr to use JRuby.

Ebarta answered 1/8, 2011 at 9:41 Comment(1)
That link explains how to get buildr to use JRuby, not how to use buildr to build JRuby projects.Esposito

© 2022 - 2024 — McMap. All rights reserved.