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

sin*_*raj 4 sbt

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

例如:

root/
+  mySbtPlugin/
+  myProject/
   +  project/
      +  plugins.sbt    // Uses `mySbtPlugin`
Run Code Online (Sandbox Code Playgroud)

Jac*_*ski 6

这是不是可能的,因为只有这样,才能定义插件单个或多个模块项目通过是project(元)版本.当我使用您描述的布局设置沙盒环境时,我再次被欺骗为您提供解决方案.

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

这就是为什么你最好的(也是唯一的)赌注是让多模块构建myProjectmySbtPlugin简化你的开发,并且只为你真正想要的这些项目启用插件(不过要小心自动插件).

项目/ 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")
Run Code Online (Sandbox Code Playgroud)

build.sbt

lazy val `my-project`, `sbt-noname` = project
Run Code Online (Sandbox Code Playgroud)

SBT-NONAME/build.sbt

sbtPlugin := true

name := "sbt-noname"

organization := "pl.japila"

version := "1.0"
Run Code Online (Sandbox Code Playgroud)

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")
    }
  )
}
Run Code Online (Sandbox Code Playgroud)

使用上面的文件,运行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
Run Code Online (Sandbox Code Playgroud)