Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT Run Main Method from a Sub Project

Tags:

scala

sbt

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
like image 813
Knows Not Much Avatar asked May 08 '16 21:05

Knows Not Much


People also ask

What is ThisBuild in sbt?

Typically, if a key has no associated value in a more-specific scope, sbt will try to get a value from a more general scope, such as the ThisBuild scope. This feature allows you to set a value once in a more general scope, allowing multiple more-specific scopes to inherit the value.

How do we specify library dependencies in sbt?

The libraryDependencies key Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.


1 Answers

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.

like image 77
Martin Avatar answered Oct 23 '22 11:10

Martin