是否建议为Maven项目分离Scala和Java源文件? [英] Is it recommended to separate Scala and Java source files for a Maven project?

查看:131
本文介绍了是否建议为Maven项目分离Scala和Java源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个完全由Java组成的Maven项目,最近开始将Scala代码混合到其中。

I've been working on a Maven project consisting entirely of Java, and lately started to mix Scala code into it.

我很惊讶于表现力很强Scala提供了scala-maven-plugin的简单使用,特别是Java和Scala之间令人难以置信的互操作性。

I'm amazed by the great expressiveness Scala offers, the easy use of scala-maven-plugin, and especially the incredible interoperability between Java and Scala.

然而,我遇到了一个不便;根据Maven的惯例,Java的源代码进入 src / main / java ,而Scala进入 src / main / scala 。我发现它非常繁琐,因为我必须经常来回浏览Java和Scala源文件,每次我必须遍历包目录的深层次结构(我经常关闭选项卡以防止我的编辑器混乱)。

However, I hit one inconvenience; according to the Maven's convention, Java's source code goes into src/main/java, whereas Scala's into src/main/scala. I found it quite cumbersome because I have to frequently go back and forth Java and Scala source files and every time I have to traverse the deep hierarchy of package directories (I often close tabs to keep my editor from cluttered).

所以问题是:是否建议维护单独的目录 src / main / java 的src /主/阶?如果是这样,为什么?

So the question is: Is it recommended to maintain separate directories src/main/java and src/main/scala? If so, why?

为了增加更多背景知识,我一直致力于Web应用程序框架Wicket,其惯例是将HTML文件与相应的Java放在一起文件。如果我们保持目录分离,HTML文件自然也是分开的(我不认为将Scala文件和相应的HTML文件放在不同的目录中是有意义的)。然后它为什么我找不到Foo.html?哦,我正在寻找错误的目录。

To add more background, I've been working on the web application framework Wicket, whose convention is to put the HTML files alongside with their corresponding Java files. If we keep the directories separated, naturally the HTML files are separated as well (I don't think putting Scala files and corresponding HTML files in different directories makes sense). And then it goes "why I can't find Foo.html? Oh, I was looking for the wrong directory."

源文件本身很容易区分人类和机器通过检查他们的扩展。我配置了pom.xml来处理在 src / main / java 中放在一起的Java和Scala,并且它有效(编译并运行)。另一方面,分离目录存在在Java和Scala中定义冲突类的风险,这是一个粗心的错误。

The source files themselves are very easy to distinguish both by humans and by machines by inspecting their extensions. I configured pom.xml to handle both Java and Scala put together in src/main/java and it worked (compiles and runs). On the other hand, separating directories poses a risk of defining conflicting classes in Java and in Scala, a careless mistake.

好吧,我不想命名目录 java 如果它不仅包含Java,还包含Scala。但这是我可以用来分隔目录的唯一一点。

(编辑:我想出了一个解决方法 - 解释;让我们想想 java 代表 Java虚拟机。这样,如果我们决定使用JNI,那么 src / main / c 并不矛盾因为C不能在JVM上运行。)

Well, I don't want to name a directory java if it contained not only Java's but also Scala's. But this is the only point I can come up with for separating directories.
( I've come up with a workaround-interpretation; let us think java stands for Java Virtual Machine. In this way, having src/main/c doesn't contradict if we ever decided to use JNI because C doesn't run on JVM.)

哦,还有一点;我的项目不是一个开源项目;开发便利性优于努力遵循惯例。

Oh, and one more point; my project is not intended as an open-source project; development convenience is preferred than trying hard to follow conventions.

此问题的灵感来自于这个答案

推荐答案

我会说是的,尽可能重复使用代码。也许以后你可以在其他地方使用这个Java文件...

I'd say yes, re-use code as much as possible. Maybe in future you can use this Java piece somewhere else...

您可能知道,您可以在Scala项目中使用Java,但在Java项目中不使用Scala。所以在这个具体的例子中,它将帮助你(未来?)Java项目。如果要重用一段Java代码,可以在Java项目和Scala项目中执行此操作。

As you probably know, you can use Java in Scala projects but not Scala in Java projects. So in this specific example it will help you with (future?) Java projects. If you want to re-use a piece of your Java code you can do that in either Java projects as well as Scala projects.

所以i.m.h.o.它不会停留在src / main / ...但你应该把它们放在不同的组件中。

So i.m.h.o. it doesn't stop at the src/main/... but you should really put them even in different components.

顺便说一句,小方注意:如果我是正确的,Wicket允许你将html放在其他地方,即使是在另一个项目中...我看到它只是方便(只)一次,我们必须创建不同的前端我们不同的客户。 java代码保持不变,wicket-id也是如此,但html到处都有变化。虽然使用Qwicky插件确实给我们带来了一些问题,因为它在我们的IDE中找不到html文件了。

Btw, little side note: if I'm correct, Wicket allows you to put the html somewhere else too, even in a different project... I saw it being handy (only) once, where we had to create different frontend for different clients of us. The java code stayed the same, the wicket-id's as well, but the html changed everywhere. Though it did give us some problems as well using the Qwicky plugin, as it could not find the html files in our IDE anymore.

这篇关于是否建议为Maven项目分离Scala和Java源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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