如何在多个项目之间共享 sbt 插件配置? [英] How to share sbt plugin configuration between multiple projects?

查看:63
本文介绍了如何在多个项目之间共享 sbt 插件配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有许多较小的 sbt 项目,我们希望将它们分开(没有多项目构建).但是,我们希望在构建之间共享配置.为了做到这一点,我们目前还有另一个项目,所有项目都将其用作它们的 project/project/Build.scala 文件中的 libraryDependency.我们能够以这种方式仅使用内置的 sbt 功能来集中构建配置.

We have a multitude of smaller sbt projects, which we would like to keep separate (no multi-project build). However we would like to share configuration between the builds. In order to do so we currently have yet another project, which all the projects use as a libraryDependency in their project/project/Build.scala files. We were able to centralize build configuration using only built-in sbt features this way.

我们还没有弄清楚如何集中插件特定的构建配置.例如,我们将 sbt-assembly 用于我们的几个服务.我们正在调整程序集合并策略,并希望在所有项目中使用它.我们如何重用配置?

What we haven't figured out is how to centralize plugin specific build configuration. For example, we use sbt-assembly for several of our services. We are adapting the assembly mergeStrategy and would like to use that in all projects. How can we re-use the configuration?

此外,我们如何自动使某些 sbt 插件可用于我们的所有构建,例如程序集或标量插件,而无需手动将其添加到每个构建中?

Also how can we automatically make certain sbt-plugins available to all of our builds, such as the assembly or scalariform plugins, without manually adding it to each build?

推荐答案

这是我们为自动插件设计的使用模式之一.自动插件可以依赖 sbt-assembly 或 sbt-scalariform 并自动将设置引入所有项目.

This is one of the usage pattern we had in mind for auto plugins. Auto plugins can depend on sbt-assembly or sbt-scalariform and introduce the settings automatically to all projects.

sbt-your-company 可以在 build.sbt 中包含 sbt-scalariform 作为库依赖项,如下所示:

sbt-your-company can include sbt-scalariform as a library dependency as follows in build.sbt:

lazy val commonSettings = Seq(
  organization := "com.example",
  version := "0.1.0"
)

lazy val root = (project in file(".")).
  settings(
    commonSettings ++
    addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0")
  : _*).
  settings(
    sbtPlugin := true,
    name := "sbt-your-company",
    // other settings here
  )

由于 addSbtPlugin(...) 通常只出现在 project/plugins.sbt 中,这可能有点奇怪,但如果你看一下 addSbtPlugin 基本上是带有一些额外属性的 libraryDependencies:

Since addSbtPlugin(...) normally appear only in project/plugins.sbt this may be a bit odd, but if you look at the implementation of addSbtPlugin is basically libraryDependencies with a few extra attributes:

def addSbtPlugin(dependency: ModuleID): Setting[Seq[ModuleID]] =
  libraryDependencies <+= (sbtBinaryVersion in update, scalaBinaryVersion in update) { (sbtV, scalaV) => sbtPluginExtra(dependency, sbtV, scalaV) }

src/main/scala/foo/houserules/FormatPlugin.scala

自动插件添加的功能之一是能够基于其他插件触发插件.通过基于 plugins.JvmPlugin 触发你的插件,你可以有效地制作一个默认启用的插件.这是一个名为 FormatPlugin 的插件示例:

src/main/scala/foo/houserules/FormatPlugin.scala

One of the feature added by auto plugin is the ability to trigger a plugin based on other plugins. By triggering your plugin based on plugins.JvmPlugin, you can effectively make a plugin that's enabled by default. Here's an example plugin called FormatPlugin:

package foo
package houserules

import sbt._
import Keys._
import com.typesafe.sbt.SbtScalariform.{ ScalariformKeys => sr, _ }

object FormatPlugin extends AutoPlugin {
  override def requires = plugins.JvmPlugin
  override def trigger = allRequirements

  override def projectSettings: Seq[Def.Setting[_]] = baseSettings

  lazy val baseSettings: Seq[Setting[_]] = scalariformSettings ++ Seq(
    sr.preferences := formatPrefs
  )

  def formatPrefs = {
    import scalariform.formatter.preferences._
    FormattingPreferences().setPreference(AlignSingleLineCaseStatements, true)
  }
}

此插件引入了 sbt-scalariform 的 scalariformSettings 和自定义格式首选项.

This plugin introduces sbt-scalariform's scalariformSettings and custom format preferences.

您所要做的就是将 sbt-your-company 包含到您的构建中.如果您的项目之一想要退出 FormatPlugin,您可以使用 disablePlugins(...).请参阅使用插件.

All you have to do is include sbt-your-company to your builds. In case one of your projects want to opt-out of the FormatPlugin you can use disablePlugins(...). See Using plugins.

有关完整示例,另请参阅我刚刚创建的 sbt-houserules.(巧合的是,我一直想为 sbt 模块化创建这个.)

For full example, see also sbt-houserules I just created. (Coincidentally I've been meaning to create this for sbt modularization.)

您还可以使用自动插件来配置其他内容,例如 Akka 版本等公共库的版本,或排除所有日志库等.

You can use also auto plugins to configure other things like version of common libraries like Akka version, or excluding all logging libraries etc.

这篇关于如何在多个项目之间共享 sbt 插件配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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