Search code examples
playframeworkroutesplayframework-2.5playframework-routing

Play with subprojects routes


I have a Play! 2.5.3 project with one subproject (with its own routes). In the main routes file the subproject's routes are defined as follows:

->        /api                         api.Routes
GET       /assets/*file                controllers.Assets.at(path="/public", file)

and the api.routes file is as folllows:

GET      /v1/permissions/roles         controllers.api.PermissionsController.roles

So the URL fro getting the roles would be /api/v1/permissions/roles.

When I run the root project (which depends on both subprojects) everything works fine, but when running each subproject separately, the routes are not found. I tried copying the main routes file to the conf folder of the built subproject during the build, but it didn't work.

The only solution I found was adding play.http.router = "api.Routes" in application.conf, but the routes "looses" its /api prefix (ie: the api route is /v1/permissions/roles)

So my question is: is it possible to have a subproject with routes, and these routes be the same when running the root project and when running each subproject separately?

And if it's possible, is it anywhere in the docs?

EDIT:

build.sbt for the root project

name := """abc"""
version := "0.1.0"
scalaVersion := "2.11.7"

resolvers += Resolver.jcenterRepo
javaOptions in Test += "-Dconfig.file=conf/test.conf"
javaOptions in Test += "-Dtest.timeout=600000"


lazy val abcCore = (project in file("modules/abc-core")).enablePlugins(PlayJava, PlayEbean)

lazy val abcCentral = (project in file("modules/abc-central")).enablePlugins(PlayJava, PlayEbean)
    .dependsOn(abcCore)
    .aggregate(abcCore)

lazy val abcSimulador = (project in file("modules/abc-simulador")).enablePlugins(PlayJava, PlayEbean)
    .dependsOn(abcCore)
    .aggregate(abcCore)


lazy val abcDevice = (project in file("modules/abc-device")).enablePlugins(PlayJava, PlayEbean)
    .dependsOn(abcCore)
    .aggregate(abcCore)

lazy val abcApi = (project in file("modules/abc-api")).enablePlugins(PlayJava, PlayEbean)
    .dependsOn(abcCore, abcSimulador)
    .aggregate(abcCore, abcSimulador)

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)
    .dependsOn(abcCore, abcDevice, abcCentral, abcSimulador, abcApi)
    .aggregate(abcCore, abcDevice, abcCentral, abcSimulador, abcApi)


libraryDependencies ++= Seq(
    javaJdbc,
    cache,
    javaWs,
    "uk.co.panaxiom" %% "play-jongo" % "2.0.0-jongo1.3",
    "be.objectify" %% "deadbolt-java" % "2.5.0",
    "mysql" % "mysql-connector-java" % "5.1.36",
    "org.hibernate" % "hibernate-validator" % "5.2.4.Final",
    "com.fasterxml.jackson.datatype" % "jackson-datatype-joda" % "2.7.5",
    "uk.co.modular-it" % "bean-utils" % "0.9.10",
    "org.hamcrest" % "hamcrest-library" % "1.3",
    "commons-beanutils" % "commons-beanutils" % "1.9.2",
    "net.coobird" % "thumbnailator" % "0.4.8",
    "io.moquette" % "moquette-broker" % "0.8.1" exclude("org.slf4j", "slf4j-log4j12"),
    "org.mindrot" % "jbcrypt" % "0.3m",
    "org.apache.commons" % "commons-math3" % "3.6.1",
    "com.typesafe.play" %% "play-mailer" % "5.0.0",
    "org.thymeleaf" % "thymeleaf" % "3.0.1.RELEASE",
    "org.jetbrains.kotlin" % "kotlin-stdlib" % "1.0.3",
    "org.eclipse.collections" % "eclipse-collections-api" % "7.1.0",
    "org.eclipse.collections" % "eclipse-collections" % "7.1.0",
    "org.eclipse.collections" % "eclipse-collections-forkjoin" % "7.1.0",
    "net.jpountz.lz4" % "lz4" % "1.3.0",
    "org.mapdb" % "elsa" % "3.0.0-M6",
    "com.google.guava" % "guava" % "19.0",
    "org.jfree" % "jfreesvg" % "3.1",
    "net.sf.jasperreports" % "jasperreports" % "6.3.1",
    "com.github.jhonnymertz" % "java-wkhtmltopdf-wrapper" % "1.0.1-RELEASE",
    "org.awaitility" % "awaitility-scala" % "2.0.0",
    "org.fusesource.mqtt-client" % "mqtt-client" % "1.14")


jacoco.settings
parallelExecution in jacoco.Config := false
jacoco.reportFormats in jacoco.Config := Seq(
    de.johoop.jacoco4sbt.ScalaHTMLReport(encoding = "utf-8", withBranchCoverage = true),
    de.johoop.jacoco4sbt.XMLReport(encoding = "utf-8"))


fork in run := false
fork in Test := false



mappings in Universal <++= (packageBin in Compile) map { jar =>
    val scriptsDir = new java.io.File("modules/abc-api/app/templates/")
    scriptsDir.listFiles.toSeq.map { f =>
        f -> ("app/templates/" + f.getName)
    }
}

mappings in Universal <++= (packageBin in Compile) map { jar =>
    val scriptsDir = new java.io.File("modules/abc-api/public/images/")
    scriptsDir.listFiles.toSeq.map { f =>
        f -> ("public/images/" + f.getName)
    }
}

Solution

  • I don't think you could do that, the projects are binary bound, I mean all the module jars has to be there in the class path (as per the build.sbt). You can not run them individually.

    I hope what you are looking is a microservices kind of stuff. If so, you need to be aware of the costs that come with microservices. Once you decide to go with microservices, you can take a look at Lagom Framework (https://www.lightbend.com/lagom)