gpt4 book ai didi

java - 将 Java/GRPC 应用程序从 Maven 迁移到 Bazel(由 docker 提供支持)

转载 作者:行者123 更新时间:2023-12-02 18:50:53 25 4
gpt4 key购买 nike

我正在尝试将我的构建工具从 maven 迁移到 bazel。我有一个带有 proto 文件的 java/grpc,如果我用 maven 构建它一切都很好,但是使用 bazel 我一直面临 grpc 上发生的问题。也许我的 bazel BUILD/WORKSPACE 文件配置不正确,我不太确定。任何帮助,将不胜感激!

这是我的工作区文件:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "3.0"
RULES_JVM_EXTERNAL_SHA = "62133c125bf4109dfd9d2af64830208356ce4ef8b165a6ef15bbff7460b35c3a"

http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

http_archive(
name = "bazel_skylib",
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()

http_archive(
name = "rules_proto",
sha256 = "73ebe9d15ba42401c785f9d0aeebccd73bd80bf6b8ac78f74996d31f2c0ad7a6",
strip_prefix = "rules_proto-2c0468366367d7ed97a1f702f9cd7155ab3f73c5",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/2c0468366367d7ed97a1f702f9cd7155ab3f73c5.tar.gz",
"https://github.com/bazelbuild/rules_proto/archive/2c0468366367d7ed97a1f702f9cd7155ab3f73c5.tar.gz",
],
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

http_archive(
name = "com_google_protobuf",
sha256 = "e11f901c62f6a35e295b7e9c49123a96ef7a47503afd40ed174860ad4c3f294f",
strip_prefix = "protobuf-3.10.0",
url = "https://github.com/protocolbuffers/protobuf/releases/download/v3.10.0/protobuf-all-3.10.0.tar.gz",
)

http_archive(
name = "googleapi",
sha256 = "51849d3ef693c88eb7692875eb444ef7131502e3fa200f25fc0a37b1e7e55ab5",
strip_prefix = "googleapis-a111a53c0c6722afcd793b64724ceef7862db5b9",
url = "https://github.com/googleapis/googleapis/archive/a111a53c0c6722afcd793b64724ceef7862db5b9.zip",
)

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()

load("@rules_jvm_external//:specs.bzl", "maven")
load("@rules_jvm_external//:defs.bzl", "artifact", "maven_install")

maven_install(
artifacts = [
"com.google.cloud:google-cloud-pubsub:1.92.0",
"com.google.cloud:google-cloud-storage:1.98.0",
"io.grpc:grpc-netty-shaded:1.23.0",
"io.grpc:grpc-protobuf:1.23.0",
"io.grpc:grpc-stub:1.23.0",
"mysql:mysql-connector-java:8.0.17",
"com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.0.15",
"com.zaxxer:HikariCP:3.3.1",
"com.google.inject:guice:4.2.2",
"jaxen:jaxen:1.2.0",
"org.dom4j:dom4j:2.1.1",
"org.slf4j:slf4j-simple:2.0.0-alpha1",
"org.slf4j:slf4j-api:2.0.0-alpha1",
"log4j:log4j:1.2.17",
"com.google.code.gson:gson:2.8.6",
"junit:junit:4.13-beta-3",
"com.google.protobuf:protobuf-java:3.0.0",
"com.google.protobuf:protobuf-java-util:3.0.0",
],
override_targets = {
"com.google.protobuf:protobuf-java": "@com_google_protobuf//:protobuf_java",
},
maven_install_json = "//:maven_install.json",
generate_compat_repositories = True,
repositories = [
"https://jcenter.bintray.com",
],
)

load("@maven//:defs.bzl", "pinned_maven_install")
pinned_maven_install()

load("@maven//:compat.bzl", "compat_repositories")
compat_repositories()

maven_install(
artifacts = [
maven.artifact(
group = "com.google.api.grpc",
artifact = "proto-google-common-protos",
version = "1.16.0",
exclusions = [
maven.exclusion(
group = "com.google.protobuf",
artifact = "protobuf-java"
)
]
)
],
maven_install_json = "//:maven_install.json",
generate_compat_repositories = True,
repositories = [
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)

load("@maven//:defs.bzl", "pinned_maven_install")
pinned_maven_install()

load("@maven//:compat.bzl", "compat_repositories")
compat_repositories()

这是我的构建文件:
package(default_visibility = ["//visibility:public"])

load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "account",
srcs = glob(["src/main/proto/account.proto"]),
deps = ["@googleapi//google/api:annotations_proto"],
)

java_proto_library(
name = "account_proto_java",
deps = [":account"],
)

java_library(
name = "AccountService",
srcs = glob(["src/main/java/**/*.java"]),
resources = glob(["src/main/resources/**"]),
deps = [
"@maven//:com_google_cloud_google_cloud_pubsub",
"@maven//:com_google_cloud_google_cloud_storage",
"@maven//:io_grpc_grpc_netty_shaded",
"@maven//:io_grpc_grpc_protobuf",
"@maven//:io_grpc_grpc_stub",
"@maven//:mysql_mysql_connector_java",
"@maven//:com_google_cloud_sql_mysql_socket_factory_connector_j_8",
"@maven//:com_zaxxer_HikariCP",
"@maven//:com_google_inject_guice",
"@maven//:jaxen_jaxen",
"@maven//:org_dom4j_dom4j",
"@maven//:org_slf4j_slf4j_simple",
"@maven//:org_slf4j_slf4j_api",
"@maven//:log4j_log4j",
"@maven//:com_google_code_gson_gson",
"@maven//:junit_junit",
"@maven//:com_google_guava_guava",
"@maven//:io_grpc_grpc_api",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_protobuf_protobuf_java_util",
":account_proto_java",
],
)

我终于运行了那个命令: docker run --rm -v /${PWD}:${PWD} -w /${PWD} l.gcr.io/google/bazel:latest build ///...
出现的一些问题:
  • 下载资源时,我似乎总是遇到错误。这是一个例子:WARNING: Download from https://maven.google.com/org/checkerframework/checker-compat-qual/2.5.5/checker-compat-qual-2.5.5.jar failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
  • 出于某种原因,当 404 出现时,它并没有止步于此,而是在构建 proto 部分时结束。我收到此错误:
  • ERROR: /c/Users/<username>/projects/Ecommerce-Account-Microservice/BUILD:16:1: Building libAccountService-class.jar (198 source files) failed (Exit 1)
    src/main/java/com/app/account/GRPCClient.java:7: error: cannot find symbol
    import com.app.account.services.AccountServiceGrpc;
    ^
    symbol: class AccountServiceGrpc
    location: package com.app.account.services
    src/main/java/com/app/account/GRPCClient.java:20: error: package AccountServiceGrpc does not exist
    private final AccountServiceGrpc.AccountServiceBlockingStub blockingStub;
    ^
    src/main/java/com/app/account/server/services/AccountService.java:24: error: cannot find symbol
    import com.app.account.services.AccountServiceGrpc;
    ^
    symbol: class AccountServiceGrpc
    location: package com.app.account.services
    src/main/java/com/app/account/server/services/AccountService.java:39: error: package AccountServiceGrpc does not exist
    public class AccountService extends AccountServiceGrpc.AccountServiceImplBase {
    ^
    src/main/java/com/app/account/GRPCClient.java:28: error: cannot find symbol
    blockingStub = AccountServiceGrpc.newBlockingStub(channel);
    ^
    symbol: variable AccountServiceGrpc
    location: class GRPCClient
    src/main/java/com/app/account/server/GrpcServer.java:28: error: no suitable method found for addService(AccountService)
    .addService(AccountService.getInstance())
    ^
    method ServerBuilder.addService(ServerServiceDefinition) is not applicable
    (argument mismatch; AccountService cannot be converted to ServerServiceDefinition)
    method ServerBuilder.addService(BindableService) is not applicable
    (argument mismatch; AccountService cannot be converted to BindableService)
    where CAP#1 is a fresh type-variable:
    CAP#1 extends ServerBuilder<CAP#1> from capture of ?
    src/main/java/com/app/account/server/services/AccountService.java:57: error: method does not override or implement a method from a supertype
    @Override
    ^
    src/main/java/com/app/account/server/services/AccountService.java:65: error: method does not override or implement a method from a supertype
    @Override
    ^
    src/main/java/com/app/account/server/services/AccountService.java:223: error: method does not override or implement a method from a supertype
    @Override
    ^
    src/main/java/com/app/account/server/services/AccountService.java:273: error: method does not override or implement a method from a supertype
    @Override
    ^
    INFO: Elapsed time: 373.098s, Critical Path: 44.04s
    INFO: 358 processes: 353 processwrapper-sandbox, 5 worker.
    FAILED: Build did NOT complete successfully
    FAILED: Build did NOT complete successfully

    我不太确定当时发生了什么或我做错了什么,这就是我展示所有代码的原因。

    非常感谢!

    标记

    最佳答案

    如果要使用多个 maven_install声明,您需要为每个声明提供唯一名称。见 documentation here .

    I always seem to get an error when it's downloading the resources. Here is an example: WARNING: Download from https://maven.google.com/org/checkerframework/checker-compat-qual/2.5.5/checker-compat-qual-2.5.5.jar failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found



    看起来您实际上并未将 maven.google.com 用于任何 Artifact 。从您的 maven_install.repositories 中删除它列出并重新运行 artifact pinning为他们。

    对于缺少的 GRPC 类,您似乎缺少了 java_grpc_library java_library之间和 java_proto_library . Example .

    关于java - 将 Java/GRPC 应用程序从 Maven 迁移到 Bazel(由 docker 提供支持),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60623461/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com