Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

buildt.sbt: exclude dependencies from dependsOn submodule

There is a similar question here but that solution does not work in sbt v1.x

In the build sbt it is well documented how to exclude dependencies when added through libraryDependencies:

libraryDependencies += "log4j" % "log4j" % "1.2.15" exclude("javax.jms", "jms")

or preventing transitive dependencies:

libraryDependencies += "org.apache.felix" % "org.apache.felix.framework" % "1.8.0" intransitive()

but my question is how (and if) it can be done when declaring dependsOn dependencies of submodules in a multi-module project like this:

lazy val core = project.dependsOn(util)

How would I do something like this (invalid code in example below) to prevent a transitive dependency from being brought in via util:

lazy val core = project.dependsOn(util exclude("javax.jms", "jms"))

also how, and more importantly, how to exclude a transitive dependency on another submodule in the multi-module project from being brought in via util (where sub3 is another submodule project declared in the same build.sbt):

lazy val core = project.dependsOn(util exclude sub3)
like image 735
IUnknown Avatar asked Sep 07 '18 14:09

IUnknown


1 Answers

The way to do it, is to use excludeDependencies SettingKey.

An short example:

excludeDependencies ++= Seq(
  ExclusionRule("commons-logging", "commons-logging")
) 

Source


If you happen to define your dependencies as val (as I do), you might find it useful to define the excludes based on your dependencies. To do so, you need this simple method:

def excl(m: ModuleID): InclExclRule = InclExclRule(m.organization, m.name)

and it allows for easy exclusions:

val theLib = "com.my.lib" % "artifact" % "version"

lazy val `projectA` = (project in file("projectA"))
  .settings(
    ...
    libraryDependencies ++= Seq(
      theLib
    )
  )

lazy val `projectB` = (project in file("projectB"))
  .settings(
    ...
    libraryDependencies ++= Seq(
      ...
    ),
    excludeDependencies ++= Seq(
      excl(theLib)
    )
  )
  .dependsOn(projectA)
like image 98
Atais Avatar answered Oct 22 '22 16:10

Atais