bula
bula

Reputation: 9419

sbt build file dependency syntax(or pattern)

i just started to learn scala and the build tool I use is sbt. Here I have to write dependencies for each sample project. I copied some dependencies from internet and pasted, but I don't get the syntax(or pattern) how to write a dependency by my own. Can someone please tell me how the keywords are arranged and what is the pattern of writing a dependency.

Upvotes: 1

Views: 680

Answers (1)

4lex1v
4lex1v

Reputation: 21557

Here is a quote from the Josh Suereth book

Defining Dependencies sbt provides a convenient syntax for defining Maven/Ivy dependencies using the % method. To define a ModuleID in sbt, just write “groupId” % “artifactId” % “version” and it will automatically become an instance of a ModuleID.

Consider this dependency:

libraryDependencies += "iv.alex" % "commons" % "100500"

In this line we have three parts:

libraryDependencies              - setting key
+=                               - operator
"iv.alex" % "commons" % "100500" - initialization part

This initialization part constructs a ModuleID, which is required by libraryDependencies setting key. It consists from other three parts from the quote:

"iv.alex" - groupId
"commons" - artifactId
"100500"  - version

But libraryDependencies key is actually a Seq[ModuleID], so you have the second operator ++= which expects a sequence of moduleIds:

libraryDependencies ++= Seq("group" % "artifact" % "version", etc...)

The most common way is to store dependencies in stores like maven cetnral. So if you want to add you own dependency, for example you want to find Akka 2.2.3 for scala 2.11, first find it (link), then you need to find .pom file or some cases it would be written like here, so you need the foloowing lines:

<dependency>
    <groupId>com.typesafe.akka</groupId>
    <artifactId>akka-actor_2.10</artifactId>
    <version>2.3-M1</version>
</dependency>

It is easy to translate into sbt dependency, like i've described above:

libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3-M1"

Note! that i've written "com.typesafe.akka" %% "akka-actor" two percentage symbols, it means that i need a library with the same version of scala which i have in my scalaVersion setting key. If you notice in maven style dependency, artifact id contains scala version: akka-actor_2.10, but not in out sbt dependency. If you don't have scala version in you artifactId, then you shouldn't write it as %% leave just one %

Upvotes: 6

Related Questions