Search code examples
bazelbazel-rulesbazel-java

Bazel: How to pass relative path of a file as an "args" to java_binary?


I have a file hello.txt that lives in src/main/resources folder.

I am using a java_binary rule and need to pass this hello.txt file as an argument.

java_binary(
    name = "Hello",
    srcs = glob(["src/main/java/**"]),
    args = ["/Users/jdoe/repo1/libraries/myproj/src/main/resources/hello.txt"],
    deps = [...],
)

The above works when I provide the full path, however it fails if I try with a relative path like src/main/resources/hello.txt.

How do I provide a relative path to the args attribute?


Solution

  • Your binary depends on the resource file hello.txt, but Bazel is not aware of this. Make hello.txt a data dependency of your java_binary, i. e. add the attribute data = ["src/main/resources/hello.txt"]. Bazel runs your executable in a sandbox, i.e. somewhere where your hello.txt is not present. The data dependency makes sure that the file is copied to the place where it is needed.