dependency is a library to parse and handle Java and Scala dependencies. It features:
- support for both Java and Scala dependencies (simply or fully cross-versioned, Scala.JS and Scala Native dependencies)
- support for exclusions, override URLs for artifacts, …
- support for Ivy module attributes
toString
representation that can be parsed back- powerful string interpolator
- Scala binary version computation
- …
Add io.get-coursier::dependency:0.2.3
to your build:
// sbt
libraryDependencies += "io.get-coursier" %% "dependency" % "0.2.3"
// mill
def ivyDeps = Agg(ivy"io.get-coursier::dependency:0.2.3")
The examples below assume dependency._
is imported:
import dependency._
dependency is published for Scala 2.12, 2.13, and 3.
import dependency.parser.DependencyParser
val input = "io.get-coursier::coursier:2.0.6"
val maybeDependency: Either[String, AnyDependency] = DependencyParser.parse(input)
val dep = maybeDependency.toOption.get
val params = ScalaParameters("2.13.6")
val javaDep: Dependency = dep.applyParams(params)
assert(javaDep.toString == "io.get-coursier:coursier_2.13:2.0.6")
val platformParams = ScalaParameters("2.13.6").copy(platform = Some("myplatform2"))
val otherJavaDep: Dependency = dep"io.get-coursier::coursier::2.0.6".applyParams(platformParams)
assert(otherJavaDep.toString == "io.get-coursier:coursier_myplatform2_2.13:2.0.6")
val dep1: AnyDependency = dep"io.get-coursier::coursier:2.0.6"
val depName = "coursier"
val ver = "2.0.6"
val dep2: AnyDependency = dep"io.get-coursier::$depName:$ver"
The interpolated strings are validated at compile-time. As a consequence, string parameters
(depName
, ver
above) should not contain separators (these will be part of the name, version, …,
else).
assert(dep.organization == "io.get-coursier")
assert(dep.name == "coursier")
assert(javaDep.name == "coursier_2.13")
assert(dep.version == "2.0.6")
import dependency.parser.ModuleParser
val moduleInput = "io.get-coursier::coursier"
val maybeModule: Either[String, AnyModule] = ModuleParser.parse(moduleInput)
val mod = maybeModule.toOption.get
val javaMod: Module = mod.applyParams(params)
assert(javaMod.toString == "io.get-coursier:coursier_2.13")
val mod1: AnyModule = mod"io.get-coursier::coursier"
val modName = "coursier"
val mod2: AnyModule = mod"io.get-coursier::$modName"
The interpolated strings are validated at compile-time. As a consequence, string parameters
(modName
above) should not contain separators (these will be part of the name, …,
else).
assert(mod.organization == "io.get-coursier")
assert(mod.name == "coursier")
assert(javaMod.name == "coursier_2.13")
Exclusions can be specified in the string representations of dependencies:
val depWithExclusions = dep"io.get-coursier::coursier:2.0.6,exclude=io.argonaut%%argonaut,exclude=org.fusesource.jansi%jansi"
assert(depWithExclusions.exclude == CovariantSet(mod"io.argonaut::argonaut", mod"org.fusesource.jansi:jansi"))
(Note the use of %
as a separator in excluded dependencies).
Custom parameters can be passed in dependencies:
val depWithParams = dep"io.get-coursier::coursier:2.0.6,url=https://dl.cs/cs.jar,intransitive"
assert(depWithParams.userParams == Map("url" -> Some("https://dl.cs/cs.jar"), "intransitive" -> None))
assert(ScalaVersion.binary("2.13.6") == "2.13")
assert(ScalaVersion.binary("3.0.0") == "3")
assert(ScalaVersion.jsBinary("1.5.1") == Some("1"))
assert(ScalaVersion.nativeBinary("0.4.0") == Some("0.4"))
Add a dependency towards io.get-coursier::dependency-interface
:
Add io.get-coursier::dependency-interface:0.2.3
to your build:
// sbt
libraryDependencies += "io.get-coursier" %% "dependency-interface" % "0.2.3"
// mill
def ivyDeps = Agg(ivy"io.get-coursier::dependency-interface:0.2.3")
Then do
import dependency.api.ApiConverter
ApiConverter.module(mod"io.get-coursier::coursier") // coursierapi.Module
ApiConverter.dependency(dep"io.get-coursier::coursier:2.0.6".applyParams(ScalaParameters("2.13.6"))) // coursierapi.Dependency
Alternatively, the dependency-interface module also provides implicit conversions for these:
import dependency.api.ops._
mod"io.get-coursier::coursier".toCs // coursierapi.Module
dep"io.get-coursier::coursier:2.0.6".applyParams(ScalaParameters("2.13.6")).toCs // coursierapi.Dependency
Licensed under the Apache 2.0 license.