# Scala Rules for Bazel

Rules

## Overview This rule is used for building [Scala][scala] projects with Bazel. There are currently four rules, `scala_library`, `scala_macro_library`, `scala_binary` and `scala_test`. In order to use `scala_library`, `scala_macro_library`, and `scala_binary`, you must add the following to your WORKSPACE file: ```python new_http_archive( name = "scala", strip_prefix = "scala-2.11.7", sha256 = "ffe4196f13ee98a66cf54baffb0940d29432b2bd820bd0781a8316eec22926d0", url = "http://downloads.typesafe.com/scala/2.11.7/scala-2.11.7.tgz", build_file = "tools/build_defs/scala/scala.BUILD", ) ``` In addition, in order to use `scala_test`, you must add the following to your WORKSPACE file: ```python http_file( name = "scalatest", url = "https://oss.sonatype.org/content/groups/public/org/scalatest/scalatest_2.11/2.2.6/scalatest_2.11-2.2.6.jar", sha256 = "f198967436a5e7a69cfd182902adcfbcb9f2e41b349e1a5c8881a2407f615962", ) ``` [scala]: http://www.scala-lang.org/ ## scala\_library / scala\_macro_library ```python scala_library(name, srcs, deps, data, main_class, resources, scalacopts, jvm_flags) scala_macro_library(name, srcs, deps, data, main_class, resources, scalacopts, jvm_flags) ``` `scala_library` generates a `.jar` file from `.scala` source files. This rule also creates an interface jar to avoid recompiling downstream targets unless then interface changes. `scala_macro_library` generates a `.jar` file from `.scala` source files when they contain macros. For macros, there are no interface jars because the macro code is executed at compile time. For best performance, you want very granular targets until such time as the zinc incremental compiler can be supported. In order to make a java rule use this jar file, use the `java_import` rule.
Attributes
name

Name, required

A unique name for this target

srcs

List of labels, required

List of Scala .scala source files used to build the library

deps

List of labels, optional

List of other libraries to linked to this library target

data

List of labels, optional

List of files needed by this rule at runtime.

main_class

String, optional

Name of class with main() method to use as an entry point

The value of this attribute is a class name, not a source file. The class must be available at runtime: it may be compiled by this rule (from srcs) or provided by direct or transitive dependencies (through deps). If the class is unavailable, the binary will fail at runtime; there is no build-time check.

resources

List of labels; optional

A list of data files to be included in the JAR.

scalacopts

List of strings; optional

Extra compiler options for this library to be passed to scalac. Subject to Make variable substitution and Bourne shell tokenization.

jvm_flags

List of strings; optional

List of JVM flags to be passed to scalac after the scalacopts. Subject to Make variable substitution and Bourne shell tokenization.

## scala_binary ```python scala_binary(name, srcs, deps, data, main_class, resources, scalacopts, jvm_flags) ``` `scala_binary` generates a Scala executable. It may depend on `scala_library`, `scala_macro_library` and `java_library` rules. A `scala_binary` requires a `main_class` attribute.
Attributes
name

Name, required

A unique name for this target

srcs

List of labels, required

List of Scala .scala source files used to build the binary

deps

List of labels, optional

List of other libraries to linked to this binary target

data

List of labels, optional

List of files needed by this rule at runtime.

main_class

String, optional

Name of class with main() method to use as an entry point

The value of this attribute is a class name, not a source file. The class must be available at runtime: it may be compiled by this rule (from srcs) or provided by direct or transitive dependencies (through deps). If the class is unavailable, the binary will fail at runtime; there is no build-time check.

resources

List of labels; optional

A list of data files to be included in the JAR.

scalacopts

List of strings; optional

Extra compiler options for this binary to be passed to scalac. Subject to Make variable substitution and Bourne shell tokenization.

jvm_flags

List of strings; optional

List of JVM flags to be passed to scalac after the scalacopts. Subject to Make variable substitution and Bourne shell tokenization.

## scala_test ```python scala_test(name, srcs, suites, deps, data, main_class, resources, scalacopts, jvm_flags) ``` `scala_test` generates a Scala executable which runs unit test suites written using the `scalatest` library. It may depend on `scala_library`, `scala_macro_library` and `java_library` rules. A `scala_test` requires a `suites` attribute, specifying the fully qualified (canonical) names of the test suites to run. In a future version, we might investigate lifting this requirement.