在Alpine上使用Bazel java_proto_library

问题描述

使用bazel alpine package时,尝试使用java_proto_library()时将失败(虽然可在archlinux,centos,fedora,debian,opensuse,ubuntu上使用)

工作空间:

load("@bazel_tools//tools/build_defs/repo:git.bzl","git_repository")

# Protobuf
git_repository(
    name = "com_google_protobuf",commit = "fde7cf7",# release v3.13.0
    remote = "https://github.com/protocolbuffers/protobuf.git",)

load("@com_google_protobuf//:protobuf_deps.bzl","protobuf_deps")
# Load common dependencies.
protobuf_deps()

测试/构建:

# not needed
#load("@rules_cc//cc:defs.bzl","cc_proto_library")
#load("@rules_java//java:defs.bzl","java_proto_library")

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

proto_library(
    name = "test_message_proto",srcs = ["test_message.proto"],deps = ["@com_google_protobuf//:duration_proto"],)

cc_proto_library(
    name = "test_message_cc_proto",deps = [":test_message_proto"],)

java_proto_library(
    name = "test_message_java_proto",)

Dockerfile:

# Create a virtual environment with all tools installed
# ref: https://hub.docker.com/_/alpine
FROM alpine:edge AS env
# Install system build dependencies
ENV PATH=/usr/local/bin:$PATH
RUN apk add --no-cache git build-base linux-headers zlib-dev
RUN apk add --no-cache -X http://dl-cdn.alpinelinux.org/alpine/edge/testing bazel

# Install OpenJDK11
#RUN apk add --no-cache openjdk11
# Remove infinite loop since jre point to the current directory
# otherwise bazel issue an error and stop...
#RUN rm /usr/lib/jvm/default-jvm/jre

ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$JAVA_HOME/bin:$PATH

FROM env AS devel
WORKDIR /home/lib
COPY . .

FROM devel as build
RUN bazel build --curses=no --copt='-Wno-sign-compare' //...:all

FROM build as test
RUN bazel test -c opt --curses=no --copt='-Wno-sign-compare' //...:all

错误:

$ docker build --target=build --tag test/bazel:alpine_build -f alpine/Dockerfile .
...
Step 12/12 : RUN bazel build --curses=no --copt='-Wno-sign-compare' //...:all
 ---> Running in c932e97d87d8
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
Loading: 
Loading: 0 packages loaded
Loading: 0 packages loaded
Loading: 0 packages loaded
DEBUG: Rule 'com_google_protobuf' indicated that a canonical reproducible form can be obtained by modifying arguments commit = "fde7cf7358ec7cd69e8db9be4f1fa6a5c431386a",shallow_since = "1597443653 -0700"
DEBUG: Call stack for the definition of repository 'com_google_protobuf' which is a git_repository (rule definition at /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/build_defs/repo/git.bzl:195:18):
 - <builtin>
 - /home/lib/WORKSPACE:4:1
Analyzing: 3 targets (2 packages loaded,0 targets configured)
Analyzing: 3 targets (5 packages loaded,5 targets configured)
Analyzing: 3 targets (5 packages loaded,5 targets configured)
Analyzing: 3 targets (15 packages loaded,133 targets configured)
Analyzing: 3 targets (20 packages loaded,866 targets configured)
Analyzing: 3 targets (20 packages loaded,866 targets configured)
Analyzing: 3 targets (21 packages loaded,895 targets configured)
Analyzing: 3 targets (21 packages loaded,895 targets configured)
INFO: Analyzed 3 targets (22 packages loaded,1014 targets configured).
INFO: Found 3 targets...
[0 / 192] [Prepa] BazelWorkspaceStatusAction stable-status.txt
ERROR: /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/jdk/BUILD:314:1: Action external/bazel_tools/tools/jdk/platformclasspath_classes/DumpPlatformClassPath.class failed (Exit 1) javac failed: error executing command external/remotejdk11_linux/bin/javac -source 8 -target 8 -Xlint:-options -cp external/remotejdk11_linux/lib/tools.jar -d ... (remaining 2 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
src/main/tools/process-wrapper-legacy.cc:58: "execvp(external/remotejdk11_linux/bin/javac,...)": No such file or directory
INFO: Elapsed time: 47.427s,Critical Path: 1.42s
INFO: 3 processes: 3 processwrapper-sandbox.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
The command '/bin/sh -c bazel build --curses=no --copt='-Wno-sign-compare' //...:all' returned a non-zero code: 1
make: *** [Makefile:116: alpine_build] Error 1

注意:Alpine软件包依赖于openJDK-8,但是即使我尝试安装openJDK-11(由于日志错误包含external/remotejdk11_linux/bin/javac,也无法安装。)

解决方法

虽然高山软件包依赖于openJDK8,但您必须安装openJDK11才能使其正常工作。

...
# Install OpenJDK11
# note: default-jvm will now point to java-11-openjdk
RUN apk add --no-cache openjdk11
# Remove infinite loop since jre point to the current directory
# otherwise bazel issue an error and stop...
RUN rm /usr/lib/jvm/default-jvm/jre

ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$JAVA_HOME/bin:$PATH

...
RUN bazel build --host_javabase=@local_jdk//:jdk //...:all

常见问题解答:

  • 如果您看到以下内容:
ERROR: infinite symlink expansion detected
[start of symlink chain]
/usr/lib/jvm/java-11-openjdk/jre
/usr/lib/jvm/java-11-openjdk
[end of symlink chain]
ERROR: /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/jdk/BUILD:66:1: @bazel_tools//tools/jdk:legacy_current_java_runtime depends on @local_jdk//:jdk in repository @local_jdk which failed to fetch. no such package '@local_jdk//': Infinite symlink expansion
ERROR: Analysis of target '//test:test_message_java_proto' failed; build aborted: Analysis failed

这意味着您忘了隐藏安装openjdk11时生成的符号链接...

RUN rm /usr/lib/jvm/default-jvm/jre
,

Bazel附带的JDK是针对glibc而非musl编译的,因此不适用于Alpine。必须使用--host_javabase=@local_jdk//:jdk明确指定系统JDk。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...