'How to organize java and scala code in Play?

activator new

results in:

Fetching the latest list of templates...



Browse the list of templates: http://lightbend.com/activator/templates
Choose from these featured templates or enter a template name:
  1) minimal-akka-java-seed
  2) minimal-akka-scala-seed
  3) minimal-java
  4) minimal-scala
  5) play-java
  6) play-scala
(hit tab to see a list of all templates)
>

The anatomy of play-java and play-scala looks as follows:

ls project-*/app
...-java/app:
controllers  filters  Filters.java  Module.java  services  views

...-scala/app:
controllers  filters  Filters.scala  Module.scala  services  views

According to this documentation:

You also have the option of using the default layout used by SBT and Maven. Please note that this layout is experimental and may have issues. In order to use this layout, you must disable the layout plugin and set up explicit monitoring for twirl templates:

build.sbt                  → Application build script
src                        → Application sources
 └ main                    → Compiled asset sources
    └ java                 → Java sources
       └ controllers       → Java controllers
       └ models            → Java business layer
    └ scala                → Scala sources
       └ controllers       → Scala controllers
       └ models            → Scala business layer
    └ resources            → Configurations files and other non-compiled resources (on classpath)
       └ application.conf  → Main configuration file
       └ routes            → Routes definition

Discussion

Based on this answer one should combine the java and scala files? E.g.:

merge

...-java/app/controllers:
AsyncController.java  CountController.java  HomeController.java

...-scala/app/controllers:
AsyncController.scala  CountController.scala  HomeController.scala

to

.../app/controllers
AsyncController.java  CountController.java  HomeController.java
AsyncController.scala  CountController.scala  HomeController.scala

Is this the Play anatomy for combining java and scala classes or should a java and scala directory be created in the app/controllers folder?

The routes file that resides in the scala and java project are identical:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# An example controller showing a sample home page
GET     /                           controllers.HomeController.index
# An example controller showing how to use dependency injection
GET     /count                      controllers.CountController.count
# An example controller showing how to write asynchronous code
GET     /message                    controllers.AsyncController.message

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)


Solution 1:[1]

Just put your controllersand models folders into an app folder, and put both scala and java files inside.

build.sbt            ? Application build script
app                  ? Application sources
 ? controllers       ? controllers (java or scala)
 ? models            ? business layer (java or scala)
 ? views             
public
conf
 ? application.conf  ? Main configuration file
 ? routes            ? Routes definition

Solution 2:[2]

You do not need to special "combine" layout - you can use java and scala in the same packages.

Example:

package controllers;

public class MyJavaClass {

  public static String getName(){
    return "My Name";
  }
}

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas Lehoux
Solution 2 Andriy Kuba