Search code examples
javadependency-managementbazel

How to depend on a test Maven jar in Bazel?


Some Maven artifacts in addition to the main jar provide a separate test jar which contains classes to aid with writing tests that use the dependency. kafka-streams is one example. In Gradle it's possible to depend on such a jar using classifier: "test" (if the test jar has -test.jar suffix) and in Maven that would be <type>test-jar</type>. How to add a test jar to a Bazel workspace?


Solution

  • The Skylark maven_jar implementation supports this, with the artifact syntax of group:artifact:version:packaging:classifier.

    load("@bazel_tools//tools/build_defs/repo:maven_rules.bzl", "maven_jar")
    maven_jar(
        name = "org_apache_kafka_test",
        artifact = "org.apache.kafka:kafka-streams:1.0.0:jar:test",
        sha1 = "b275b72148aad7a59cc12f1005507d61fc0ae77b",
    )