为什么不能用build.scala构建定义找到我的自定义命令?

时间:2014-10-23 18:08:16

标签: sbt

为什么sbt无法在多项目构建中找到命令?

我的插件类似于

object MyPlugin extends Plugin {

    lazy val plug = Seq( commands ++= Seq(versionWriteSnapshotRelease) )

     def versionWriteSnapshotRelease = Command.command(
         "versionWriteSnapshotRelease",
         "Writes the release format of the snapshot version. This is used to preserve the actual snapshot version in a release commit.",
         ""
     ) { state =>  .... }
}

我的project/Build.scala文件如下:

lazy val app = Project(id = "app-subproject", base = file("app"))
    .settings(MyPlug.plug :_*)
    .settings(...)

lazy val common = Project(id = "library-subproject", base = file("common"))
    .settings(MyPlug.plug :_*)
    .settings(...)

文件布局如

root
|_ common
    |_ src
|_ app
    |_ src

此配置失败,错误:

[error] Not a valid command: versionWriteSnapshotRelease
[error] Not a valid project ID: versionWriteSnapshotRelease
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: versionWriteSnapshotRelease (similar: version, ...)
[error] versionWriteSnapshotRelease

但是,如果我重组为

之类的东西
lazy val app = Project(id = "app-subproject", base = file("."))
    .settings(MyPlug.plug :_*)
    .settings(...)

lazy val common = Project(id = "library-subproject", base = file("common"))
    .settings(MyPlug.plug :_*)
    .settings(...)

文件布局如

root
|_ common
    |_ src
|_ src

然后它有效。请注意,我的更改是将app项目/src放入basedir,并将app项目设置为基础"."

此插件用于多个项目,并且在文件布局为第二种形式时没有问题。所以我知道这不是插件本身的问题。

1 个答案:

答案 0 :(得分:1)

错误的原因是您在没有命令集的情况下使用the third default root project定义了两个子模块commonapp

您应该为默认根项目添加另一个项目定义,以便为其定义命令,如下所示:

lazy val root = Project(id = "root", base = file("."))
    .settings(MyPlug.plug :_*)
    .settings(...)

您已确认在将app转换为默认根项目时考虑测试。

作为添加第三个模块定义之前的附加检查,请执行projects并查看已定义的项目。对于列表中的每个项目,执行[projectName]/versionWriteSnapshotRelease。我确定它适用于appcommon,但不适用于默认的根模块。