运行 构建文件时出现 bazel 错误,协议错误,是吗?

bazel error while running build file, wrong protocol, is it?

运行 通过 bazel 构建文件时遇到以下错误

ERROR: /home/mywork1/...../X/service/tests/BUILD:47:11: in deps attribute of java_binary rule //javatests/.../X/service/tests:XHandler-Test:
'//java/..../X/service:thrift-server' does not have mandatory provider 'link_params' or 'java_common.provider'
and java_binary rule '//java/..../X/service:thrift-server' is misplaced here
(expected cc_binary, cc_library, genrule, genproto, java_import, java_library, java_proto_library, java_lite_proto_library, proto_library, sh_binary or sh_library).

BUILD 文件如下所示:

package(default_visibility = ["//visibility:public"])

java_library(
    name = "X-thrift-s",
    srcs = glob(["*.java"]),
    deps = [
        "//thrift/services/X:XService",
    "//java/com/.../services/X/service:thrift-server",
    ]
)

java_import(
    name = "X-thrift-p",
    jars = [
        "//external:thrift-jar",
        "//external:opencsv-jar",
        "//java/lib:cspj-jar",
    ]
)

load("//tools/bzl:genthrift.bzl","thrift_java_library")

thrift_java_library(
 name = "XService",
 srcs = [
     "XService.thrift"        
 ],
)


java_binary(
   name = "XHandler-Test",
   srcs = glob(["*.java"]),
   main_class = "com.......service.tests.XHandlerTest",
   deps = [
       "//java/....:X-thrift",
       "//java/com.....:thrift-server",
       "//java/com.....:XService",
   ],  
)

为了作为 Java 库,规则需要提供某些信息。显然,thrift_java_libary 没有。如果您是规则编写者,则 a blog post 介绍了如何实现这一点。

但是,假设您只是使用 Thrift 规则并且不想修改它们:Thrift 规则是否生成 .jar 文件作为输出?如果是这样,你可以这样做:

thrift_java_library(
 name = "XService",
 srcs = [
     "XService.thrift"        
 ],
)

java_import(
    name = "XServiceWrapper",
    jars = ["XService.jar"], # Replace this with the actual name of the thrift_library's output file
)

java_binary(
    ...
    deps = [":XServiceWrapper", ...],
)

您正在做的是手动将输出包装在与 java_binary 兼容的规则中。