Add a compile time only sub-project dependency in sbt

怎甘沉沦 提交于 2019-12-21 02:34:08

问题


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

回答1:


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.




回答2:


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

Just had this discussion last night...



来源:https://stackoverflow.com/questions/35477974/add-a-compile-time-only-sub-project-dependency-in-sbt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!