0

我有一个 Kotlin JVM 项目,它在本机运行时运行一些 JavaScript。目前,不同的语言源定义在不同的存储库中,JS 文件被 webpack 打包为 JAR,指定为 JVM 项目的依赖项。这很好用,但我想合并这两个存储库,因为它们本质上是耦合的。与其维护大量不同的构建工具,我认为这将是一个学习和使用多语言构建系统的好机会,比如 Bazel。

目前的结构:

项目文件结构

本质上,我正在尝试构建两个主要包。web 包构建正确,我可以通过命令行查看 webpacked 输出。包括完整图片的网络BUILD文件:

load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "compileCore",
    srcs = ["index.ts"],
    tsconfig = "tsconfig.json",
)

filegroup(
    name = "internalCore",
    srcs = ["compileCore"],
    output_group = "es5_sources",
)

load("@npm//webpack-cli:index.bzl", webpack = "webpack_cli")

webpack(
    name = "bundle",
    outs = ["bundle.prod.js"],
    args = [
        "--mode production",
        "$(execpath internalCore)",
        "--config",
        "$(execpath webpack.config.js)",
        "-o",
        "$@",
    ],
    data = [
        "internalCore",
        "webpack.config.js",
        "@npm//:node_modules",
    ],
    visibility = ["//visibility:public"],
)

另一个重要的包是嵌套//jvm/src/main/java/com/example/bazel/plugin包。这本质上是最终的交付物,它应该是一个 JAR,其中包含作为资源的 Web 包的输出。

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")

kt_jvm_library(
    name = "plugin",
    srcs = glob(["*.kt"]),
    deps = [
        # ... some deps
    ],
    resources = ["//web:bundle"],
    visibility = ["//visibility:public"],
)

这看起来很简单,但是在构建过程中会出现以下错误:

❯ bazel build //jvm/src/...
INFO: Analyzed target //jvm/src/main/java/com/example/bazel/plugin:plugin (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /Users/jzucker/dev/GitHub/plugin-example-bazel/jvm/src/main/java/com/example/bazel/plugin/BUILD:12:15: error executing shell command: '/bin/bash -c external/bazel_tools/tools/zip/zipper/zipper c bazel-out/darwin-fastbuild/bin/jvm/src/main/java/com/example/bazel/plugin/plugin-resources.jar @bazel-out/darwin-fastbuild/bin/jvm/src/ma...' failed (Exit 255) bash failed: error executing command /bin/bash -c ... (remaining 1 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
File web/bundle.prod.js does not seem to exist.Target //jvm/src/main/java/com/example/bazel/plugin:plugin failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.402s, Critical Path: 0.05s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

经过一些实验,这似乎是一个尝试将生成的输出捆绑为kt_jvm_library. 如果资源引用另一个包中的有形源文件,那么它就可以正常工作。这里的主要问题是这是否是 Bazel 的正确模式,或者我是否试图滥用这项技术。这似乎是一个相对简单的用例,但文档中有一行最让我担心:

所有规则的不变性是规则生成的文件总是与规则本身属于同一个包;无法将文件生成到另一个包中。但是,规则的输入来自另一个包的情况并不少见。

来自https://docs.bazel.build/versions/master/build-ref.html

任何见解将不胜感激。

4

1 回答 1

0

这实际上是 Bazel Kotlin 规则集中的一个错误:github.com/bazelbuild/rules_kotlin/issues/281

在修复之前,您可以将资源打包在 a 中java_library并将其包含为resource_jars.

java_library(
    name = "resources",
    resources = ["//web:bundle"],
    resource_strip_prefix = "web",
)

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")

kt_jvm_library(
    name = "plugin",
    srcs = glob(["*.kt"]),
    deps = [
        # ... some deps
    ],
    resource_jars = ["resources"],
    visibility = ["//visibility:public"],
)
于 2020-07-31T18:45:30.687 回答