简体   繁体   English

在同一项目中使用多种JVM语言

[英]Using multiple JVM languages in the same project

I'd like to use Kotlin & Scala together in projects, and maybe some other languages, but I've seen no good way of doing it. 我想在项目中以及其他一些语言中一起使用Kotlin和Scala,但是我没有很好的方法。 The only way I thought of was compiling one language and decompiling it into Java to work with the other. 我想到的唯一方法是编译一种语言并将其反编译为Java以与另一种语言一起使用。 Are there any alternatives? 还有其他选择吗?

You can use Scala and Java simultaneously, since scalac understands and compiles Java files. 您可以同时使用Scala和Java,因为scalac可以理解和编译Java文件。 The same probably holds for other languages. 其他语言也可能如此。 Problems might arise when using multiple alternative JVM languages, since, eg, the Kotlin compiler probably can't understand the Scala files and vice versa. 当使用多种替代JVM语言时,可能会出现问题,因为例如Kotlin编译器可能无法理解Scala文件,反之亦然。

I think the best way would be to split the project into different modules, and use at most one alternative language per module. 我认为最好的方法是将项目分成不同的模块,并且每个模块最多使用一种替代语言。

What do I mean with module ? 我对模块是什么意思?

With module , I mean a set of source files that gets translated into one (binary) artifact, ie a jar file. 对于module ,我的意思是将一组源文件转换为一个 (二进制)工件,即jar文件。 Under different circumstances I would simply call a "module" a project. 在不同的情况下,我只是将“模块”称为项目。 Note that a module may depend on other modules on the binary level (eg has some jar files as dependencies). 请注意,一个模块可能在二进制级别上依赖于其他模块(例如,具有一些jar文件作为依赖项)。

Multi module support in IDEs IDE中的多模块支持

I think it should be possible with most major IDEs to work on different modules simultaneously, even if each module uses a different language. 我认为,即使每个模块使用不同的语言,大多数主要的IDE也应该可以同时在不同的模块上工作。 Terminology varies across IDEs. 各个IDE的术语有所不同。

Terminology 术语

For Intellij IDEA, one of my modules is called "module". 对于Intellij IDEA,我的模块之一称为“模块”。 For Eclipse it would be called "project". 对于Eclipse,它将被称为“项目”。

For the sake of completeness and not putting words into someone else's mouth, I wanted to weigh in. 为了完整起见,而不是在别人的嘴里说些什么,我想称一下。

I agree with the last sentence of ziggystar's answer . 我同意齐格斯塔的回答的最后一句话。 The right thing to do is to take a component-based approach and not try to combine multiple languages in one component or project. 正确的做法是采用基于组件的方法,而不是尝试在一个组件或项目中组合多种语言。

From a technical perspective, each of the JVM languages has their own compiler. 从技术角度来看,每种JVM语言都有自己的编译器。 Some, such as Scala's, can compile both Scala and Java files. 有些,例如Scala的,可以同时编译Scala和Java文件。 However, this may or may not be true for other compilers. 但是,这对于其他编译器可能正确,也可能不正确。 In order to avoid strange build processes, a good approach would be to use a single language for every built module. 为了避免奇怪的构建过程,一个好的方法是对每个构建的模块使用一种语言。

Since you're sticking to JVM languages, every languages can be compiled into a JAR, so you can easily distribute your executable binary as a single JAR file, with all of the components wrapped up inside it. 由于您坚持使用JVM语言,因此每种语言都可以编译成JAR,因此您可以轻松地将可执行二进制文件作为单个JAR文件分发,并将所有组件包装在其中。 This is the Fat JAR approach (see this question on Stack Overflow , this post on Java Code Geeks ). 这就是Fat JAR方法(请参阅有关Stack Overflow的此问题有关Java Code Geeks的这篇文章 )。

From a human readability perspective, this should also make your software more easily understood. 从人类可读性的角度来看,这还应该使您的软件更容易理解。 Not only have you decomposed it into logical building blocks (each component), but someone making modifications only needs to understand the language that the component they are working on is written in and the public interface of the components they need to interact with. 不仅您将其分解为逻辑构建块(每个组件),而且进行修改的人只需要了解他们正在使用的组件所使用的语言以及与之交互的组件的公共接口。 There's no mental context switching between languages. 语言之间没有思维上的切换。

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

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