无法从 JRuby gem 访问 jar 依赖项
我在本地系统上创建了一个用于文本挖掘的 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 underlib/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是否建议使用 LockJar 管理你的 Jars 会更容易。只需安装 gem
创建一个 Jarfile
创建一个 Jarfile Jar 依赖项列表:
最简单的 jar 表示法是groupId:artifactId:version。其他支持的变体有groupId:artifactId:type:version和groupId:artifactId:type:classifier:version。
生成 Jarfile.lock
传递依赖项被解析、下载并生成到 Jarfile.lock 与 命令行:(
您也可以从 Ruby 运行它,如 LockJar.lock)
访问您的 Jars
Jarfile.lock 包含所有 Jar 依赖项信息。现在,您可以通过以下方式将 Jarfile.lock 中的 jar 加载到类路径:
使用 Gem 打包时,您需要确保 Jarfile.lock 中的 Jars 已下载with:
确保您请求的 Jarfile.lock 与 Gem 的安装目录相关。以下是一个示例帮助程序:
您还可以将 Gem 设置为 下载Gem 安装时的 Jar 依赖项,无需调用
LockJar.install
。Might I suggest that managing your Jars with LockJar would be easier. Just install the gem
Create a Jarfile
Create a Jarfile that contains a list of your Jar dependencies:
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:
(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:
When packaging with a Gem, you will need to ensure the Jars from Jarfile.lock have been downloaded with:
Make sure you request the Jarfile.lock is relative to the Gem's installed dir. Here is an example helper:
You can also setup your Gem to download the Jar dependencies when the Gem installs, removing the need to call
LockJar.install
.使用绝对路径,如下所示:
Use absolute paths like so: