简体   繁体   中英

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM