SBT 插件:如何将编译器插件添加为不向下游传播的依赖项? [英] SBT Plugin: How to add compiler plugin as a dependency that is not propagated downstream?

查看:43
本文介绍了SBT 插件:如何将编译器插件添加为不向下游传播的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 SBT 插件.我想使用 Circe JSON 库,但它需要 Scala 2.10 上的 Macro Paradise 编译器插件.

通常您将编译器插件添加到 build.sbt 并将 SBT 插件添加到 project/plugins.sbt.

现在,当您构建 SBT 插件时,其他插件将成为依赖项,因此您将它们放在 build.sbt 中,然后它们会传播到您使用 SBT 插件的项目中.

当我将以下代码片段放入 SBT 插件的 build.sbt 时:

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

Paradise 编译器插件是否会传播到下游项目?

解决方案

编译器插件默认不传播,但实际上下游用户需要它们作为依赖,你没有办法绕过这个要求.

原因很简单,他们的代码会在不同的编译单元中编译,所以只要你有依赖编译器插件的特性,会在最后的代码库中找到,你也需要贴个注释在此插件上显式添加依赖项.

希望这会有所帮助,并以真正流行的 Monocle lib 此处为例.例如,如果没有天堂,注释就不会扩展,所以这完全是一个最终用户需要什么的问题.

报价

如果要使用@Lenses等宏注释,还需要包含:

addCompilerPlugin("org.scalamacros" %% "paradise" % "2.1.0" crossVersion.full)

I'm writing a SBT plugin. I would like to use Circe JSON library, but it requires the Macro Paradise compiler plugin on Scala 2.10.

Normally you add compiler plugins to build.sbt and SBT plugins to project/plugins.sbt.

Now when you're building a SBT plugin, the other plugins become dependencies, so you put them to build.sbt and they are propagated to the projects where you use your SBT plugin.

When I put the following snippet in build.sbt of my SBT plugin:

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

Does the Paradise compiler plugin propagate to downstream projects?

解决方案

Compiler plugins are not propagated by default, but they will in fact be required by downstream users as a dependency, and there is no way for you to bypass this requirement.

The reason is simple, their code will be compiled in a different compilation unit, so as long as you have features that depend on the compiler plugin that will be found in the end codebase, you'll also need to stick a note on this plugin to explicitly add the dependency.

Hope this helps, and take for example the really popular Monocle lib here. Annotations won't expand without paradise for instance, so it's all a question of what the end user will need.

Quote

If you want to use macro annotations such as @Lenses, you will also need to include:

addCompilerPlugin("org.scalamacros" %% "paradise" % "2.1.0" cross CrossVersion.full)

这篇关于SBT 插件:如何将编译器插件添加为不向下游传播的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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