无法从JRuby gem访问jar依赖项 [英] Unable to access jar dependencies from JRuby gem

查看:138
本文介绍了无法从JRuby gem访问jar依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地系统上创建了一个用于文本挖掘的gem,它依赖于
外部Java库来进行分类任务。我试图通过JRuby在这里利用
Java的强大功能。目录结构如下:

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

我在 lib / classifier.rb 中加载这些罐子为

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

我已将此gem添加到Git存储库并在
中引用它我的Rails 3.0.9项目的Gemfile。但是,我的sample_classifier.rb
无法在
lib / java 下的任何一个罐子中找到import的任何类。

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.

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

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

lib/ directory

并在<$ c中添加以下内容$ c> sample_classifier.rb :

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

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

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.

推荐答案

我建议用管理你的罐子吗? LockJar 会更容易。只需安装宝石

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

gem install lock_jar



创建一个Jarfile



创建 Jarfile ,其中包含您的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"

最简单的jar符号是 groupId:artifactId:version 。其他受支持的变体是 groupId:artifactId:type:version groupId:artifactId:type:classifier:version

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

传递依赖关系被解析,下载并生成 Jarfile.lock ,其中包含命令行

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

lockjar lock

(您也可以从Ruby运行它 LockJar.lock

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

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

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

使用Gem打包时,您需要确保来自 Jarfile.lock <的Jars / em>已下载:

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

LockJar.install

确保您要求 Jarfile.lock 与Gem安装的目录相关。这是一个示例帮助器:

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

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

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

这篇关于无法从JRuby gem访问jar依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆