如何在构建项目中使用 Ruby 代码?

发布于 2024-11-27 07:13:33 字数 376 浏览 1 评论 0原文

如何在 buildr 项目中使用 Ruby?

我在很多单独的项目中使用过 Ruby、JRuby、Java 和 Clojure。我目前正在标准 Ruby 中开发一个模拟应用程序,我想尝试使用 Clojure 后端(我确实喜欢功能代码)以及 JRuby GUI 和测试套件。我还可以看到将来在不同的项目中使用 Scala 作为后端。

我想我要为我的项目尝试一下 buildr (http://buildr.apache.org/),但我注意到 buildr 似乎没有设置为在项目本身内使用 JRuby 代码!这看起来有点愚蠢,因为该工具旨在统一常见的 JVM 语言,并且内置于 ruby​​ 中。

有谁知道如何实现这一点,而不是将输出的 jar 包含在一个独特的、仅限 ruby​​ 的项目中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

a√萤火虫的光℡ 2024-12-04 07:13:33

您对于如何使用 (J)Ruby 代码并不是很具体,因此我将从多种可能的方法中为您提供一个示例。

让我们创建一个目录结构,并将Java和Ruby文件放入其中,

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

Foo.java内容如下,

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

以及一个简单的app.rb启动脚本,

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

您的buildfile 看起来有点像,

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

run 任务记录在 http://buildr.apache.org/more_stuff.html#run

,您现在可以通过键入来运行您的应用程序,

$ buildr run

并获得以下输出,

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

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
茶花眉 2024-12-04 07:13:33

您可以安装构建器来使用 JRuby

You can install buildr to use JRuby.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文