Bazel Maven迁移传递依赖范围

问题描述 投票:2回答:2

我试图在其中一个具有deps和transitive依赖项目的项目中使用generate_workspace。一旦生成了generate_workspace.bzl并将其复制到WORKSPACE并遵循bazel文档中的指令。虽然我看到在generate_workspace.bzl中列出的deps及其传递deps我的项目在java_library阶段期间无法解析传递deps ..当我在IDEA中导入相同的项目时,我没有看到正确加载的jar。

我怀疑的是,我看到generate_workspace.bzl正在添加其作为runtime_deps的瞬态代表,这意味着它们仅在runtenter code hereime期间可用

我在这里创建了所有文件的要点https://gist.github.com/kameshsampath/8a4bdc8b22d85bbe3f243fa1b816e464

理想情况下,在我的maven项目中,我只需要https://gist.github.com/kameshsampath/8a4bdc8b22d85bbe3f243fa1b816e464#file-src_main_build-L8-L9,虽然generate_workspace.bzl已经正确解析了我认为如果我的src / main / BUILD看起来像

java_binary(
      name = "main",
      srcs = glob(["java/**/*.java"]),
      resources = glob(["resources/**"]),
      main_class = "com.redhat.developers.DemoApplication",
      # FIXME why I should import all the jars when they are transitive to spring boot starter
      deps = [
          "//third_party:org_springframework_boot_spring_boot_starter_actuator",
          "//third_party:org_springframework_boot_spring_boot_starter_web",
            ],
)

但遗憾的是,由于传递deps没有作为上述声明的一部分加载,因此会产生大量编译错误。最终我必须像在https://gist.github.com/kameshsampath/8a4bdc8b22d85bbe3f243fa1b816e464#file-src_main_build中那样定义

src_main_build是包src / main / BUILD下的BUILD文件third_party_BUILD是包下的BUILD third_party / BUILD

bazel
2个回答
1
投票

Bazel希望您声明所有直接依赖项。即如果您直接使用jar A中的类,则需要将其置于直接依赖项中。

您正在寻找的是一个部署jar。这有点hacky但你可以这样做(在third_party/BUILD):

java_binary(
    name = "org_springframework_boot_spring_boot_starter_actuator_bin",
    main_class = "not.important",
    runtime_deps = [":org_springframework_boot_spring_boot_starter_actuator"], )

java_import(
    name = "springframework_actuator",
    jars = [":org_springframework_boot_spring_boot_starter_actuator_bin_deploy.jar"], 
)

这将捆绑除jar中的neverlink之外的所有依赖项(_deploy.jar)并重新公开它。


0
投票

更新:rules_jvm_external是Bazel团队正式维护的规则集,用于传递获取和解析工件。

你可以找到Spring Boot here的例子。 WORKSPACE文件中的声明如下所示:

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

maven_install(
    artifacts = [
        "org.hamcrest:hamcrest-library:1.3",
        "org.springframework.boot:spring-boot-autoconfigure:2.1.3.RELEASE",
        "org.springframework.boot:spring-boot-test-autoconfigure:2.1.3.RELEASE",
        "org.springframework.boot:spring-boot-test:2.1.3.RELEASE",
        "org.springframework.boot:spring-boot:2.1.3.RELEASE",
        "org.springframework.boot:spring-boot-starter-web:2.1.3.RELEASE",
        "org.springframework:spring-beans:5.1.5.RELEASE",
        "org.springframework:spring-context:5.1.5.RELEASE",
        "org.springframework:spring-test:5.1.5.RELEASE",
        "org.springframework:spring-web:5.1.5.RELEASE",
    ],
    repositories = [
        "https://jcenter.bintray.com",
    ]
)
© www.soinside.com 2019 - 2024. All rights reserved.