如何使用使用它的项目在多项目构建中开发sbt插件?

时间:2014-06-15 10:08:10

标签: sbt

是否可以在多项目设置中构建sbt插件并在同一个多项目的其他子项目中使用该插件?

例如:

root/
+  mySbtPlugin/
+  myProject/
   +  project/
      +  plugins.sbt    // Uses `mySbtPlugin`

1 个答案:

答案 0 :(得分:6)

可能,因为为单个或多个模块项目定义插件的唯一方法是通过project(meta)构建。当我使用您描述的布局设置沙箱环境时,我再次被欺骗以获得解决方案。

sbt允许project(meta)项目仅在根目录中。没有其他project目录被处理并成为构建定义的一部分。

这就是为什么你最好的(也是唯一的)赌注是为myProjectmySbtPlugin构建多模块以简化开发,并且只为这些项目启用插件想要(小心auto-plugins)。

<强>项目/ plugins.sbt

lazy val root = (project in file(".")) dependsOn sbtNonamePlugin

lazy val sbtNonamePlugin = ProjectRef(file("../sbt-noname"), "sbt-noname")

addSbtPlugin("pl.japila" % "sbt-noname" % "1.0")

<强> build.sbt

lazy val `my-project`, `sbt-noname` = project

<强> SBT-NONAME / build.sbt

sbtPlugin := true

name := "sbt-noname"

organization := "pl.japila"

version := "1.0"

<强> sbtnoname / SRC /主/阶/ sbtnoname / Plugin.scala

package sbtnoname

import sbt._
import plugins._

object Plugin extends AutoPlugin {

  override def trigger = allRequirements

  override val projectSettings: Seq[Setting[_]] = inConfig(Test)(baseNonameSettings)

  lazy val sayHello = taskKey[Unit]("Say hello")

  lazy val baseNonameSettings: Seq[sbt.Def.Setting[_]] = Seq(
    sayHello := {
      println("I'm the plugin to say hello")
    }
  )
}

使用上述文件,运行sbt

> about
[info] This is sbt 0.13.6-SNAPSHOT
[info] The current project is {file:/Users/jacek/sandbox/multi-plugin/}my-project 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.4
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, sbtnoname.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
> projects
[info] In file:/Users/jacek/sandbox/multi-plugin/
[info]   * multi-plugin
[info]     my-project
[info]     sbt-noname
> plugins
In file:/Users/jacek/sandbox/multi-plugin/
    sbt.plugins.IvyPlugin: enabled in multi-plugin, sbt-noname, my-project
    sbt.plugins.JvmPlugin: enabled in multi-plugin, sbt-noname, my-project
    sbt.plugins.CorePlugin: enabled in multi-plugin, sbt-noname, my-project
    sbt.plugins.JUnitXmlReportPlugin: enabled in multi-plugin, sbt-noname, my-project
    sbtnoname.Plugin: enabled in multi-plugin, sbt-noname, my-project
> my-project/test:sayHello
I'm the plugin to say hello
[success] Total time: 0 s, completed Jun 15, 2014 3:49:50 PM