[英]Integrating Scala into an existing project in Java
we have a project written in Java. 我们有一个用Java编写的项目。 It is a maven project, that has jsp pages and a lot of java code:
这是一个maven项目,它有jsp页面和很多java代码:
Well, we are looking towards Scala. 好吧,我们正在寻找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.
例如,我们使用映射器从数据库中获取实体集合,然后转换此集合,过滤它并返回到servlet。 That seems like a good thing to do in Java.
这在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?
我可以在我用Java编写的逻辑文件中使用一些Scala类,这些类再次使用用Java编写的映射器文件吗?
I work on a mixed Java/Scala project right now (previously a Java-only project). 我现在正在处理一个混合的Java / Scala项目(以前只是一个Java项目)。 Remember, there is no difference as far as the JVM is concerned between a .class file generated from javac vs. one generated from scalac.
请记住,就javac生成的.class文件与scalac生成的.class文件之间的JVM而言,没有任何区别。
For example: I implement Java interfaces and extend Java classes in Scala with no problems. 例如:我实现Java接口并在Scala中扩展Java类没有任何问题。 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.
我还在同一个定义文件中将Java bean和'Scala'spring bean一起实例化(scala bean依赖于java bean,反之亦然),并且我在Spring集成消息传递管道中共存Java和Scala bean。
The two languages inter-operate quite well, though where collections are concerned, you'll probably want to take a look at Scala's JavaConversions and JavaConverters objects. 这两种语言互操作性很好,但在涉及集合的情况下,您可能希望查看Scala的JavaConversions和JavaConverters对象。
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). 对于构建,我们使用Ant(我个人更喜欢SBT用于新项目,但是如果您正在处理使用ant的预先存在的Java项目,那么更改构建系统可能会更麻烦)。 To configure ant to compile scala files, have a look at this question .
要配置ant以编译scala文件,请查看此问题 。
I see that no one has mentioned David Bernard 's scala-maven-plugin , formerly the maven-scala-plugin, mentioned in @richard-close's answer. 我看到没有人提到David Bernard的scala-maven-plugin ,以前是maven-scala-plugin,在@ richard-close的回答中提到过。 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.
我没有丰富的经验,但我已经立即成功完成了两个Java项目(一个相当大,一个小),所以我认为值得在这里发布 - 尽管这个问题现在有些过时 - 因为我通过谷歌来到这个问题。
The Mixed Java/Scala Projects page should be able to get you going, though there are other interesting documentation pages as well. 尽管还有其他有趣的文档页面,但混合Java / Scala项目页面应该能够帮助您实现目标。 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).
不要犯下我做的错误并尝试使用其他配置首先记录在其他配置中:只需在顶级pom.xml中执行这些最小配置更改(或者如果Scala在项目中的范围有限,则无论如何都有意义) 。 Go here for an easier to copy & paste text (no '+'s showing additions).
转到此处可以更轻松地复制和粘贴文本(没有'+'显示添加内容)。
<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 (ie Scala 2.11.x); 如果您使用的是Java 8(即Scala 2.11.x),请务必使用上面的Scala更新版本; some of the official docs may be slightly oudated.
一些官方文档可能会稍微调整一下。
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. 有一件事我注意到文档说你需要做但显然不需要这样做:你不需要将Scala源放在一个单独的目录中,并且可以将它们留在项目的现有java目录下。 This is convenient, and seems to work, but I do not know if there are side effects.
这很方便,似乎有效,但我不知道是否有副作用。
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. 如果您碰巧使用IntelliJ,请不要忘记右键单击项目视图中的maven模块和“添加框架支持...”以将Scala框架添加到项目中。 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.
如果您使用的是多模块项目,而不仅仅是顶层模块,则可能需要将框架添加到要使用Scala的每个子模块中。 Then, you should be able to autoconvert Java files to Scala files, accessible through the Refactor menu (though you will need to expect some manual fixes).
然后,您应该能够将Java文件自动转换为Scala文件,可通过Refactor菜单访问(尽管您需要一些手动修复)。 This can give you a quick way to test the config, especially if there is some little Java class you can try first.
这可以为您提供一种快速测试配置的方法,特别是如果您可以先尝试一些小Java类。
@james-adam's comments about JavaConversions are also very important to be aware of. @james-adam关于JavaConversions的评论也非常重要。 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.
在一个项目中尝试将Scala与另一种JVM语言(Kotlin)集成时,我已经意识到的一个问题是,由于每种语言处理集合的方式,事情可能不会那么顺利:它们都设计为与Java兼容,但是不一定是彼此。 I don't know the details, but this is a bit tangential to the question anyway.
我不知道细节,但无论如何这与问题有点相关。
This: http://scala-tools.org/mvnsites/maven-scala-plugin looks like a good way to integrate your Scala code into your Maven build, though I haven't tried it. 这个: http : //scala-tools.org/mvnsites/maven-scala-plugin看起来是将Scala代码集成到Maven构建中的好方法,尽管我还没有尝试过。 (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).
(我使用SBT,它可以很好地完成Scala / Java集成,以及通过插件创建IDE项目(Idea&Eclipse)。但是,您不希望改变整个构建过程)。
是的,您可以在Java和Scala之间存在相互依赖性而不会出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.