从Maven标准目录映射到Bazel以获取测试资源 [英] Mapping from Maven standard directory to Bazel for test resources

查看:134
本文介绍了从Maven标准目录映射到Bazel以获取测试资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个

We have test dependency files in src/test/resources per the Maven Standard Directory Layout. These test files end up in the JAR and on the classpath when test classes fetch them via, e.g. Resources#asCharSource.

Bazel依赖于测试文件并将其显示在类路径中的首选方式是什么?

What is the preferred way in Bazel to depend to test files and have them appear in the classpath?

我的src/test/resources/BUILD中包含以下内容:

filegroup(
    name = "test-deps",
    testonly = 1,
    srcs = glob(["*.txt"]),
    visibility = ["//visibility:public"],
)

还有我的src/main/java/com/path/to/my/test/BUILD中的以下内容:

And the following in my src/main/java/com/path/to/my/test/BUILD:

java_test(
    name = "MyTest",
    srcs = ["MyTest.java"],
    resources = ["//src/test/resources:test-deps"],
    test_class = "com.path.to.my.test.MyTest",
    deps = [
        "//src/main/com/path/to/my/code",
        "//:junit_junit",
    ],    
)

这行得通,但是我不确定这是否是Bazel中最好的方法.

This works, but I'm not sure if it is the best way in Bazel.

推荐答案

是的,这是推荐的方法.如文档中的资源所述,Bazel理解Maven标准目录布局,并将数据文件放在正确的类路径中:

Yes, that is the recommended approach. As mentioned in the docs for resources, Bazel understands the Maven Standard Directory Layout and will put the data files at the right classpath:

jar文件中资源的位置由以下方式确定: 项目结构. Bazel首先寻找Maven的标准 目录布局,("src"目录后跟"resources" 目录孙代.)

The location of the resources inside of the jar file is determined by the project structure. Bazel first looks for Maven's standard directory layout, (a "src" directory followed by a "resources" directory grandchild).

如果要单独捆绑资源,则可以创建仅资源的jar,然后使用

If you wanted to bundle the resource separately, you could create a resources-only jar and then depend on it with the resource_jars attribute.

正如Ittai在下面指出的那样,Bazel不会自检资源,因此您必须注意不要以任何冲突结束(例如pkg1/src/main/resources/foo和pkg2/src/main/resources/foo). resources不是resource_jars都不会为您检查此情况,因此,如果您对此有所顾虑,您可能希望将所需的任何资源放入文件组中,并使用常规的目标或测试来检查冲突.

as Ittai points out below, Bazel will not introspect resources, so you'd have to be careful not to end up with any collisions (e.g., pkg1/src/main/resources/foo and pkg2/src/main/resources/foo). Neither resources not resource_jars will check this for you, so if this is a concern, you might want to put any resources you need in a filegroup and have a genrule target or test that checks for collisions.

这篇关于从Maven标准目录映射到Bazel以获取测试资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆