简体   繁体   中英

How to specify java META-INF directory and services in bazel?

I am trying to provide my own implementation of the System.LoggerFinder and as far as I know I have to specify the class in some resource file /resources/META-INF/services/java.lang.System$LoggerFinder .

Now my implementation is located in its own package (Including build file and java_library() as rule), which is different from the package & BUILD file my java_binary() lives in. I added the implementation as deps to the BUILD file of the binary and made sure the package is visible using //visibility:public just to make sure that's not the problem. I tried putting the above mentioned file into both of them and specifying it as resource file in the respective BUILD file using resources = ["resources/META-INF/services/java.lang.System$LoggerFinder"] , but bazel always complains that either

  • the file '//:resources/META-INF/services/java.lang.System$LoggerFinder' is missing,
  • or, if I use resources = ["//resources/META-INF/services/java.lang.System$LoggerFinder"] instead, that the the resource directory is missing a BUILD file.

So basically my question is: Where do I have to put the resources and how do I have to specify them? If I have to add a BUILD file to the resources what rule should I use?

Thanks!

One way to do this is to create a jar file with the files in the META-INF folder and add that as a dependency of your java_library. The contents of the jar should be merged in. Something like this:

java_library(
  name = "lib",
  srcs = [...],
  deps = [":meta_inf_import"],
)

java_import(
  name = "meta_inf_import",
  jars = [":meta_inf.jar"],
)

genrule(
  name = "gen_meta_inf",
  srcs = ["java.lang.System$LoggerFinder"],
  outs = ["meta_inf.jar"],
  cmd = """
mkdir -p META-INF/services
cp $< META-INF/services/
jar -cf $@ .
""",
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM