将 Scala 集成到 Java 中的现有项目中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20396411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:39:11  来源:igfitidea点击:

Integrating Scala into an existing project in Java

javascalamavenservletsscala-collections

提问by SPIRiT_1984

we have a project written in Java. It is a maven project, that has jsp pages and a lot of java code:

我们有一个用 Java 编写的项目。这是一个maven项目,有jsp页面和很多java代码:

  • servlets for handling user requests and dealing out responses.
  • classes of logic used by servlets in order to process the required logic.
  • classes of SQL generators used to connect to database and perform the specific queries required by the logic.
  • 用于处理用户请求和处理响应的 servlet。
  • servlet 用于处理所需逻辑的逻辑类。
  • 用于连接到数据库并执行逻辑所需的特定查询的 SQL 生成器类。

Well, we are looking towards Scala. For example, we use mappers in order to get a collection of entities from the database and after that transform this collection, filter it and return to the servlet. That seems like a good thing to do in Java. The question is - how can I integrate that into an existing project? Can I use in my logic files written in Java some Scala classes that again use the mapper files written in Java?

好吧,我们正在寻找 Scala。例如,我们使用映射器从数据库中获取实体集合,然后转换这个集合,过滤它并返回到 servlet。这在 Java 中似乎是一件好事。问题是 - 我如何将其集成到现有项目中?我可以在用 Java 编写的逻辑文件中使用一些再次使用用 Java 编写的映射器文件的 Scala 类吗?

采纳答案by James Adam

I work on a mixed Java/Scala project right now (previously a Java-only project). Remember, there is no difference as far as the JVM is concerned between a .class file generated from javac vs. one generated from scalac.

我现在从事一个混合的 Java/Scala 项目(以前是一个仅限 Java 的项目)。请记住,就 JVM 而言,从 javac 生成的 .class 文件与从 scalac 生成的文件之间没有区别。

For example: I implement Java interfaces and extend Java classes in Scala with no problems. I also instantiate Java beans and 'Scala' spring beans together in the same definition file (with scala beans having dependencies on java beans and vice-versa), and I have both Java and Scala beans coexisting together in a spring integration messaging pipeline.

例如:我在 Scala 中实现 Java 接口并扩展 Java 类,没有任何问题。我还在同一个定义文件中一起实例化 Java bean 和 'Scala' spring bean(scala bean 依赖于 java bean,反之亦然),并且我让 Java bean 和 Scala bean 在 spring 集成消息传递管道中共存。

The two languages inter-operate quite well, though where collections are concerned, you'll probably want to take a look at Scala's JavaConversionsand JavaConvertersobjects.

这两种语言可以很好地互操作,尽管在涉及集合的地方,您可能需要查看 Scala 的JavaConversionsJavaConverters对象。

For building, we use Ant (I'd personally prefer SBT for a new project, but if you're dealing with a pre-existing java project that uses ant, it's likely to be more trouble than it's worth to change build systems). To configure ant to compile scala files, have a look at this question.

对于构建,我们使用 Ant(我个人更喜欢 SBT 用于新项目,但如果您正在处理一个使用 ant 的预先存在的 Java 项目,它可能会比更改构建系统更麻烦而不值得)。要配置 ant 以编译 Scala 文件,请查看此问题

回答by Alexey Romanov

Yes, you can have mutual dependencies between Java and Scala without problems.

是的,您可以毫无问题地在 Java 和 Scala 之间建立相互依赖关系。

回答by Richard Close

This: http://scala-tools.org/mvnsites/maven-scala-pluginlooks like a good way to integrate your Scala code into your Maven build, though I haven't tried it. (I use SBT, which does Scala/Java integration nicely, as well as IDE project creation (Idea & Eclipse) via plugins. You wouldn't want to change your whole build process, though).

这个:http: //scala-tools.org/mvnsites/maven-scala-plugin看起来是将 Scala 代码集成到 Maven 构建中的好方法,尽管我还没有尝试过。(我使用 SBT,它可以很好地进行 Scala/Java 集成,以及通过插件创建 IDE 项目(Idea 和 Eclipse)。不过,您不想改变整个构建过程)。

回答by bbarker

I see that no one has mentioned David Bernard's scala-maven-plugin, formerly the maven-scala-plugin, mentioned in @richard-close's answer. I don't have extensive experience with it, but I have had immediate success with two Java projects (one fairly large, one small), so I figured it was worth posting this here — even though the question is somewhat dated now — as I arrived at this question via Google.

我看到没有人提到@richard-close 的回答中提到的David Bernardscala-maven-plugin,以前是 maven-scala-plugin 。我在这方面没有丰富的经验,但是我在两个 Java 项目(一个相当大,一个很小)上取得了立竿见影的成功,所以我认为值得在这里发布这个 - 尽管现在这个问题有些过时了 - 因为我通过谷歌到达这个问题。

The Mixed Java/Scala Projectspage should be able to get you going, though there are other interesting documentation pages as well. Just add the relevant sections shown here and try to compile your project. Don't make the mistake I did and try to use other configurations documented elsewhere with this one first: just follow these minimal config changes in your top-level pom.xml (or whatever makes sense if Scala will have limited scope in your project). Go herefor an easier to copy & paste text (no '+'s showing additions).

混合的Java / Scala的项目页面应该可以让你去,但也有其他有趣的文档页面为好。只需添加此处显示的相关部分并尝试编译您的项目。不要犯我犯的错误,并尝试首先使用其他地方记录的其他配置与此配置:只需在顶级 pom.xml 中遵循这些最小的配置更改(或者如果 Scala 在您的项目中的范围有限,则任何有意义的更改) . 去这里更容易复制和粘贴文本(没有“+”显示添加)。

<dependencies>
+       <dependency>
+        <groupId>org.scala-lang</groupId>
+        <artifactId>scala-library</artifactId>
+        <version>2.11.7</version>
+      </dependency>
</dependencies>
<build>
 <pluginManagement>
  <plugins>
+        <plugin>
+          <groupId>net.alchim31.maven</groupId>
+          <artifactId>scala-maven-plugin</artifactId>
+          <version>3.2.1</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-compiler-plugin</artifactId>
+          <version>2.0.2</version>
+        </plugin>
  </plugins>
 </pluginManagement>
 <plugins>
+       <plugin>
+        <groupId>net.alchim31.maven</groupId>
+        <artifactId>scala-maven-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>scala-compile-first</id>
+            <phase>process-resources</phase>
+            <goals>
+              <goal>add-source</goal>
+              <goal>compile</goal>
+            </goals>
+          </execution>
+          <execution>
+            <id>scala-test-compile</id>
+            <phase>process-test-resources</phase>
+            <goals>
+              <goal>testCompile</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>compile</phase>
+            <goals>
+              <goal>compile</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
 </plugins>
</build>

Be sure to use an updated version of Scala above if you are using Java 8 (i.e. Scala 2.11.x); some of the official docs may be slightly oudated.

如果您使用的是 Java 8(即 Scala 2.11.x),请务必使用上述 Scala 的更新版本;一些官方文档可能稍微有些旧。

One thing I noticed that the docs say you need to do but apparently don't need to do: you don't need to have your Scala sources in a separate directory, and can possibly leave them under the existing java directory of your project. This is convenient, and seems to work, but I do not know if there are side effects.

我注意到文档说您需要做但显然不需要做的一件事:您不需要将 Scala 源代码放在单独的目录中,并且可以将它们保留在项目的现有 java 目录下。这很方便,而且似乎有效,但我不知道是否有副作用。

IntelliJ notes

IntelliJ 笔记

If you happen to be using IntelliJ, don't forget to right-click on the maven module in your project view and 'Add Framework Support ...' to add the Scala Framework to the project. You will probably need to add the framework to each submodule you intend to use Scala in if you are using a multi-module project, not just the top-level module. Then, you shouldbe able to autoconvert Java files to Scala files, accessible through the Refactor menu (though you will need to expect some manual fixes). This can give you a quick way to test the config, especially if there is some little Java class you can try first.

如果您碰巧使用 IntelliJ,请不要忘记在项目视图中右键单击 maven 模块并“添加框架支持...”以将 Scala 框架添加到项目中。如果您使用的是多模块项目,而不仅仅是顶级模块,您可能需要将框架添加到您打算在其中使用 Scala 的每个子模块。然后,您应该能够将 Java 文件自动转换为 Scala 文件,可通过 Refactor 菜单访问(尽管您需要进行一些手动修复)。这可以为您提供一种快速测试配置的方法,特别是如果您可以先尝试一些小的 Java 类。

Caveats

注意事项

@james-adam's comments about JavaConversions are also very important to be aware of. One issue I've realized when trying to integrate Scala with another JVM language (Kotlin) in a single project, is that things may not be that smooth due to how each language handles collections: they are both designed to work well with Java, but not necessarily with each other. I don't know the details, but this is a bit tangential to the question anyway.

@james-adam 关于 JavaConversions 的评论也很重要,需要注意。在单个项目中尝试将 Scala 与另一种 JVM 语言 (Kotlin) 集成时,我意识到的一个问题是,由于每种语言处理集合的方式,事情可能不会那么顺利:它们都被设计为与 Java 配合良好,但是不一定是彼此。我不知道细节,但这无论如何都与问题无关。