Maven:在一个项目中混合使用Java和Scala [英] Maven: mixing Java and Scala in one project
问题描述
今天我一直在努力找到一个合适的解决方案来设置一个包含Java和Scala代码的maven项目(它们之间具有双向依赖关系)。
Today I've been trying to find a proper solution to set up a maven project that contains both Java and Scala code (with two-way dependencies between them).
我发现的解决方案通常包括在 process-resources $中调用scala-maven-plugin或maven-scala-plugin c $ c>阶段使其在默认的maven编译器插件之前运行(例如: http://www.hascode.com/2012/03/snippet-mixing-scala-java-in-a-maven-project/ , https://itellity.wordpress.com/2014 / 08/21 / mixed-scala-and-java-in-a-maven-project / ,
官方scala-maven-plugin页面: http://davidb.github.io/scala-maven-plugin/example_java.html 。
The solutions I've found usually consist of invoking scala-maven-plugin or maven-scala-plugin in the process-resources
phase so that it runs before the default maven compiler plugin (examples: http://www.hascode.com/2012/03/snippet-mixing-scala-java-in-a-maven-project/, https://itellity.wordpress.com/2014/08/21/mixing-scala-and-java-in-a-maven-project/,
the official scala-maven-plugin page: http://davidb.github.io/scala-maven-plugin/example_java.html).
这导致解决方案如下所示:
That leads to the solution that looks like this:
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<recompileMode>incremental</recompileMode>
<executions>
<execution>
<id>scala-compile</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>
</plugins>
</build>
此解决方案运行良好 - 在 process-resources <中调用Scala编译/ code>阶段,它编译Java和Scala代码,因此当maven编译器插件在
编译
阶段运行时,.class文件都已准备就绪。
This solutions works well - Scala compilation is invoked in the process-resources
phase and it compiles both Java and Scala code so .class files are all ready when the maven compiler plugin runs in the compile
phase.
问题是这个解决方案看起来并不干净。在编译阶段之前调用Scala编译过程只是为了确保它在maven编译器插件之前运行似乎是hacky。
The problem is that this solution doesn't look clean to me. Invoking the Scala compilation process before the compilation phase just to make sure it runs before the maven compiler plugin seems "hacky".
Scala编译器无论如何编译Java类,所以我想我可以完全关闭默认的maven编译器插件,然后Scala编译器可以在编译中运行
阶段。虽然配置稍微长一点,但它看起来更干净:
Scala compiler compiles Java classes anyway so I thought I could just completely turn off the default maven compiler plugin and then Scala compiler could run in the compile
phase. It looks much cleaner to me although the configuration is a little bit longer:
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<recompileMode>incremental</recompileMode>
<executions>
<execution>
<id>scala-compile</id>
<phase>compile</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
我想知道为什么这个解决方案不是博客文章或官方插件页面上建议的解决方案。这种方法有什么缺点吗?我应该期待使用第二种解决方案而不是第一种解决方案吗?
I'm wondering why this solution is not the one advised in blog posts or on the official plugin page. Are there any downsides to this approach? Are there any problems I should expect using the second solutions instead of the first one?
推荐答案
- 是的,解决方案是hacky但maven-compiler-plugin始终是第一个插件在编译阶段运行(就像硬编码成maven)。
- 我没有用scala 2.11测试,但是先前版本的scalac不能将.java编译成.class(只解析它们)。自从2.7开始,scala-maven-plugin就会以每个版本的scala运行。
这篇关于Maven:在一个项目中混合使用Java和Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!