malmling
malmling

Reputation: 2518

Playframework 2.2 with scala and sub-projects

I have been trying to build up a project with some subprojects but I cannot get it to work as I want..

What I have now is a Play scala main project. I added two sub-modules, domain and infrastructure. I want everything to depend on everything. What I mean is that my infrastructure which is a scala module should have access to my main projects application.conf etc.

I'm going to use my infrastructure to store stuff in the database, which is set in the main projects conf-directory.

I have this structure right now:

- Main project
  - app
     -controllers
     -views
  - conf
     -evolutions
     -application.conf
     -routes
  - domain <- scala module
  - infrastructure <- scala module
  - project
  - public
  - test
  - build.sbt

I want everything to be as one. All dependencies and modules should be accessable in all modules.

I want to be able to access the database that is setup in application.conf from infrastructure

My build.sbt now is:

name := "Main"

version := "1.0-SNAPSHOT"

play.Project.playScalaSettings

lazy val Main = project.in(file("."))

lazy val domain = project dependsOn Main

lazy val infrastructure = project dependsOn domain

libraryDependencies ++= Seq(
  anorm,
  jdbc,
  cache,
  "org.scala-tools" % "maven-scala-plugin" % "2.15.2"
)

How should my build.sbt be configured so that all modules can access everything in this project?

Thanks

Upvotes: 1

Views: 337

Answers (1)

yǝsʞǝla
yǝsʞǝla

Reputation: 16412

First of all SBT does not allow cyclic project dependencies, i.e A -> B; B -> A. Otherwise it would not know where to start building from. Think of a project structure as of a DAG. Even if cyclic dependencies would be possible then it would not make sense to split things into projects because they have access to each other making it essentially a single project. It gets a bit hard sometimes trying to keep code in separate subprojects and thinking about access restrictions that you get. You can still get a "diamond" project dependency structure though if both your sibling projects depend on some other project: A -> C; B -> C, root -> (A, B) which is "fine".

As for the project structure you can look at documentation and many examples on internet or github. I would suggest to convert to Build.scala which should go to project directory. It will give you better control and customization. You could have something like this:

import sbt._
import Keys._

object Build extends Build {
  lazy val root = Project("root", file("."))
    .aggregate(infrastructure, domain)

  lazy val infrastructure = Project("infrastructure", file("infrastructure"))
    .settings(commonSettings)

  lazy val domain = Project("domain", file("domain"))
    .settings(commonSettings)
    .dependsOn(infrastructure)

  lazy val commonSettings =
    settings ++
    Seq(
      anorm,
      jdbc,
      cache,
      "org.scala-tools" % "maven-scala-plugin" % "2.15.2")
}

Notice that root project (you can rename it to Main, whatever) is just an aggregate project to conveniently build everything else. It does not contain any source code or any files at all.

Upvotes: 1

Related Questions