Skip to content

Library to parse and handle Java and Scala dependencies

License

Notifications You must be signed in to change notification settings

ckipp01/dependency

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dependency

Build status Maven Central

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

Usage

Add io.get-coursier::dependency:0.1.0 to your build:

// sbt
libraryDependencies += "io.get-coursier" %% "dependency" % "0.1.0"
// mill
def ivyDeps = Agg(ivy"io.get-coursier::dependency:0.1.0")

The latest version is Maven Central.

The examples below assume dependency._ is imported:

import dependency._

dependency is published for Scala 2.12, 2.13, and 3.

Dependencies

Parsing

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

Converting to pure Java dependency

val params = ScalaParameters("2.13.6")
val javaDep: Dependency = dep.applyParams(params)

assert(javaDep.toString == "io.get-coursier:coursier_2.13:2.0.6")

String interpolator

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).

Accessing fields

assert(dep.organization == "io.get-coursier")
assert(dep.name == "coursier")
assert(javaDep.name == "coursier_2.13")
assert(dep.version == "2.0.6")

Modules

Parsing

import dependency.parser.ModuleParser

val moduleInput = "io.get-coursier::coursier"
val maybeModule: Either[String, AnyModule] = ModuleParser.parse(moduleInput)
val mod = maybeModule.toOption.get

Converting to pure Java module

val javaMod: Module = mod.applyParams(params)

assert(javaMod.toString == "io.get-coursier:coursier_2.13")

String interpolator

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).

Accessing fields

assert(mod.organization == "io.get-coursier")
assert(mod.name == "coursier")
assert(javaMod.name == "coursier_2.13")

Exclusions

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).

Parameters

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))

Scala binary version

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"))

License

Licensed under the Apache 2.0 license.

About

Library to parse and handle Java and Scala dependencies

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Scala 88.9%
  • Shell 5.9%
  • Batchfile 5.2%