How to do bazel java_export with desired change only

  1. How to to bazel java_export with desired change only
  2. Background
  3. Issue
  4. Solution

How to to bazel java_export with desired change only

Faced one interesting issue when using bazel, the background is we have multiple repository to host code — SOA. For sharing, then we need to export the library and publish to company wide artifactory.


We use bazel mainly because we have code on different languages, like RoR, Java, Kotlin, Scala, Clojure, etc. Bazel makes it easier to manage it cross different language in a monorepo.

For the export library purpose, we heavily rely on the rules_jvm_external , which has built-in java_export commend we could leverage on directly.

def java_export(
        deploy_env = [],
        excluded_workspaces = {name: None for name in DEFAULT_EXCLUDED_WORKSPACES},
        pom_template = None,
        visibility = None,
        tags = [],
        testonly = None,

Basically we could follow the pattern here to define our srcs, deps, maven_coordinates, and then bazel could help us export it and publish to selected artifactory.


On the other end, we have a service built on kotlin with gradle. When we implement the library, we found inside the jar, besides the code in the export places, it also contain directory from* , and unfortunately the protobuf package version in the lib mismatch with what we use in our own service, as the version in the lib is lower, it breaks the compilation of our code base, especially when we have couple extension functions in protobuf to enrich the conversion, whereas the old protobuf version does not support.

So basically we need to find a way to not include undesired directory inside the jar. In our case, remove those from*


Add those into runtime_deps, then the export jar would not contain the directory from the pkg. It solves our problem perfect.


文章标题:How to do bazel java_export with desired change only


本文作者:Leilei Chen

发布时间:2023-08-16, 20:26:10

最后更新:2023-08-16, 20:43:23


版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。