简体   繁体   中英

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

I have an sbt plugin project that uses multi-project build. I would like to use this plugin as a dependency for the other sbt project and access sub-project of this sbt plugin. I have created a plugin and I added plugin to an sbt project, but i am not able to access sub-project of plugin there.

sbt-plugin

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

plugin-test is an sbt project which i used to test sbt-plugin

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()
  }

when i run plugin-test project from root it is running but with below mentioned log and i am not sure why is it showing this because according to me output would be only 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  

When i try to run sub-project of sbt-plugin via pluginpro/run , it can't find main class.

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

i have created main class in sbt-plugin/plugin project. I performed publish-local and plugin/publish-local on both projects and the artifacts resolved correctly.

What am I missing here?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM