如何为测试源添加 Scala 编译器插件 [英] How to add Scala compiler plugin only for test sources

查看:35
本文介绍了如何为测试源添加 Scala 编译器插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅在编译测试源时添加 Scala 编译器插件?

Is it possible to add a Scala compiler plugin only when compiling test sources?

当通过调用 SBT 的 addCompilerPlugin 添加编译器插件时,会添加库依赖项.相关方法有:

When a compiler plugin is added by calling SBT's addCompilerPlugin then a library dependency is added. The relevant methods are:

  /** Transforms `dependency` to be in the auto-compiler plugin configuration. */
  def compilerPlugin(dependency: ModuleID): ModuleID =
    dependency.withConfigurations(Some("plugin->default(compile)"))

  /** Adds `dependency` to `libraryDependencies` in the auto-compiler plugin configuration. */
  def addCompilerPlugin(dependency: ModuleID): Setting[Seq[ModuleID]] =
    libraryDependencies += compilerPlugin(dependency)

问题归结为是否有 withConfigurations 导致插件仅在编译测试源时位于类路径上.我尝试了 Some("plugin->default(testCompile)") 没有成功.

The question boils down if there is a withConfigurations that causes the plugin to be on the classpath only when compiling test sources. I tried Some("plugin->default(testCompile)") without success.

推荐答案

回答我自己的问题:可以通过将 autoCompilerPlugins 设置为 false 并添加必要的 -Xplugin=... Scalac 选项在测试配置中手动进行.这可以通过使用 Classpaths.autoPlugins 实用方法来完成.

To answer my own question: It is possible by setting autoCompilerPlugins to false and adding the necessary -Xplugin=... Scalac option manually in the test configuration. This can be done by using the Classpaths.autoPlugins utility method.

就我而言,我只想在测试期间激活 SemanticDB 编译器插件.这可以通过以下 SBT 设置来实现:

In my case I wanted to activate the SemanticDB compiler plugin during tests only. This can be accomplished by the following SBT settings:

    autoCompilerPlugins := false,
    ivyConfigurations += Configurations.CompilerPlugin,
    scalacOptions in Test ++= Classpaths.autoPlugins(update.value, Seq(), ScalaInstance.isDotty(scalaVersion.value)),
    scalacOptions in Test += "-Yrangepos",
    scalacOptions in Test += "-P:semanticdb:text:on",

这篇关于如何为测试源添加 Scala 编译器插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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