0

My company is switching from ant to sbt to ease Scala integration into our huge Java existing code (smart move if you ask me). After compiling, we usually post-process all the generated .class with a tool of our own which is a result of the compilation.

I have been trying to do the same in sbt and it appears more complicated than expected. I tried:

  • calling our postprocessor with fullRunTask. Works fine but we would like to pass "products.value" to look for the .class files and it does not work

  • another and even better solution would be to extend compile (compile in Compile ~= { result => ...). But I did not found how the code after "result =>" can call our postprocessor

  • we are looking at other solutions: multiple projects, one for the postprocessor, one for the rest of the code and this would clean but because the source code is entangled, this is not as easy as it seems (and we still would have the first problem)

Any help?

  • Could you check if my answer to a [similar question](http://stackoverflow.com/questions/23873031/sbt-plugin-how-to-list-files-output-by-incremental-recompilation/23876075#23876075) would suit you? – lpiepiora Jun 05 '14 at 18:50

2 Answers2

0

I would just write a simple plugin that runs after the other stages. It can inspect the target folder for all the .class files.

You could then do something like sbt clean compile myplugin in your build server.

This is the approach taken by the proguard plugin[1]. You could take a look at that as a starting point.

[1] https://github.com/sbt/sbt-proguard

sksamuel
  • 16,154
  • 8
  • 60
  • 108
  • Thanks. This is something I have considered (as I will need a JavaCC plugin but this is another story). But right now, looks like overkill just to add a simple call to a class after running "compile". – user2753182 Jun 06 '14 at 13:54
  • You could write a plugin in 10 lines, and running a tool seems to indicate more than a quick task, but you can do it as a task in your build.sbt and then just invoke the task after compile. – sksamuel Jun 06 '14 at 14:08
  • As I said, running our tool is simply calling a main so fullRunTask would does the trick easily but passing dynamic arguments to it, especially "values" is where I'm having a hard time. – user2753182 Jun 06 '14 at 19:37
0

Finally, I found a solution after reading "SBT in Action" and other documents. It is very simple but understanding SBT is not (at least for me).

   name := "Foo"

   version := "1.0"

   scalaVersion := "2.11.0"

   fork := true

   lazy val foo = TaskKey[Unit]("foo")

   val dynamic = Def.taskDyn {
    val classDir = (classDirectory in Compile).value
    val command = " Foo "+classDir
    (runMain in Compile).toTask(command)
   }

   foo := {
    dynamic.value
   }

   foo <<= foo triggeredBy(compile in Compile)

The sample project contains a Foo.scala with the main function