[英]How to package in a .jar java main + jruby compiled class + ruby script?
I am building a Spark application that make use of some feature already developed in Ruby. 我正在构建一个Spark应用程序,该应用程序利用Ruby中已经开发的某些功能。
I made the choice to invoke the Ruby part from my Scala main by defining MyProxy
class in ruby and compiling with JRuby. 通过在ruby中定义
MyProxy
类并使用JRuby进行编译,我决定从我的Scala主程序中调用Ruby部分。 I can then use MyProxy
to invoke the rest of the Ruby code that stay in scripts. 然后,我可以使用
MyProxy
调用保留在脚本中的其余Ruby代码。 Foremost reason is that I was unable to compile them with JRuby, probably because they are too dynamic: 最主要的原因是我无法使用JRuby编译它们,可能是因为它们太动态了:
## myProxy.rb -> compiled into myProxy.class
## jrubyc --javac myProxy.rb
require 'java'
java_package 'ruby.proxy'
require_relative 'some.rb'
class MyProxy
def self.invoke_script()
... ## invoke some other ruby in script that are note compiled by jrubyc
end
end
and the Scala Main: 和Scala Main:
object myRun extends App {
val something = MyProxy.invoke_script()
...
}
At runtime the flow looke like this: 在运行时,流程如下所示:
Main.class (scala) -> call myProxy.class (compiled ruby of myProxy.rb) -> call function in script.rb Main.class(scala)->调用myProxy.class(myProxy.rb的已编译Ruby)-> script.rb中的调用函数
It works, and I was able to make a runnable jar for the Scala and compiled ruby part. 它起作用了,我能够为Scala和可编译的ruby部件制作一个可运行的jar。 But when I am running it:
java -jar myApp.jar
, it still need to access my myProxy.rb file and, of course, all other scrips.rb. 但是,当我运行它时:
java -jar myApp.jar
,它仍然需要访问mymyProxy.rb文件以及所有其他scrips.rb文件。 So I need a copy of all my ruby scripts in the working directory when executing this command. 因此,执行此命令时,我需要工作目录中所有ruby脚本的副本。
Ideally I would like to include all the ruby scripts in the myApp.jar as well, and be able to deploy easily deploy on a spark cluster. 理想情况下,我也希望将所有ruby脚本也包括在myApp.jar中,并且能够轻松地部署在spark集群上。 Is this possible, and how?
这有可能吗?
I have looked at warbler and rawr . 我看了莺和罗勒 。 However, I don't see how these tools can help me in this mixed environment (main in Java, some part compiled ruby, some part pure scripts).
但是,我看不到这些工具如何在这种混合环境中提供帮助(主要在Java中,部分编译为ruby,某些部分为纯脚本)。
Any help appreciated! 任何帮助表示赞赏!
As stated in the jruby documentation , packaging ruby scripts in a jar, should be has simple as including them in the .jar (sic): 如jruby文档所述 ,将ruby脚本包装在jar中应该很简单,只需将它们包含在.jar中即可(原文如此):
Everything in the Java classpath is considered to be a load path entry, so .rb scripts, for example, contained in JAR files, are loadable.
Java类路径中的所有内容都被视为加载路径条目,因此,可以加载JAR文件中包含的.rb脚本。
It wasn't working in my case, because I was using require_relative in my script, which doesn't seem to work properly in recent release of JRuby . 在我的情况下,它不起作用,因为我在脚本中使用了require_relative , 但在最新版本的JRuby中似乎无法正常工作 。 Replacing require_relative with require made it works with scripts embedded in myApp.jar .
用require替换require_relative使其可以与myApp.jar中嵌入的脚本一起使用 。
Moreover using require_relative 'toto'
in titi.rb was triggering an error of titi.rb script not found, which was misleading since it was titi.rb which was being executed. 此外,在titi.rb中使用
require_relative 'toto'
触发未找到titi.rb脚本的错误,这是误导的,因为正在执行的是titi.rb。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.