1

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

malmling
  • 2,398
  • 4
  • 19
  • 33

1 Answers1

1

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.

yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65
  • this makes sense. But if I want one module to be a play framework project that the other modules need to be a part of. I mean that the play-module will have a application.conf file with the database setup and that the infrastructure-module needs to access that database. I'm trying to build it like this because when I create a play project and instead have three different packages, like (presentation, domain, infrastructure) that doesnt work because you cannot or shouldn't modify the default packages in a play project. How would you do that without making it too complex? – malmling Feb 03 '14 at 14:49
  • I think your Scala code should be able to find application.conf from classpath. If classpath is properly set it does not matter where exactly application.conf resides. Package names can be anything and reside in any project, you don't have to name packages after your project. Finally you can change `root` project to a regular project and use `dependsOn` instead of `aggregate`. – yǝsʞǝla Feb 03 '14 at 15:48