我的混合Scala / Java Maven项目无法编译

我有一个混合的Scala / Java项目,编译得不好。

当Java代码试图在同一个包中调用Scala代码时,会出现问题。

当然,我有标准的布局:

  • 的src /主/ JAVA
  • 的src /主/阶
  • 的src /测试/ JAVA
  • 的src /测试/阶

我看过其他类似的Stackoverflow问题,但这个问题有点过时了。 这个问题也无济于事。

我还关注了scala-maven-plugin文档页面 。

    net.alchim31.maven scala-maven-plugin 3.1.6   org.apache.maven.plugins maven-compiler-plugin 2.0.2      net.alchim31.maven scala-maven-plugin   scala-compile-first process-resources  add-source compile    scala-test-compile process-test-resources  testCompile      org.apache.maven.plugins maven-compiler-plugin   compile  compile       

我试过关注这篇博文并没有成功。

使用从pom.xml导入的Scala插件的IDEA项目可以成功编译和运行我的项目。

这样做的正确方法是什么? Java代码是否被编译两次? 首先是Scala插件和Java插件吗?

这是pom.xml的一个工作示例:

   4.0.0 stackoverflow q24448582 1.0-SNAPSHOT  2.10.3    org.scala-lang scala-library ${scala.version}      org.scala-tools maven-scala-plugin 2.15.2   compile  compile  compile   test-compile  testCompile  test-compile   process-resources  compile        

问题1

起初我认为问题是由于使用了现代版本的maven-compiler-plugin。 我使用的是3.0版而不是2.0.2版。

解决方案是: – 删除maven-compiler-plugin – 添加此设置:incremental

最后我的pom.xml是这样的:

  net.alchim31.maven scala-maven-plugin 3.1.6  incremental  -deprecation -explaintypes -target:jvm-1.7     scala-compile-first process-resources  add-source compile    scala-test-compile process-test-resources  add-source testCompile     

问题2

我遇到的另一个问题是:

   org.json4s json4s-native_${scala.version} 3.2.9   org.scala-lang scala-library ${scala.version}.4    2.10  

这使得这个问题得以解决: https : //issues.scala-lang.org/browse/SI-5733

可能它使用的是旧版本的Scala编译器。

解决方案是将scala.version重命名为scala.major,因为此属性具有特殊含义。

为了让scala-maven-plugin尊重Java 1.8,但仍然允许java和scala的混合编译,我们使用这个:

 ...  org.apache.maven.plugins maven-compiler-plugin 3.5  1.8 1.8    default-compile none     net.alchim31.maven scala-maven-plugin 3.2.2  incremental     compile testCompile     ...