简体   繁体   English

从Groovy导入Scala

[英]Import Scala from Groovy

Is there a way, preferred standard way to import Scala code (local jar, or local maven, or remote artifactory)? 有没有一种方法,首选的标准方法来导入Scala代码(本地jar,本地maven或远程工件)?

We are using Groovy on Grails, and some library we want to use are written in Java or Scala. 我们在Grails上使用Groovy,而我们要使用的某些库是用Java或Scala编写的。 Considering they are all JVM languages, there must be a way to interplay. 考虑到它们都是JVM语言,因此必须有一种相互作用的方法。

I've search the net, but have not get a clear answer. 我已经在网上搜索了,但没有一个明确的答案。

Both Scala and Java compiles into Java Bytecode ( .class files) and packed as .jar files. Scala和Java都编译为Java字节码( .class文件)并打包为.jar文件。 It's same bytecode, and can be used from any other JVM language under same classpath. 它是相同的字节码,并且可以在同一类路径下的任何其他JVM语言中使用。

So your app can mix java bytecode produces from any other JVM language, including Java, Groovy, Scala, Clojure, Kotlin, etc. You can put such jars into /lib dir (if your Grails version supports this; it's less preferred way) or as dependency from Maven repository (local or remote). 因此,您的应用程序可以混合使用其他任何JVM语言(包括Java,Groovy,Scala,Clojure,Kotlin等)生成的Java字节码。您可以将此类jars放入/lib目录(如果您的Grails版本支持此方式;这是首选)。作为Maven存储库(本地或远程)的依赖项。 Then you can use such classes from your code 然后您可以从代码中使用此类

See: 看到:

PS there could be some incompatibilities with some classes, because of different nature of sources languages. PS,由于源语言的不同性质,可能与某些类不兼容。 But in general it should work. 但总的来说,它应该起作用。

Did some simple tests. 做一些简单的测试。 In hello-world example, just use 在hello-world示例中,只需使用

groovy -cp the-assembly-SNAPSHOT.jar main-file.groovy groovy -cp the-assembly-SNAPSHOT.jar主文件.groovy

And in the main-file.groovy , should have an import , like in Java/Scala 并且在main-file.groovy ,应该有一个import ,就像在Java / Scala中一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM