如何设置Main类使用sbt-assembly插件为SBT多项目运行

时间:2014-11-23 13:41:42

标签: scala sbt-assembly

这是我在文件系统上的项目结构:

rootProj
  +- masterProj
  +- proj1
  ...
  +- projN

我正在尝试使用sbt-assembly插件来创建一个胖罐。我想将主类设置为

masterProj/core/Main.scala

从rootProj目录组装执行assemby命令时获取可运行的胖jar

如何在rootProj / build.sbt中设置mainClass?

我设置

mainClass in assembly := Some("core.Main") // fully qualified path

但是当我尝试在装配后运行jar时

rootProj$ java -jar target/scala-2.10/rootProj-assembly-0.1-SNAPSHOT.jar

我收到此错误

Exception in thread "main" java.lang.NoClassDefFoundError: core/Main
Caused by: java.lang.ClassNotFoundException: core.Main
  at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

任何提示?

这是我的build.sbt

import AssemblyKeys._

assemblySettings

name := "deals-backend"

version := Common.version

scalaVersion := Common.scalaVersion

resolvers ++= Seq(
  "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
  "Sonatype repository"    at "https://oss.sonatype.org/service/local/staging/deploy/maven2"
)

scalacOptions in ThisBuild ++= Seq("-unchecked", "-deprecation", "-feature")

retrieveManaged := true

lazy val dealsClassifier   = Project( "deals-classifier", file("deals-classifier") )

lazy val dealsEntities     = Project( "deals-entities-pub", file("deals-entities-pub") )

lazy val dealsDbPre        = Project( "deals-db-pre", file("deals-db-pre") )

lazy val dealsDbPub        = Project( "deals-db-pub", file("deals-db-pub") )

lazy val dealsSolrClient   = Project( "deals-solr-client", file("deals-solr-client") ).dependsOn(dealsDbPub % "test->test;compile->compile")

lazy val dealsMasterSpray  = Project( "deals-master-spray", file("deals-master-spray") )
           .dependsOn(dealsClassifier, dealsEntities, dealsDbPre, dealsDbPub, dealsSolrClient)
           .settings(mainClass in (Compile, run) := Some("core.Main"))

lazy val root = project.in( file(".") ).aggregate(dealsClassifier, dealsEntities, dealsDbPre, dealsDbPub, dealsSolrClient, dealsMasterSpray)

run in Compile <<= (run in Compile in dealsMasterSpray)

mainClass in (Compile, assembly) := Some("deals-master-spray.core.Main")

0 个答案:

没有答案