Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT build, run main class from subproject on Compile and run

I have a simple build tool Multi-Project problem...

I have the following directory structure represents my java sbt projects:

/project1
/project2
/project3

So all projects share a common direct parent folder. Projects 2 and 3 are referenced in project 1's build.sbt like this:

.dependsOn(project2, project3)
.aggregate(project2, project3)

lazy val project2 = ProjectRef(file("../project2"), "project2")

lazy val project3 = ProjectRef(file("../project3"), "project3")

This way there's a dependency between project1 and the others.

All is fine to this point and everything works as it should.

But now I want to execute the main method from project2 before anything else is executed. When I execute the "run" task from the parent (project1), I want a specific class from project2 to have its main method executed. How do I do this? The sbt documentation explains that "Aggregation means that running a task on the aggregate project will also run it on the aggregated projects.": http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Multi-Project.html#aggregation

I'm not seeing my main class on projet2 been executed. I also added this to project2's build.sbt:

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

The goal of the projet is to generate code at Compiletime and runtime. Project2's job is to generate Java and Javascript code. The could should be generated before the other projects are built.

Is that possible? If not, I'll have to settle for running project2 independently from the other projects.

=]

like image 756
Misael Neto Avatar asked Aug 24 '14 21:08

Misael Neto


People also ask

Does sbt run compile?

sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file.

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.


1 Answers

if i have a structure as indicated below:

+ root +--- backend +--- frontend and a build.sbt project similar as indicated in http://www.scala-sbt.org/0.13/docs/Multi-Project.html , lets say:

lazy val commonSettings = Seq(
  version := "0.1.0-SNAPSHOT",
  scalaVersion := "2.12.1",
  resolvers := allResolvers,
  libraryDependencies := AllLibraryDependencies
)

lazy val client = (project in file("client")).
  //  .enablePlugins(PlayScala)
  settings(commonSettings: _*).
  settings(
    name := "client"
  )
  .aggregate(common, frontend, backend)
  .dependsOn(common, frontend, backend)


lazy val common = (project in file("common")).
  settings(commonSettings: _*).
  settings(
    name := "common"
  )

lazy val frontend = (project in file("frontend")).
  settings(commonSettings: _*).
  settings(
    name := "frontend"
  )
  .aggregate(common)
  .dependsOn(common)

lazy val backend = (project in file("backend")).
  settings(commonSettings: _*).
  settings(
    name := "backend"
  )
  .aggregate(common)
  .dependsOn(common)

`

Then to execute i.e a class inside frontend project, this command has worked for me:

sbt "frontend/runMain sample.cluster.transformation.frontend.TransformationFrontendApp 2551"

like image 190
andhdo Avatar answered Oct 26 '22 13:10

andhdo