Skip to main content

How to build a simple Scala application with Bazel

Picture of Rikito Taniguchi, Software Developer

Rikito Taniguchi

Software Developer
Feb 15, 2023|14 min read
How_to_build_a_simple_Scala_application_with_Bazel_image-min.jpg
1|-- WORKSPACE
2`-- src
3 `-- main
4 `-- scala
5 |-- cmd
6 | |-- BUILD.bazel
7 | `-- Runner.scala
8 `-- lib
9 |-- BUILD.bazel
10 `-- Greeting.scala
1load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") #Import a rule from the Bazel's "standard library"
2 ...
3http_archive( #This rule can download and import an archived repo
4 name = "io_bazel_rules_scala", #The name that will be used to reference the repo
5 sha256 = "77a3b9308a8780fff3f10cdbbe36d55164b85a48123033f5e970fdae262e8eb2",
6 strip_prefix = "rules_scala-20220201", #Only files from this directory will be unpacked and imported
7 type = "zip",
8 url = "https://github.com/bazelbuild/rules_scala/releases/download/20220201/rules_scala-20220201.zip",
9)
1// src/main/scala/lib/Greeting.scala
2package lib
3object Greeting {
4 def sayHi = println("Hi!")
5}
1// src/main/scala/cmd/Runner.scala
2package cmd
3import lib.Greeting
4object Runner {
5 def main(args: Array[String]) = {
6 Greeting.sayHi
7 }
1# src/main/scala/lib/BUILD.bazel
2load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library")
3scala_library(
4 # unique identifier of this target
5 name = "greeting",
6 # list of Scala files to build
7 srcs = ["Greeting.scala"],
8)
1$ bazel build //src/main/scala/lib:greeting
2...
3INFO: Found 1 target...
4Target //src/main/scala/lib:greeting up-to-date:
5 bazel-bin/src/main/scala/lib/greeting.jar
6INFO: Elapsed time: 0.152s, Critical Path: 0.00s
7INFO: 1 process: 1 internal.
8INFO: Build completed successfully, 1 total action
1# src/main/scala/cmd/BUILD.bazel
2load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary")
3scala_binary(
4 name = "runner",
5 main_class = "cmd.Runner",
6 srcs = ["Runner.scala"],
7 deps = ["//src/main/scala/lib:greeting"],
8)
1$ bazel build //src/main/scala/cmd:runner
2
3ERROR: .../01_scala_tutorial/src/main/scala/cmd/BUILD.bazel:3:13:
4in scala_binary rule //src/main/scala/cmd:runner:
5target '//src/main/scala/lib:greeting' is not visible from
6target '//src/main/scala/cmd:runner'.
1 scala_library(
2 name = "greeting",
3 srcs = ["Greeting.scala"],
4+ visibility = ["//src/main/scala/cmd:__pkg__"],
5 )
1$ bazel build //src/main/scala/cmd:runner
2...
3INFO: Found 1 target...
4Target //src/main/scala/cmd:runner up-to-date:
5 bazel-bin/src/main/scala/cmd/runner.jar
6 bazel-bin/src/main/scala/cmd/runner
7INFO: Elapsed time: 0.146s, Critical Path: 0.01s
8INFO: 1 process: 1 internal.
9INFO: Build completed successfully, 1 total action
1$ ./bazel-bin/src/main/scala/cmd/runner
2Hi!
3
4$ bazel run //src/main/scala/cmd:runner
5Hi!
1// src/main/scala/example/App.scala
2package example
3import scala.meta._
4object App {
5 def main(args: Array[String]) = {
6 pprint.pprintln(parse(args.head))
7 }
8 private def parse(arg: String) = {
9 arg.parse[Source].get
10 }
11}
1http_archive(
2 name = "rules_jvm_external",
3 strip_prefix = "rules_jvm_external-4.5",
4 sha256 = "b17d7388feb9bfa7f2fa09031b32707df529f26c91ab9e5d909eb1676badd9a6",
5 url = "https://github.com/bazelbuild/rules_jvm_external/archive/refs/tags/4.5.zip",
6)
7...
1Then list all the dependencies in the maven_install statement, which is also in the WORKSPACE file.
1# src/main/scala/example/BUILD.bazel
2scala_binary(
3 name = "app",
4 main_class = "example.App",
5 srcs = ["App.scala"],
6 deps = [
7 "@maven//:com_lihaoyi_pprint_2_13",
8 "@maven//:org_scalameta_scalameta_2_13",
9 ],
10)
1$ bazel build //src/main/scala/example:app
2...
3INFO: Found 1 target...
4Target //src/main/scala/example:app up-to-date:
5 bazel-bin/src/main/scala/example/app.jar
6 bazel-bin/src/main/scala/example/app
7INFO: Elapsed time: 0.165s, Critical Path: 0.00s
8INFO: 1 process: 1 internal.
9INFO: Build completed successfully, 1 total action
10...
11
12$ bazel-bin/src/main/scala/example/app "object main { println(1) }"
13Source(
14 stats = List(
15 Defn.Object(
16 ...
17 )
18 )
19)

Subscribe to our newsletter and never miss an article