14

I came across this code:

import sbt._ 

class AProject(info: ProjectInfo) extends DefaultProject(info) { 
  val scalaToolsSnapshots = ScalaToolsSnapshots
  val scalatest = "org.scalatest" % "scalatest" %
    "1.0.1-for-scala-2.8.0.RC1-SNAPSHOT"
}

And I'm quite confused as to what scalatest contains, and what the % does.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184

2 Answers2

12

It declares a dependency. In particular,

val scalatest = "org.scalatest" % "scalatest" % "1.0.1-for-scala-2.8.0.RC1-SNAPSHOT

refers to a dependency which can be found at

http://scala-tools.org/repo-snapshots/org/scalatest/scalatest/1.0.1-for-scala-2.8.0.RC1-SNAPSHOT/

Where everything before org refers to the repository, which is (pre-)defined elsewhere.

It is not easy to find the implicit that enables % on String, but, for the record, it is found on ManagedProject, converting a String into a GroupID. In the same trait there's also another implicit which enables the at method.

At any rate, the implicit will turn the first String into a GroupID, the first % will take a String representing the artifact ID and return a GroupArtifactID, and the second will take a String representing the revision and return a ModuleID, which is what finally gets assigned to scalatest.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • Actually, sbt checks multiple servers for the package, doesn't it? – Raphael Mar 11 '11 at 19:11
  • @Raphael By default, yes, it does. I said that the dependency _can be found_ at that path, not that it is the only place it will look for. On the other hand, the stuff before `org` is what will be changing for each look up. – Daniel C. Sobral Mar 11 '11 at 21:34
  • 2
    A follow-up question is what does double percent `%%` mean in the same case. I feel like I've seen it asked and answered here or somewhere else but I can't find it :-( – Ivan Apr 07 '12 at 19:50
6

If you used Maven this is essentially the same thing but with Scala DSL. % works as a separator:

<dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest</artifactId>
      <version>1.0.1-for-scala-2.8.0.RC1-SNAPSHOT</version>      
</dependency>

Read more: http://code.google.com/p/simple-build-tool/wiki/LibraryManagement

Rustem Suniev
  • 1,149
  • 9
  • 8