Running a scala console

Running

You can start a console with a classpath set with the project dependencies (compile, test, runtime) and compiler classes (main and test):

  1. compile your code (running the console doesn't force to compile:
    mvn test-compile #or mvn test
  2. run the console :
    mvn scala:console
  3. when the scala prompt is shown, play :
    ...
    [INFO] [scala:console]
    Welcome to Scala version 2.6.0-final.
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> print("hello")
    hello
    scala> new bootstrap.liftweb.Boot().boot
    ...
          

pre/post action

If you want to always run some code before use the console or after use it, and don't want to type it at every startup. Then you could wrap the console into a scala main().

I'll show an example with the LiftConsole. A good pratice is to put this class in the test directory (it's not part of the main code ;) )

  1. add into the pom.xml the jar with the scala 's console
          <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-compiler</artifactId>
            <version>${scala.version}</version>
            <scope>test</scope>
          </dependency>
          
  2. create the scala file src/test/scala/LiftConsole.scala (based on liftrepl)
    import bootstrap.liftweb.Boot
    import scala.tools.nsc.MainGenericRunner
    
    object LiftConsole {
       def main(args : Array[String]) {
         // Instantiate your project's Boot file
         val b = new Boot();
         // Boot your project
         b.boot;
         // Now run the MainGenericRunner to get your repl
         MainGenericRunner.main(args)
         // After the repl exits, then exit the scala script
         exit(0)
       }
    }
          
  3. compile:
    mvn test
  4. run :
    mvn scala:console -DmainConsole=LiftConsole
  5. if you don't want to always need to set "-DmainConsole=LiftConsole", then edit your pom.xml and add to the configuration of the plugin :
          <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>4.8.1</version>
            <configuration>
              <mainConsole>LiftConsole</mainConsole>
            </configuration>
          </plugin>
          

running with JavaRebel

If you want to run the console with JavaRebel, you need :

  1. install javarebel somewhere on your disk, javarebel is available on a public maven repository (commercial tool)
  2. call the console with the option define where is the javarebel.jar:
    mvn scala:console -Djavarebel.jar.path=[path_of_javarebel.jar]
    or through the pom.xml
          <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>4.8.1</version>
            <configuration>
              <javaRebelPath>${user.home}/lib/java/javarebel/javarebel.jar</javaRebelPath>
            </configuration>
          </plugin>
          
  3. NOTE - Forking is currently disabled in the plugin due to cross-platform issues with jline and process indirection. If you wish to use JRebel with the console, please set your maven opts in your environment:
    MAVEN_OPTS="$MAVEN_OPTS -noverify -javaagent:/...path/to/jrebel.jar" mvn scala:console