Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a compile time only sub-project dependency in sbt

I have a multi-project contains a private macro sub-project which's usage is limited to implement method body of other sub-projects. Neither should it be on the runtime classpath of other sub-projects, nor should it be visible in any form in the published POM of other sub-projects. So that other sbt project could use library from this project without knowing the macro sub-project.

For external dependency, I found this SO Q&A works perfectly, but for sub-project when I trying to do the similar thing to dependsOn, sbt complains about configuration "compileonly" not found.

ivyConfigurations += config("compileonly").hide

val macro = Project("macro", file("macro"))

val lib = Project("lib", file("lib")).dependsOn(macro % "compile->compileonly")
like image 669
Chikei Avatar asked Feb 18 '16 09:02

Chikei


2 Answers

That error is because the project doesn't have that config.

val CompileOnly = config("compileonly").hide    

ivyConfigurations += CompileOnly

val macro = Project("macro", file("macro")).configs(CompileOnly) // add config

val lib = Project("lib", file("lib")).dependsOn(macro % CompileOnly)

But then the problem is

macro#macro_2.10;0.1-SNAPSHOT: configuration not public in macro#macro_2.10;0.1-SNAPSHOT: 'compileonly'. It was required from lib#lib_2.10;0.1-SNAPSHOT compile

The solution is

val CompileOnly = config("compileonly")

val macro = Project("macro", file("macro")).configs(CompileOnly)

val lib = Project("lib", file("lib")).dependsOn(macro % CompileOnly)
  .settings(ivyConfigurations += CompileOnly.hide)

You may also want to familiarize yourself with the provided configuration. It's a standard Maven/Ivy config that means that the jar will be provide on the classpath at runtime (e.g. like the JDK, or a servlet container), but not at compile time.

like image 173
Paul Draper Avatar answered Oct 08 '22 17:10

Paul Draper


val lib = Project("lib", file("lib")).dependsOn(macro % "compile-internal") 

Just had this discussion last night...

like image 5
pfn Avatar answered Oct 08 '22 15:10

pfn