Mapping from Maven standard directory to Bazel for test resources

爷,独闯天下 提交于 2019-12-04 03:55:00

问题


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.

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

I have the following in my src/test/resources/BUILD:

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

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",
    ],    
)

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


回答1:


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:

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).

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.

Edit: 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.



来源:https://stackoverflow.com/questions/43072078/mapping-from-maven-standard-directory-to-bazel-for-test-resources

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!