如何在使用它的项目的多项目构建中开发 sbt 插件? [英] How to develop sbt plugin in multi-project build with projects that use it?

查看:28
本文介绍了如何在使用它的项目的多项目构建中开发 sbt 插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to build an sbt plugin in a multi-project setup and use that plugin in some other sub-project of the same multi-project?

例如:

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

推荐答案

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

It is not possible since the only way to define plugins for a single or multi-module project is via project (meta)build. I was tricked again to have a solution for you when I set up the sandbox environment with the layout you described.

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

sbt allows project (meta)project to be in the root directory only. No other project directories are processed and become part of the build definition.

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

That's why your best (and only) bet is to have the multi-module build for myProject and mySbtPlugin to ease your development, and enable the plugin only for these project(s) you really want to (be careful with auto-plugins, though).

project/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"

sbt-noname/src/main/scala/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.

With the files above, run 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

这篇关于如何在使用它的项目的多项目构建中开发 sbt 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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