在 sbt 中添加仅编译时的子项目依赖项 [英] Add a compile time only sub-project dependency in sbt
问题描述
我有一个多项目包含一个私有宏子项目,其用途仅限于实现其他子项目的方法体.它也不应该在其他子项目的运行时类路径上,也不应该以任何形式在其他子项目的已发布 POM 中可见.这样其他 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.
对于外部依赖,我发现了这个 SO Q&A 工作得很好,但是对于子项目,当我尝试对 dependsOn
做类似的事情时,sbt 抱怨找不到配置compileonly".
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")
推荐答案
这个错误是因为项目没有那个配置.
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)
但问题是
macro#macro_2.10;0.1-SNAPSHOT: 配置在宏#macro_2.10;0.1-SNAPSHOT: 'compileonly' 中不公开.它是 lib#lib_2.10;0.1-SNAPSHOT 编译所必需的
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
解决办法是
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)
<小时>
您可能还想熟悉 提供的
配置.这是一个标准的 Maven/Ivy 配置,这意味着 jar 将在运行时在类路径上提供(例如,像 JDK 或 servlet 容器),但不会在编译时提供.
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.
这篇关于在 sbt 中添加仅编译时的子项目依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!