2

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?

raindev
  • 1,017
  • 1
  • 12
  • 25
  • A question about the same functionality in Gradle: https://stackoverflow.com/questions/20224260/how-do-i-pull-maven-test-jars-using-gradle#20225377 – raindev Dec 01 '17 at 21:05
  • Can you write a separate `maven_jar` rule for the test one? – László Dec 04 '17 at 15:14
  • That's exactly what I'm trying to figure out how to do :) – raindev Dec 04 '17 at 15:38
  • Note that the question is about the case when the same artifact provides both an ordinary and a test jar. Visit the link to Maven Central from the question for an example. – raindev Dec 04 '17 at 15:41

2 Answers2

4

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",
)
Adam
  • 1,767
  • 12
  • 26
  • Finally got to test it: works as advertised, thanks Adam. Is the Skylark rule something that's safe to use as a drop-in replacement or there're things to consider? E.g. why it's not the default? – raindev Dec 22 '17 at 14:35
1

I think this feature is missing from maven_jar.

I could write a rule for the main jar:

maven_jar(
    name = "org_apache_kafka",
    artifact = "org.apache.kafka:kafka-streams:1.0.0",
    sha1 = "a6c87c367176beb7650eb2df173fd9fe6e38656f",
)

But I could not write one for the test jar, this didn't work:

maven_jar(
    name = "org_apache_kafka_test",
    artifact = "org.apache.kafka:kafka-streams:1.0.0-test",
    sha1 = "b275b72148aad7a59cc12f1005507d61fc0ae77b",
)

I recommend filing a feature request at https://github.com/bazelbuild/bazel/issues/new .

László
  • 3,973
  • 1
  • 13
  • 26
  • I tried exactly what you suggest without luck too. Is there any point in filing an issue if the feature is supported by the Skylark rule as pointed out by Adam? As I understand Skylark will superceed the current generation of rules eventually or it's not true? – raindev Dec 22 '17 at 14:38
  • [raindev](https://stackoverflow.com/users/2057275/raindev), I'm glad it works! There's no point in filing a bug if the Skylark implementation works. Thanks! – László Jan 02 '18 at 09:16