Is there a way to invoke a java code (something like custom maven plugins) in bazel build?

Viewed 237

I made a plugin to run it during compilation of maven modules. But since my org is now moving to bazel -- Do we have a way that we can directly run plugins(mojos)in bazel?

If not, is there a way during bazel build of a java code that we can invoke another java code which has all the project info just like MavenProject parameter in mojos?

2 Answers

You can use a genrule with the java_binary for your plugin as a tool. Something like this:

java_binary(
    name = "my_plugin",
    ...
)

genrule(
    name = "run_plugin",
    srcs = [],
    cmd = "$(location my_plugin) > \"$@\"",
    outs = ["plugin_output"],
    tools = [":my_plugin"],
)

In this example, you should be able to run your plugin with bazel build :plugin_output.

It sounds like what you might want to try is a java_plugin, which is for running annotation processors with java_binary and java_library: https://docs.bazel.build/versions/master/be/java.html#java_plugin

and you specify the plugin with the plugins attribute of java_binary or java_library: https://docs.bazel.build/versions/master/be/java.html#java_binary.plugins

java_binary(
  name = ...,
  srcs = ...,
  deps = ...,
  plugins = [":my_plugin"],
)

java_plugin(
  name = "my_plugin",
  srcs = ...,
  deps = ...,
  processor_class = "...",
)
Related