使用bazel从maven导入并使用exernal jar文件

时间:2017-08-05 01:24:05

标签: scala maven bazel

我正在尝试了解如何使用bazel来构建java / scala项目,因此我创建了一个玩具项目,其中包含几个简单的类和一个使用scalatest框架的测试。我试图通过创建以下工作区文件从maven导入所有传递依赖项:

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",
)

根据https://docs.bazel.build/versions/master/be/workspace.html#maven_jar

然后我尝试使用以下BUILD文件构建我的测试:

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"]
)

引用外部广告,如链接中建议的<name>//jar

然而,当我构建时,我收到以下错误:

$ 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

有谁知道我做错了什么?我四处搜索,找不到一个明确的例子,它展示了如何编写一组基本的WORKSPACE和BUILD文件,以构建一个小项目,具体取决于maven的几个显式依赖项。

1 个答案:

答案 0 :(得分:2)

你看到的错误是由于bazel 0.5.1和bazel 0.5.2之间的突然变化打破了scala规则。截至撰写本文时,https://github.com/bazelbuild/rules_scala中的README表示&#34;您必须拥有0.5.2或更高版本的bazel&#34;。

您的BUILD和WORKSPACE文件是正确的。