如何通过将 sbt-plugin 用作多项目构建中的依赖项来访问它的子项目?

How can I access sub-projects of an sbt-plugin by using it as dependency in a multi-project build?

我有一个使用多项目构建的 sbt 插件项目。我想将此插件用作其他 sbt 项目的依赖项并访问此 sbt 插件的子项目。我创建了一个插件并将插件添加到 sbt 项目,但我无法访问那里的插件子项目。

sbt-插件

build.sbt

name := "sbt-plugin"
sbtPlugin := true

val commonSettings = Seq(
    organization := "com.example",
    version := "1.0",
    scalaVersion := "2.11.7",
    javacOptions := Seq("-source", "1.8", "-target", "1.8"), 
    scalacOptions := Seq("-target:jvm-1.8", "-unchecked","-deprecation", "-encoding", "utf8")
   )

  lazy val root = (project in file("."))
                 .settings(commonSettings: _*)
                 .dependsOn(plugin)
                 .aggregate(plugin)

  lazy val plugin = (project in file("plugin"))
                 .settings(commonSettings: _*)
                 .settings(
                     name := "plugin" ,
                     mainClass in (Compile, run) := Some("com.example.Main")
    )

sbt-plugin\plugin\src\main\scala\com\example\Main.scala

package com.example

object Main {
    def main(args: Array[String]){
        println("Hello from plugin in sbt-plugin");
    }
}

sbt-plugin\plugin\src\main\scala\com\example\Hello.scala

  package com.example

  // Sample code I would like to access from another sbt project
  object Hello {
     def show = println("Hello, world!")
  }

插件测试

plugin-test 是我用来测试 sbt-plugin 的 sbt 项目

plugin-test\build.sbt

name := """plugin-test"""

val commonSettings = Seq(
    version := "1.0",
    scalaVersion := "2.11.7",
    javacOptions := Seq("-source", "1.8", "-target", "1.8"), 
    scalacOptions := Seq("-target:jvm-1.8", "-unchecked", "-deprecation", "-encoding", "utf8"),
    libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test"    
)

lazy val root = (project in file("."))
               .settings(commonSettings: _*)
               .dependsOn(pluginpro)
               .aggregate(pluginpro)
               .settings(
                   mainClass in (Compile, run) := Some("com.exam.Test")
  )

lazy val pluginpro = (project in file("pluginpro"))
                   .settings(commonSettings: _*)
                   .settings(
                        libraryDependencies += "com.example" % "plugin_2.11" % "1.0"         
    )

plugin-test\src\main\scala\com\exam\Test.scala

  package com.exam

  object Test {
     def result = com.example.Hello.show()
  }

当我从 root 运行 插件测试项目时它是 运行ning 但是下面提到的日志我不确定为什么它显示这个因为根据我的输出将只是 Hello, world!

background log: info: Running com.exam.Test
background log: debug: Waiting for threads to exit or System.exit to be called.
background log: debug: Waiting for thread run-main-0 to terminate.
background log: debug:   Classpath:
E:\Play\SBT Plugin\sbt demo1\plugin-test\target\scala-2.11\classes
E:\Play\SBT Plugin\sbt demo1\plugin-test\pluginpro\target\scala-2.11\classes
C:\Users\Jeetu\.ivy2\cache\org.scala-lang\scala-library\jars\scala-library-2.11.7.jar
C:\Users\Jeetu\.ivy2\local\com.example\plugin_2.11.0\jars\plugin_2.11.jar
Hello, world!
()
background log: debug:  Thread run-main-0 exited.
background log: debug: Interrupting remaining threads (should be all daemons).
background log: debug: Sandboxed run complete..
background log: debug: Exited with code 0  

当我尝试通过 pluginpro/run 访问 sbt-plugin 的 运行 子项目时,它找不到主项目 class。

 > pluginpro/run
[trace] Stack trace suppressed: run last pluginpro/compile:backgroundRun for the full output.
[error] (pluginpro/compile:backgroundRun) No main class detected.

我在 sbt-plugin/plugin 项目中创建了主 class。 我在两个项目上都执行了本地发布和 plugin/publish-local,并且正确解析了工件。

我在这里错过了什么?

我通过在 pluginpro 项目的 build.sbt 中添加以下内容解决了这个问题:

mainClass in (Compile, run) := Some("com.example.Main")