Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT run code in project after compile

Tags:

sbt

we need to run some code after the compile step. Making things happen after the compile step seems easy:

compile in Compile <<= (compile in Compile) map{x=>
    // post-compile work
    doFoo()
    x
}

but how do you run something in the freshly compiled code?

More info on the scenario: we are using less for css in a lift project. We wanted lift to compile less into css on the fly (if needed) to help dev, but produce less using the same code, during the build, before tests etc run. less-sbt may help but we are interested in how to solve this generally.

like image 943
Channing Walton Avatar asked Jan 29 '14 14:01

Channing Walton


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.

How do I compile a Scala project?

Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Build Tools | sbt. In the sbt projects section, select a project for which you want to configure build actions. In the sbt shell section, select the builds option. Click OK to save the changes.

How do I exit sbt?

To leave sbt shell, type exit or use Ctrl+D (Unix) or Ctrl+Z (Windows).


1 Answers

You can use the triggeredBy method like this:

yourTask <<= (fullClasspath in Runtime) map {classpath =>
  val loader: ClassLoader = ClasspathUtilities.toLoader(classpath.map(_.data).map(_.getAbsoluteFile))
  loader.loadClass("your.class.Here").newInstance()
} triggeredBy(compile in Compile)

This will instantiate your class that has just been compiled, using the runtime classpath for your application, after any compile.

like image 163
Raymond Barlow Avatar answered Dec 05 '22 00:12

Raymond Barlow