我可以使用混合的Java和Scala代码在Maven中以两种方式编译Scala项目? [英] Can I compile a Scala project with mixed java and scala code with dependencies both ways in Maven?

查看:139
本文介绍了我可以使用混合的Java和Scala代码在Maven中以两种方式编译Scala项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中包含scala和java代码,我正在eclipse中使用scala IDE插件进行管理。目前的目录结构是这样组织的,这样一些软件包中就有java和scala代码。有scala类依赖于java类,还有其他依赖于scala类的java类。该插件可以透明地处理所有这些 - 我相信(但我不是100%肯定的)scala编译器将java和scala代码汇编在一起,这就是交叉依赖关系的处理方式。我确实找到一些参考,使用Maven编译混合项目例如,在这里,但是我看到的是将java和scala分成两个单独的源代码树。 Maven首先尝试编译scala代码,因为它依赖于java代码而失败。同样,如果首先编译了java代码,那么它也将失败,因为依赖关系是两种方式。有没有办法让Maven把所有的代码整合在一起,就像eclipse插件一样?如果可能,我也希望有能力生成一个eclipse项目,而不需要太多的手动修复。

解决方案

通常的Maven目录结构是

  src / 
main /
java /
com / foo / mypackage /
scala /
com / foo / mypackage /

如果您以这种方式设置,那么肯定可以在Java和Scala代码之间建立交叉依赖关系。请参阅 maven-scala-plugin scala-maven-plugin 页面上的主题。



我不建议将Java和Scala代码放在同一目录中。


I have a project which has both scala and java code which I am currently managing in eclipse using the scala IDE plugin. At present the directory structure is organized such that some packages have both java and scala code in them. There are scala classes which depend on java classes and there are other java classes which depend on the scala classes. The plugin handles all of this transparently – I believe (but I’m not 100% sure) that the scala compiler compiles both the java and scala code together which is how the cross dependencies are handled. I did find some references to compiling mixed projects using Maven e.g. here but the ones I saw talking about separating the java and scala into two separate source trees. Maven attempts to compile the scala code first which fails because of the dependencies on the java code. Likewise, if the java code were compiled first it would also fail as the dependencies go both ways. Is there a way to have Maven compile all the code together like the eclipse plugin? I’d also like to have the ability to generate an eclipse project without doing too much manual fix-up afterwards if possible.

解决方案

The usual Maven directory structure is

src/
  main/
    java/
      com/foo/mypackage/
    scala/
      com/foo/mypackage/

If you set things up this way then for sure you can have cross dependencies between your Java and Scala code. See the maven-scala-plugin or scala-maven-plugin pages on the topic.

I would not recommend putting Java and Scala code in the same directory.

这篇关于我可以使用混合的Java和Scala代码在Maven中以两种方式编译Scala项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆