Knows Not Much
Knows Not Much

Reputation: 31546

SBT Run Main Method from a Sub Project

I am writing a project which contains scala macros. This is why I have organized my project into two sub projects called "macroProj" and "coreProj". The "coreProj" depends on "macroProj" and also contains the main method to run my code.

My requirement is that when I do a sbt run from the root project, the main method is taken from the coreProj sub project. I search and found a thread which has a solution for this

sbt: run task on subproject

Based on this my build.sbt looks like

lazy val root = project.aggregate(coreProj,macroProj).dependsOn(coreProj,macroProj)

lazy val commonSettings = Seq(
    scalaVersion := "2.11.7",
    organization := "com.abhi"
)

lazy val coreProj = (project in file("core"))
    .dependsOn(macroProj)
    .settings(commonSettings : _*)
    .settings(
        mainClass in Compile := Some("demo.Usage")
    )

lazy val macroProj = (project in file("macro"))
    .settings(commonSettings : _*)
    .settings(libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value)

mainClass in Compile <<= (run in Compile in coreProj)

But when I do sbt run from the root directory. I get an error

/Users/abhishek.srivastava/MyProjects/sbt-macro/built.sbt:19: error: type mismatch;
 found   : sbt.InputKey[Unit]
 required: sbt.Def.Initialize[sbt.Task[Option[String]]]
mainClass in Compile <<= (run in Compile in coreProj)
                                         ^
[error] Type error in expression

Upvotes: 5

Views: 2452

Answers (1)

Martin
Martin

Reputation: 2028

In sbt, mainClass is a task that will return an entry point to your program, if any.

What you want to do is to have mainClass be the value of mainClass in coreProj.

You can achieve that this way:

mainClass in Compile := (mainClass in Compile in coreProj).value

This could also be achieved with

mainClass in Compile <<= mainClass in Compile in coreProj

However, the preferred syntax is (...).value since sbt 0.13.0.

Upvotes: 6

Related Questions