Is it recommended to separate the Scala and Java source files for the Maven project?

I was working on a Maven project, all made up of Java, and recently I started mixing Scala code.

I am struck by the great expressiveness of Scala sentences, the easy use of the scala -maven-plugin and the particularly incredible interoperability between Java and Scala.

However, I hit one inconvenience; according to the Maven convention, the Java source code goes to src/main/java , whereas Scala to src/main/scala . I found this rather cumbersome because I often have to go back and forth Java and Scala source files, and every time I have to go through a deep hierarchy of package directories (I often close tabs so my editor is not cluttered).

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

To add more background, I worked on a Wicket web application map, the convention of which is to put HTML files along with the corresponding Java files. If we separate the directories, naturally, the HTML files will also be divided (I don’t think that using Scala files and corresponding HTML files in different directories makes sense). And then this happens, β€œWhy can't I find Foo.html? Oh, I was looking for the wrong directory.”

The source files themselves are very easy to distinguish between people and machines by checking their extensions. I configured pom.xml to process both Java and Scala compiled in src/main/java and it worked (compiles and runs). Directory splitting, on the other hand, creates the risk of defining conflicting classes in Java and Scala, a careless mistake.

Well, I do not want to name the java directory if it contains not only Java, but also Scala. But this is the only thing I can think of for directory splitting. (Edit: I came up with a workaround, let's think java means Java virtual machine. So having src/main/c does not contradict if we ever decided to use JNI because C does not run on the JVM.)

Oh, and one more thing; my project is not intended as an open source project; ease of development is preferable to trying to complicate the implementation of agreements.

This question is inspired by this answer .

+3
source share
1 answer

I would say yes, reuse the code as much as possible. Perhaps in the future you can use this part of Java somewhere else ...

As you probably know, you can use Java in Scala projects, but not Scala in Java projects. So, in this specific example, this will help you with (future?) Java projects. If you want to reuse part of your Java code, you can do this in Java projects as well as in Scala projects.

So imho it does not stop at src / main / ... but you really have to put them even in different components.


Btw, a quick note: if I'm right, Wicket allows you to put html in a different place, even in another project ... I saw that it was convenient (only) once when we had to create different interfaces for different clients from us. Java code remains the same, as well as the gate ID, but html has changed everywhere. Although this gave us some problems using the Qwicky plugin, as it could no longer find html files in our IDE.

0
source

All Articles