Search code examples
scalamavenbazel

Import and use an exernal jar file from maven using bazel


I'm trying to understand how to use bazel to build java/scala projects, so I've created a toy project that has a couple of trivial classes and one test that uses the scalatest framework. I've attempted to import all transitive dependencies from maven by creating the following workspace file:

workspace(name = "scala_bazel_example")

git_repository(
    name = "io_bazel_rules_scala",
    remote = "git://github.com/bazelbuild/rules_scala",
    commit = "aaa6c7d4db4f231a541f20a60760420f4bdd11e8"
)

load("@io_bazel_rules_scala//scala:scala.bzl", "scala_repositories")

scala_repositories()

maven_jar(
  name = "junit",
  artifact = "junit:junit:4.11",
)

maven_jar(
  name = "org_scalatest",
  artifact = "org.scalatest:scalatest_2_11:3.0.1",
)

maven_jar(
  name = "org_hamcrest",
  artifact = "org.hamcrest:hamcrest-core:1.3",
)

as per https://docs.bazel.build/versions/master/be/workspace.html#maven_jar

Then I tried to BUILD my test with the following BUILD file:

scala_test(
  name = "example-test",
  srcs = glob(["**/*.scala"]),
  deps = [
    "@org_scalatest//jar",
    "@junit//jar",
    "@org_hamcrest//jar",
    "//src/scala/main/src/com/foo/util:example-lib"]
)

referencing the external jars as suggested in the link as <name>//jar.

However, when I build, I get the following error:

$ bazel build :example-test 
ERROR: /home/ubuntu/.cache/bazel/_bazel_ubuntu/c83ae3b365c8d335f0b135ef3a055202/external/io_bazel_rules_scala/third_party/plugin/src/main/BUILD:5:1: in scala_library_for_plugin_bootstrapping rule @io_bazel_rules_scala//third_party/plugin/src/main:dependency_analyzer: 
Traceback (most recent call last):
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/c83ae3b365c8d335f0b135ef3a055202/external/io_bazel_rules_scala/third_party/plugin/src/main/BUILD", line 5
                scala_library_for_plugin_bootstrapping(name = 'dependency_analyzer')
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/c83ae3b365c8d335f0b135ef3a055202/external/io_bazel_rules_scala/scala/scala.bzl", line 576, in _scala_library_impl
                _lib(ctx, True)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/c83ae3b365c8d335f0b135ef3a055202/external/io_bazel_rules_scala/scala/scala.bzl", line 503, in _lib
                _collect_jars_from_common_ctx(ctx)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/c83ae3b365c8d335f0b135ef3a055202/external/io_bazel_rules_scala/scala/scala.bzl", line 486, in _collect_jars_from_common_ctx
                _collect_jars(ctx.attr.deps + auto_deps + extr..., ...)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/c83ae3b365c8d335f0b135ef3a055202/external/io_bazel_rules_scala/scala/scala.bzl", line 463, in _collect_jars
                _collect_jars_when_dependency_analyzer_is_off(dep_targets)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/c83ae3b365c8d335f0b135ef3a055202/external/io_bazel_rules_scala/scala/scala.bzl", line 422, in _collect_jars_when_dependency_analyzer_is_off
                java_provider.compile_jars
'java_common.provider' object has no attribute 'compile_jars'
Available attributes: transitive_runtime_jars.
ERROR: Analysis of target '//src/scala/test/src/com/foo/util:example-test' failed; build aborted.
INFO: Elapsed time: 0.287s

Does anyone have an idea what I'm doing wrong? I searched around and couldn't find a clear example that showed how to write a basic set of WORKSPACE and BUILD files to build a small project depending on just a couple of explicit dependencies from maven.


Solution

  • The error you are seeing is due to a breaking change between bazel 0.5.1 and bazel 0.5.2 that broke the scala rules. As of time of writing, the README in https://github.com/bazelbuild/rules_scala says "you must have bazel 0.5.2 or later".

    Your BUILD and WORKSPACE files are correct.