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

时间:2017-03-29 10:49:51

标签: scala plugins sbt multi-project sbt-plugin

我有一个使用多项目构建的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-插件\插件\ SRC \主\阶\ COM \示例\ Main.scala

package com.example

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

SBT-插件\插件\ SRC \主\阶\ COM \示例\ 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项目,我用来测试sbt-plugin

插件测试\ 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"         
    )

插件测试\ SRC \主\阶\ COM \考试\ Test.scala

  package com.exam

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

当我从root运行插件测试项目时它正在运行,但是使用下面提到的日志,我不知道为什么会显示这个因为根据我的输出只有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\1.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的子项目时,它无法找到主类。

 > 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项目中创建了主类。 我在两个项目上执行了publish-local和plugin / publish-local,并正确解析了工件。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我通过在pluginpro项目中的build.sbt中添加以下内容来解决它:

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