surenyonjan
surenyonjan

Reputation: 2109

what are the ways of declaring dependencies in sbt

In sbt documentation, they have mentioned different ways of declaring project dependencies.

libraryDependencies += groupID % artifactID % revision

libraryDependencies += groupID % artifactID % revision % configuration

libraryDependencies ++= Seq(
   groupID %% artifactID % revision,
   groupID %% otherID % otherRevision
)

However, when I created new Play2 framework with:

activator new HelloWorld play-scala

I could see following code in build.sbt:

libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
ws
)

How are they resolved (jdbc, anorm, cache, ws)? Are there some other way also to declare dependencies?

Upvotes: 0

Views: 305

Answers (1)

0__
0__

Reputation: 67300

These are all examples of the one and same approach. The libraryDependencies key carries a sequence of artifacts, each of which is defined as

groupID % artifactID % revision % configuration

in most cases omitting the optional configuration that is commonly used to restrict dependencies to be added to the "test" only scope.

If you want to declare an individual dependency, you have

libraryDependencies += single

If you have multiple, you typically add them as a batch in a sequence:

libraryDependencies ++= Seq(first, second, third)

In your last example, jdbc, anorm etc. have been previously defined as above, so you are just referring to these pre-existing values. You could also do that yourself:

lazy val myDep = groupID % artifactID % revision

libraryDependencies += myDep

To concatenate groupID and artifactID, there is a special operator %% instead of % which is useful for Scala libraries because they are often compiled against different versions of Scala, such as 2.10 and 2.11. The %% means that the Scala version is appended to the artifactID, relying on this common convention. I.e.

"com.foo" %% "bar" % "1.0"

Is equal to

"com.foo" % "bar_2.11" % "1.0"

if your project's Scala version is 2.11, or

"com.foo" % "bar_2.10" % "1.0"

if your Scala version is 2.10, etc.

Upvotes: 3

Related Questions