Bazel build不是从Maven项目开始的



我是 Bazel 的新手,正在学习它的构建工作,目前我正在尝试从 Maven 项目中使用 bazel 构建,请告诉我如何使其工作,谢谢。

这是我尝试定义的工作区文件:

maven_jar(
  name = "junit",
  artifact = "junit:junit:3.8.1",
)
maven_jar(
  name = "log4j1",
  artifact = "org.apache.logging.log4j:log4j-core:2.6.2",
)
maven_jar(
  name = "log4j2",
  artifact = "org.apache.logging.log4j:log4j-api:2.6.2",
)
.....

这是我尝试定义的构建文件:

package(default_visibility = ["//visibility:public"])
java_binary(
    name = "everything",
    srcs = glob(["src/main/java/**/*.java"]),
    resources = glob(["src/main/resources/**"]),
     main_class = "src/main/java/com/test/test/test/App",
    deps = [
    "@junit//jar",
    "@log4j1//jar",
    "@log4j2//jar",
    "@jackson//jar",
    "@jsonsimple//jar",
    "@commonsdbutils//jar",
    "@commons//jar",
    "@guava//jar",
    "@poi//jar"],
)

这是我得到的 Bazel 构建结果:

mbp:bazel_test me$ bazel build //:everything
INFO: Analysed target //:everything (1 packages loaded).
INFO: Found 1 target...
    ERROR: /Users/me/git/test/test/BUILD:4:1: Building everything-class.jar (104 source files) failed (Exit 1)
    src/main/java/com/test/test/test/Testapp.java:13: error: cannot find symbol
    import org.apache.poi.ss.usermodel.Cell;
                                      ^
      symbol:   class Cell
      location: program package org.apache.poi.ss.usermodel
      .....
      Target //:everything failed to build

我在这里看到两个问题:

  • main_class属性应使用点而不是斜杠:

 java_binary(
    name = "everything",
    srcs = glob(["src/main/java/**/*.java"]),
    main_class = "src.main.java.com.test.test.test.App",
    ...
  • poi 罐子里没有org.apache.poi.ss.usermodel.Cell类。

找到罐子的名称:

$ bazel query --output=build @poi//jar
...
java_import(
  name = "jar",
  visibility = ["//visibility:public"],
  jars = ["@poi//jar:poi-ooxml-3.9.jar"],
  srcjar = "@poi//jar:poi-ooxml-3.9-sources.jar",
)

包的位置是 $(bazel info output_base)/external/poi ,现在我们知道(从上面bazel query输出中的 srcs 属性(该文件被称为 poi-ooxml-3.9.jar

$ unzip -l $(bazel info output_base)/external/poi/jar/poi-ooxml-3.9.jar | grep "org.apache.poi.ss.usermodel"
        0  2012-11-26 17:14   org/apache/poi/ss/usermodel/
     2970  2012-11-26 17:14   org/apache/poi/ss/usermodel/WorkbookFactory.class

相关内容

  • 没有找到相关文章

最新更新