21

In Build.scala I have a dependency between projects:

val coreLib = Projects.coreLib()
val consoleApp = Projects.consoleApp().dependsOn(coreLib)
val androidApp = Projects.androidProject().dependsOn(coreLib/*, exclusions = xpp */)

Core library project defines a library in its libraryDependencies (XPP parser), which I want to exclude in androidApp, since Android framework have its own XPP implementation out of the box.

How can I exclude XPP library from transitive dependencies of coreLib in androidApp project?

EDIT:

According to my research exclusion is possible ONLY to ModuleID which is used in conjunction with libraryDependency. Meanwhile dependsOn puts all transitive dependencies to classpath, there is no way in api to exclude some transitive dependencies of this project, you dependsOn

DETAILS:

I'm running sbt 0.13.5 currently.

libraryDependencies of commonLib as well as it various settings supplied in build.sbt so that this project could be reused as standalone, and because it feels right and natural way of supplying settings in sbt.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
dant3
  • 966
  • 9
  • 26

1 Answers1

34

This appears to work for me:

val someApp = project.settings(
  libraryDependencies += "junit" % "junit" % "4.11"
)

val androidApp = project.dependsOn(someApp).settings(
  projectDependencies := {
    Seq(
      (projectID in someApp).value.exclude("junit", "junit")
    )
  }
)

What the projectDepenendencies is doing is what sbt, by default, attempts to do. It converts any inter-project dependencies into ModuleIDs which Ivy will use during resolution. Because the Project API has no way to specify excludes currently, we bypass this automatic layer and manually declare the Ivy dependency as well.

Result:

> show someApp/update
...
[info] Update report:
...
[info]  compile:
[info]      org.scala-lang:scala-library:2.10.4 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/home/jsuereth/.sbt/boot/scala-2.10.4/lib/scala-library.jar)
[info]      junit:junit:4.11: (Artifact(junit,jar,jar,None,ArraySeq(master),None,Map()),/home/jsuereth/.ivy2/cache/junit/junit/jars/junit-4.11.jar)
[info]      org.hamcrest:hamcrest-core:1.3: (Artifact(hamcrest-core,jar,jar,None,ArraySeq(master),None,Map()),/home/jsuereth/.ivy2/cache/org.hamcrest/hamcrest-core/jars/hamcrest-core-1.3.jar)
 ...

And the dependent project with junit/hamcrest excluded:

> show androidApp/update
...
[info] Update report:
...
[info]  compile:
[info]      org.scala-lang:scala-library:2.10.4 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/home/jsuereth/.sbt/boot/scala-2.10.4/lib/scala-library.jar)
[info]      someapp:someapp_2.10:0.1-SNAPSHOT: 
...
jsuereth
  • 5,604
  • 40
  • 40
  • It appears to be working solution. Note that if you wan't to do the same trick with scala library, supply scala library version postfix (the ```_2.10``` thing) in it's module name (second argument of ```exclude``` method). – dant3 Aug 05 '14 at 13:52
  • 4
    This ought to be in the SBT documentation. Confirmed that this also works with SBT 0.13.7. – david.perez Jan 20 '15 at 16:51
  • I get the following error when I attempt this: error: not found: value projectId – autodidacticon Dec 17 '15 at 22:42
  • 1
    @autodidacticon, it should be `projectID` not `projectId`. It's defined under `sbt.Keys.projectID` – lyjackal Mar 22 '18 at 20:11