通过Bazel编译Java项目时,出现以下异常。

BUILD:10:1: //:greeter: no such attribute 'src' in 'java_library' rule

构建文件:
load("@rules_java//java:defs.bzl", "java_binary")

java_binary(
        name="ProjectRunner",
        srcs = ["src/main/java/com/example/ProjectRunner.java"],
        main_class = "com.example.ProjectRunner",
        deps = [":greeter"],
)

java_library(
        name="greeter",
        src=["src/main/java/com/example/Greeting.java"],
)

最佳答案

该问题是由于BULID文件中的java_library规则中的属性名称是:srcs不是src所引起的。

load("@rules_java//java:defs.bzl", "java_binary")

java_binary(
        name="ProjectRunner",
        srcs = ["src/main/java/com/example/ProjectRunner.java"],
        main_class = "com.example.ProjectRunner",
        deps = [":greeter"],
)

java_library(
        name="greeter",
        **srcs**=["src/main/java/com/example/Greeting.java"],
)

已编译并正在运行。
INFO: Analyzed target //:ProjectRunner (1 packages loaded, 4 targets configured).
INFO: Found 1 target...
Target //:ProjectRunner up-to-date:
  bazel-bin/ProjectRunner.jar
  bazel-bin/ProjectRunner
INFO: Elapsed time: 0.079s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action

10-06 12:55