无法从 JRuby gem 访问 jar 依赖项

发布于 2024-12-25 20:19:32 字数 1109 浏览 0 评论 0原文

我在本地系统上创建了一个用于文本挖掘的 gem,它依赖于 用于分类任务的外部 Java 库。我正在尝试驾驭 这里通过 JRuby 发挥 Java 的力量。目录结构如下:

- classifier/
 * classifier.gemspec
 * Gemfile
 * Gemfile.lock
 * lib/
     * classifier/
         * version.rb
         * sample_classifier.rb
     * classifier.rb
     * java/
         * sample_lib.jar
         * another_sample_lib.jar
         * yet_another_sample_lib.jar
 * Rakefile

我将这些 jar 加载到 lib/classifier.rb 中,因为

Dir["java/*.jar"].each{|jar| require jar}

我已将此 gem 添加到 Git 存储库并在 我的 Rails 3.0.9 项目的 Gemfile。但是,我的sample_classifier.rb 无法找到从以下任何 jar 中“导入”的任何类 lib/java

如果我将 lib/java 复制到我的 Rails 应用程序

lib/ directory

并在 sample_classifier.rb 中添加以下内容,事情就会起作用:

Dir["lib/java/*.jar"].each{|jar| require jar}

但是,我认为泄露 gem 不是一个好的做法 我的 Rails 应用程序中的依赖项。有更好的方法来实现这一目标吗?我 四处寻找与 gem 捆绑 jar 文件,但我发现了结果 完全相反,即将 gem 捆绑在 jar 文件中。我可以添加吗 这些 jar 到 Rails 的 config.autoload_path 或类似的文件时加载它们 应用程序启动?捆绑 jar 依赖项是一个好习惯吗 与宝石?

我确信有一种更干净的方法可以做到这一点。

I have created a gem on my local system for text mining which relies on
external Java libraries for classification tasks. I'm trying to harness
the power of Java through JRuby here. Directory structure is as follows:

- classifier/
 * classifier.gemspec
 * Gemfile
 * Gemfile.lock
 * lib/
     * classifier/
         * version.rb
         * sample_classifier.rb
     * classifier.rb
     * java/
         * sample_lib.jar
         * another_sample_lib.jar
         * yet_another_sample_lib.jar
 * Rakefile

I'm loading these jars in lib/classifier.rb as

Dir["java/*.jar"].each{|jar| require jar}

I have added this gem to a Git repository and referenced it in the
Gemfile of my Rails 3.0.9 project. However, my sample_classifier.rb
fails to locate any classes 'import'ed from any of the jars under
lib/java.

Things work if I copy lib/java to my Rails application

lib/ directory

and add the following in sample_classifier.rb:

Dir["lib/java/*.jar"].each{|jar| require jar}

However, I don't think it's a good practice to spill the gem's
dependencies in my Rails app. Is there a better way to achieve this? I
looked around for bundling jar files with a gem but I found results for
exactly the opposite i.e. bundling gems within a jar file. Can I add
these jars to Rails' config.autoload_path or similar to load them when
the application starts? Is it a good practice to bundle jar dependencies
with the gem?

I'm sure there's a cleaner way to do this.

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

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

发布评论

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

评论(2

君勿笑 2025-01-01 20:19:32

我是否建议使用 LockJar 管理你的 Jars 会更容易。只需安装 gem

gem install lock_jar

创建一个 Jarfile

创建一个 Jarfile Jar 依赖项列表:

jar "com.tobedevoured:example1:0.0.1"
jar "com.tobedevoured:example2:0.0.2"
jar "com.tobedevoured:example3:0.0.3"

最简单的 jar 表示法是groupId:artifactId:version。其他支持的变体有groupId:artifactId:type:versiongroupId:artifactId:type:classifier:version

生成 Jarfile.lock

传递依赖项被解析、下载并生成到 Jarfile.lock命令行:(

lockjar lock

您也可以从 Ruby 运行它,如 LockJar.lock)

访问您的 Jars

Jarfile.lock 包含所有 Jar 依赖项信息。现在,您可以通过以下方式将 Jarfile.lock 中的 jar 加载到类路径:

LockJar.load

使用 Gem 打包时,您需要确保 Jarfile.lock 中的 Jars 已下载with:

LockJar.install

确保您请求的 Jarfile.lock 与 Gem 的安装目录相关。以下是一个示例帮助程序:

module Example
  def self.installHelper
    # get jarfile relative the gem dir from lib/example.rb
    lockfile = File.expand_path( "../../Jarfile.lock", __FILE__ ) 

    LockJar.install( :lockfile => lockfile )
  end

  def self.loadHelper
    # get jarfile relative the gem dir from lib/example.rb
    lockfile = File.expand_path( "../../Jarfile.lock", __FILE__ ) 

    LockJar.load( :lockfile => lockfile )
  end
end

您还可以将 Gem 设置为 下载Gem 安装时的 Jar 依赖项,无需调用 LockJar.install

Might I suggest that managing your Jars with LockJar would be easier. Just install the gem

gem install lock_jar

Create a Jarfile

Create a Jarfile that contains a list of your Jar dependencies:

jar "com.tobedevoured:example1:0.0.1"
jar "com.tobedevoured:example2:0.0.2"
jar "com.tobedevoured:example3:0.0.3"

The simplest jar notation is groupId:artifactId:version. Other supported variations are groupId:artifactId:type:version and groupId:artifactId:type:classifier:version.

Generating a Jarfile.lock

The transitive dependencies are resolved, download, and generate to a Jarfile.lock with the command line:

lockjar lock

(You can also run it from Ruby as LockJar.lock)

Accessing your Jars

The Jarfile.lock contains all the Jar dependency information. Now you can load your jars from the Jarfile.lock to the classpath with:

LockJar.load

When packaging with a Gem, you will need to ensure the Jars from Jarfile.lock have been downloaded with:

LockJar.install

Make sure you request the Jarfile.lock is relative to the Gem's installed dir. Here is an example helper:

module Example
  def self.installHelper
    # get jarfile relative the gem dir from lib/example.rb
    lockfile = File.expand_path( "../../Jarfile.lock", __FILE__ ) 

    LockJar.install( :lockfile => lockfile )
  end

  def self.loadHelper
    # get jarfile relative the gem dir from lib/example.rb
    lockfile = File.expand_path( "../../Jarfile.lock", __FILE__ ) 

    LockJar.load( :lockfile => lockfile )
  end
end

You can also setup your Gem to download the Jar dependencies when the Gem installs, removing the need to call LockJar.install.

茶底世界 2025-01-01 20:19:32

使用绝对路径,如下所示:

jars_dir = File.join(File.dirname(__FILE__), 'java')
for jar in Dir["#{jars_dir}/*.jar"]
  require jar
end

Use absolute paths like so:

jars_dir = File.join(File.dirname(__FILE__), 'java')
for jar in Dir["#{jars_dir}/*.jar"]
  require jar
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文