在sbt中添加仅编译时子项目依赖项 [英] Add a compile time only sub-project dependency in sbt

查看:159
本文介绍了在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#macro_2中不公开。 10; 0.1-SNAPSHOT:仅编译。 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆