简体   繁体   中英

how to auto-reload changed scala classes into SBT REPL

I am new to Scala and to using emacs + ensime + sbt setup for my Scala development.

This setup is quite nice and light, but there is one thing which drives me nuts - inability to auto-compile / reload changes into Scala console started from sbt.

I use REPL a lot and would like to be able to start REPL from sbt with console command and test my changes to scala classes from REPL without having to close it and reload every time I make a change.

I come from Erlang environment and this way of development is easy with Erlang but seems to be difficult with SBT. I have the JRebel plug-in installed but it doesn't seem to be working for the situation I described.

Has anybody been able to make something similar work and would be willing to share the configuration steps?

Much appreciated in advance.

There are two things possible in sbt :

  1. Causing automatic recompilation of the project sources triggered by a file change by prefixing a command with ~ (tilde). The console , or console-quick , or console-project commands can be prefixed, too, but you have to exit REPL to make the recompilation happen (just hit Ctrl+D and wait.)

  2. Causing automatic execution of REPL commands just after firing the console. They can be defined as properties (eg in build.sbt ):

     initialCommands in console := """ import some.library._ def someFun = println("Hello") """ 

    It's not necessary to define the property separately in consoleQuick because it defaults to the one defined in console , but if you would like to use the console-project command you have to define it separately.

On a final note: remember to leave empty line between every property in an *.sbt file. They're necessary to parse the properties correctly. In the example above there are no blank lines in between so it means that everything goes into the initialCommands property (and that's what we want.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM