|
BuildConfiguration
sbt has movedsbt has now completely moved to GitHub. See https://github.com/harrah/xsbt/wiki/Settings. IntroductionThis page describes build configuration for when the default actions or paths are not sufficient or appropriate for your project. Examplesbt is configured by creating a project definition in the project/build directory. A project definition is a class (written in Scala) that implements sbt.Project. This is usually done by extending sbt.DefaultProject. The following is an example of adding a Hello World task to your build. It assumes you have already initialized your project as described on the Setup page. project/build/HelloWorld.scala import sbt._
class HelloWorldProject(info: ProjectInfo) extends DefaultProject(info)
{
lazy val hi = task { println("Hello World"); None }
}If you have sbt running, you need to reload the project with the new project definition: > reload Otherwise, start up sbt. Execute hi: > hi ... Hello World ... The details of writing tasks are described on the Creating/Modifying Actions page. Configuration Detailssbt only recompiles a project definition on startup or when invoking the interactive command reload. Therefore, if you are running in interactive mode, invoke reload after making changes to your project definition for the changes to take effect. The project/build directory is a project itself. Therefore, you can add jars to the project/build/lib directory to use from your project definition. You can also use multiple sources for your project definition. The only requirement is there must be exactly one public, concrete subclass of Project with a constructor that accepts a ProjectInfo instance. sbt finds this subclass when it compiles the project definition, so the names of source files and classes are not important. Specifying OptionsThe general way to configure your build is to override one or more methods in your project definition. For example, the default options for the compile task are defined in the compileOptions method: def compileOptions: Seq[CompileOption] = Deprecation :: Nil Deprecation is a val in ScalaProject that is an instance of CompileOption and represents the command line scalac option -deprecation. To additionally specify the -unchecked command line option, override compileOptions in your project definition (class TestProject above): override def compileOptions = super.compileOptions ++ Seq(Unchecked) The currently available options for tasks are listed below. Compile OptionsMethod compileOptions specifies additional options for task compile to pass to the Scala compiler. It has type Seq[CompileOption]. There is a similar method testCompileOptions for test-compile. Provided options are:
override def compileOptions = super.compileOptions ++
compileOptions("-encoding", "utf8")Method javaCompileOptions specifies the options passed to the Java compiler by the compile task. There is a similar method testJavaCompileOptions for test-compile. Currently, all options are specified by:
Run OptionsMethod mainClass is of type Option[String] and specifies an optional main class to run when the run task is invoked. The default implementation specifies no main class (None). When mainClass is not specified, the run task will determine which class to run automatically. If exactly one main class is detected, it is run. If multiple main classes are detected, the user is prompted for which one to run. Package OptionsIf defined, the mainClass method specifies the value of the Main-Class attribute in the manifest. The manifestClassPathmethod, if defined, specifies the value of Class-Path attribute. Method packageOptions specifies general options for task package and is of type Seq[PackageOption]. Provided options are:
Document OptionsMethod documentOptions specifies the options for the API documentation tasks doc and docTests. Provided options are:
By default, a window and document title are specified in 2.7 and a document title is specified in 2.8 (window title does not exist at this time). Test OptionsMethod testOptions specifies the options for the test action.
Changing PathsThe only paths in sbt that are completely fixed are the locations of the files in the project directory. The other paths, such as the path to sources, jars, and outputs, are configurable. Overriding the default paths is described in this section. See Paths for details on constructing paths. Paths in sbt are incrementally built up out of path components so that you can easily rename any directory in the path or modify the path structure. You should always refer to a path by its sbt method instead of constructing a Path literal. For example, always refer to the output directory as outputPath instead of path("target") and refer to the directory that classes are compiled to as mainCompilePath instead of "target" / "classes". The paths and the names used to construct them are defined in BasicProjectPaths, which is mixed into the default project definition, as members that you can override. If you only want to rename a directory, override the member that specifies the name. For example, to rename the output directory from target to build, override def outputDirectoryName = "build" The outputs for compilation and API documentation are defined as: def outputPath = crossPath(outputDirectoryName) def mainCompilePath = outputPath / compileDirectoryName def docPath = outputPath / docDirectoryName def analysisPath = outputPath / analysisDirectoryName and so the directory name will be used by subpaths as well. Similarly, you could rearrange the source directory from src/
main/
scala/
resources/
test/
scala/
resources/to src/ resources/ test-src/ test-resources/ by specifying the following in your project definition: override def mainScalaSourcePath = "src" override def mainResourcesPath = "resources" override def testScalaSourcePath = "test-src" override def testResourcesPath = "test-resources" The paths directly used by the default actions are listed below. Other paths are only used to build up these paths. If redefined, care should be taken to keep these paths distinct from each other.
Additionally, web applications use these paths defined in sbt.WebProjectPaths:
| |
Hi! I have a few questions that I hope the docs can be clarified to answer:
I'm confused about what the name of the main project configuration files needs to be called. Obviously every project won't use "TestProject?.scala", but does sbt just do a scan of project/build/src/ until it finds an instance of DefaultProject?, or does it decide based on the "project name," or what?
Would it be possible to provide a snapshot of what exactly the sbt command does when it creates a new project?
Also, assuming that I've already got a bunch of scala code, what would be a good way to get it laid out in a directory structure sbt will grok?
Thanks!
Hi!
sbt compiles the sources. In the process, it finds a public, concrete subclass of Project (multiple instances will produce an error) and creates an instance.
When a project is created, sbt puts the requested information in project/build.properties file in Java properties format (see the Properties page for more information). It also creates the following directories: src/main/scala, src/main/resources, src/test/scala, src/test/resources, and lib.
The easiest way is to put your main sources in src/main/scala. You could alternatively change the source path in your project definition as indicated Changing Paths section.
I'll update the documentation to indicate this.
Thanks, Mark
The information on project creation was actually already on the Setup page in the Create Project section. I added a couple of sentences to the end of the Examples section to clarify the situation with project definition sources.
Thanks, Mark
can you add an example of defining custom packageOptions. I looked at the docs and tried to define it properly but still unsuccessful
Actually what I am trying to do is to change the name of the war package. I want to get rid of scala version from the name.
Mark, a minor mistake at 'Package Options' it's java.util.jar.Attributes instead of java.util.Attributes
Thanks, updated.
The mavenLocal example is not working on Windows because the URL will be incorrect. Here is the fix:
val mavenLocal = "Local Maven Repository" at new File(Path.userHome + "/.m2/repository").toURI.toStringHi!
I'd like to create a standalone jar file of my compiled scala project. (i.e. I want to be able to run the file on a system that has only java installed, but not scala).
I'm new to both Java and Scala, but I think I need to somehow get the scala-library.jar into the jar created by "package". Any ideas on how I'd do that using sbt?
p.s. if it helps any, I'm trying to use scala to write code for Hadoop, and I don't want to have to install and maintain scala installations across all the machines in my cluster.
Thanks!
One way to create an executable Jar in Scala - http://janxspirit.blogspot.com/2011/01/create-executable-scala-jar-with-sbt.html
Thank you that worked!