Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

scala - How to set default dependencies for all subprojects in SBT?

Trying to understand how to set up SBT subprojects. What is the correct way to set default dependencies for all my sub projects?

I tried this, but my sub projects weren't picking up any of the dependencies (they downloaded fine).

import sbt._

class MyProjects(info: ProjectInfo) extends DefaultProject(info)
{
  val projA = project("projA", "ProjectA")
  val projB = project("projB", "ProjectB")

  val akkaRepo = "Akka maven2 repo" at "http://www.scalablesolutions.se/akka/repository/"
  val multiverseRepo = "Multiverse maven2 repo" at "http://multiverse.googlecode.com/svn/maven-repository/releases/"
  val guiceyFruitRepo = "GuiceyFruit Maven2 repo" at "http://guiceyfruit.googlecode.com/svn/repo/releases/"
  val jBossRepo = "JBoss maven2 repo" at "https://repository.jboss.org/nexus/content/groups/public/"

  val junit = "junit" % "junit" % "4.5" % "test"
  val scalatest = "org.scalatest" % "scalatest" % "1.2" % "test"
  val akka = "se.scalablesolutions.akka" % "akka-core_2.8.0" % "0.10"
}

Then, based on this I tried the following. It worked, but it's not what I was expecting to have to do. Isn't there a simpler was to set default dependencies for all subprojects?

import sbt._  

class MyProjects(info: ProjectInfo) extends DefaultProject(info)
{
  val projA = project("projA", "ProjectA", new Proj(_))
  val projB = project("projB", "ProjectB", new Proj(_))

  val akkaRepo = "Akka maven2 repo" at "http://www.scalablesolutions.se/akka/repository/"
  val multiversRepo = "Multiverse maven2 repo" at "http://multiverse.googlecode.com/svn/maven-repository/releases/"
  val guiceyFruitRepo = "GuiceyFruit Maven2 repo" at "http://guiceyfruit.googlecode.com/svn/repo/releases/"
  val jBossRepo = "JBoss maven2 repo" at "https://repository.jboss.org/nexus/content/groups/public/"

  class Proj(info:ProjectInfo) extends DefaultProject(info){
    val junit = "junit" % "junit" % "4.5" % "test"
    val scalatest = "org.scalatest" % "scalatest" % "1.2" % "test"
    val akka = "se.scalablesolutions.akka" % "akka-core_2.8.0" % "0.10"
  }
}

Edit: Should point out there is an better way to use Akka, but was just illustrating my point.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use inheritance and mixins:

import sbt._

class ModularProject(info: ProjectInfo) extends DefaultProject(info){

    lazy val childProject = project("projA", "ProjectA", 
        new DefaultProject(_)   
            with Repositories 
            with GlobalDependencies
            with AkkaDependencies)

    trait Repositories{
        lazy val akkaRepo = "Akka maven2 repo" at 
        "http://www.scalablesolutions.se/akka/repository/"
        lazy val multiversRepo = "Multiverse maven2 repo" at 
        "http://multiverse.googlecode.com/svn/maven-repository/releases/"
        lazy val guiceyFruitRepo = "GuiceyFruit Maven2 repo" at 
        "http://guiceyfruit.googlecode.com/svn/repo/releases/"
        lazy val jBossRepo = "JBoss maven2 repo" at 
        "https://repository.jboss.org/nexus/content/groups/public/"
    }

    trait GlobalDependencies{
        lazy val junit = "junit" % "junit" % "4.5" % "test"
        lazy val scalatest = "org.scalatest" % "scalatest" % "1.2" % "test"
    }

    trait AkkaDependencies{
        lazy val akka = "se.scalablesolutions.akka" % "akka-core_2.8.0" % "0.10"
    }       

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...