scalasbtincremental-compiler

How to prevent SBT from recompiling modified .class files?


In our project, we have a enhance post-process to the .class files generated by compile. This enhance step actually modifies the generated .class files then overrides it.

enhance <<= enhance triggeredBy (compile in Compile)

The problem is that sbt has a mechanism called incremental recompilation. It monitors the generated .class file. Every time the enhancer overrides the generated .class file, sbt recognises these modifications and recompiles related sources in next compile command.

To us, a recompilation is a very time-consuming work. We want to stop sbt from recompiling modified .class file. That may mean making sbt only monitor source changes, not output changes.

I did some searching on this. But I found little things about this. Now I know a trait called Analysis is likely responsible for the mapping from sources to output .class files. So I ask help from you guys.

Ps: we may solve this problem by putting the output of enhance to another folder, but it is not preferred.


Solution

  • I said about that sbt monitors the output .class file. When a .class file is modified, it recompiles the .class file's source.

    After some research, we found that sbt notices file's modification by its last modified time. That is to say, we can fool sbt by rolling back the last modified time after the modification, so that sbt won't notice any changes.

    So, our solution is simple but effective:

    1. find all .class files
    2. note down their last modified time
    3. do the enhance
    4. put back the former last modified time

    This is a small trick. We still expect more robust solutions.