Search code examples
bazel

Aliasing jar target of maven_jar rule


I have the following maven_jar in my workspace:

maven_jar(
    name = "com_google_code_findbugs_jsr305",
    artifact = "com.google.code.findbugs:jsr305:3.0.1",
    sha1 = "f7be08ec23c21485b9b5a1cf1654c2ec8c58168d",
)

In my project I reference it through @com_google_code_findbugs_jsr305//jar. However, I now want to depend on a third party library that references @com_google_code_findbugs_jsr305 without the jar target.

I tried looking into both bind and alias, however alias cannot be applied inside the WORKSPACE and bind doesn't seem to allow you to define targets as external repositories.

I could rename the version I use so it doesn't conflict, but that feels like the wrong solution.


Solution

  • IIUC, your code needs to depend on both @com_google_code_findbugs_jsr305//jar and @com_google_code_findbugs_jsr305//:com_google_code_findbugs_jsr305. Unfortunately, there isn't any pre-built rule that generates BUILD files for both of those targets, so you basically have to define the BUILD files yourself. Fortunately, @jart has written most of it for you in the closure rule you linked to. You just need to add //jar:jar by appending a couple of lines, after line 69 add something like:

    repository_ctx.file(
        'jar/BUILD', 
        "\n".join([
           "package(default_visibility = '//visibility:public')"] + _make_java_import('jar', '//:com_google_code_findbugs_jsr305.jar')
    

    This creates a //jar:jar (or equivalently, //jar) target in the repository.