My favorites | Sign in
Project Home Downloads Wiki Issues
Search
for

sbt has moved

sbt has now completely moved to GitHub.

See https://github.com/harrah/xsbt/wiki.

Getting Started

Forking  
Updated Jul 27, 2011 by dmhar...@gmail.com

sbt has moved

sbt has now completely moved to GitHub.

See https://github.com/harrah/xsbt/wiki/Forking.

Forking

Introduction

By default, the run action runs in the same JVM as sbt. Forking is required under certain circumstances, however. Or, you might want to fork Java processes when implementing new tasks.

Forking run

Basics

The following examples demonstrate forking the run action and changing the working directory or arguments.

  override def fork = forkRun
  
  override def fork =
    forkRun(new File("different-working-directory"))

  override def fork =
    forkRun("-Xmx8G" :: Nil)

  override def fork =
    forkRun(
      Some(new File("different-working-directory")),
      "-Xmx8G" :: Nil
    )

Advanced

The method to override in your project definition is fork, which is of type Option[ForkScala]. By default, it is None, which indicates forking is disabled. To enable forking, override fork to return an instance of ForkScalaRun (wrapped in Some). ForkScalaRun defines these methods:

def runJVMOptions: Seq[String] = Nil
def workingDirectory: Option[File] = None
def javaHome: Option[File] = None
def scalaJars: Iterable[File] = None

By default, the forked process uses the same Java and Scala being used for the build and the working directory and JVM options of the current process. Override the above methods to use a different version. For example, to override the working directory used when running, do:

def forkConfiguration = new ForkScalaRun { override def workingDirectory = Some(info.projectPath.asFile) } 
override def fork = Some(forkConfiguration)

Direct Usage

To fork a new Java process, use the Fork API. The methods of interest are Fork.java, Fork.javac, Fork.scala, and Fork.scalac. See the ForkJava and ForkScala classes for the arguments and types.

The last argument in all cases is either the Logger to which output should go or the OutputStrategy to use (such as standard output or another OutputStream).

Comment by peter.li...@gmail.com, May 10, 2010

Is there a way to make a scala console session started from within sbt run in a forked jvm in the same way as this is possible for a program run?

Comment by project member dmhar...@gmail.com, May 18, 2010

no, sorry

Comment by kbar...@gmail.com, May 28, 2010

If I override fork by defining a new ForkScalaRun without an implementation for scalaJars I get an error java.lang.RuntimeException: Scala jars not specified. The solution is to use the method forkRun(workingDirectory0: Option[File], jvmOptions: Seq[String]) which provides a good default for the scalaJars method.

Comment by timot...@gmail.com, Jul 1, 2010

Kbarros, please give an example.

Comment by kbar...@gmail.com, Jul 15, 2010

My point was that forkRun provides a good default for the scalaJars method, whereas new ForkScalaRun{...} does not.

In other words, use the Basics section unless you know what you're doing.

Comment by dave.cor...@gmail.com, Oct 27, 2010

forkRun is great but unfortunately it seems to put all output into the 'info' stream for logging. Without forkRun enabled output goes straight to the console. Is it possible to replicate this behaviour with forkRun enabled?

Powered by Google Project Hosting