4

I've been trying to reference an artefact without luck.

With maven I have no problem doing this:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.13</artifactId>
    <version>3.0.0</version>
    <classifier>test</classifier>
</dependency>

Maven selects the correct artefact.

However, with gradle, it always seems to include the artefact without the classifier, no matter what I try:

implementation 'org.apache.kafka:kafka_2.13:3.0.0:test'

I have read the gradle documentation and it suggests this syntax, maybe it has something to do with this specific artefact?

Update

My goal is to use spring-kafka-test. Our internal artefact repository is not set up to use pom resolution, which is why I need to add transitives manually.

I've ruled out the fact that it might be our internal repository by only using maven central; and I get the same results.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
epoch
  • 16,396
  • 4
  • 43
  • 71
  • 1
    Try `org.apache.kafka:kafka_2.13:3.0.0:test@jar` since the Gradle docs seem to say that the extension is needed. Otherwise, if the goal is to test Kafka broker interactions, Testcontainers or EmbeddedKafka / spring-kafka-test are frequently used – OneCricketeer Jan 04 '22 at 13:44
  • @OneCricketeer, also does not work; My goal is indeed to use spring-kafka-test; but since I don't have pom resolution, I need to add this transitive manually; Doing this in maven works, my question is purely from a gradle viewpoint, as to how I can configure it to resolve this specific artefact. The only thing i can think of now, is to rename and re-host this specific file. – epoch Jan 04 '22 at 13:57
  • Is there a specific error message from gradle cli that gets returned? Or its just ignoring the classifier? What gradle version? – OneCricketeer Jan 04 '22 at 14:04

2 Answers2

1

I managed to include only the mentioned jar, with:

dependencies {
  implementation ('org.apache.kafka:kafka_2.13:3.0.0:test') {
    exclude group: 'org.apache.kafka' // or finer grained, if we like
  }
  ...
}

See also: How to specify a classifier in a gradle dependency's dependency?

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • 1
    I figured it out based on this answer; Gradle was in fact including the artifact, but its output is misleading. Nowhere does it say it is actually including the `test` jar, it just reflects (even with dependencyInsight) that `org.apache.kafka:kafka_2.13:3.0.0` exists. However I found the jar in Intellij's External libraries section. But thanks for the answer it helped me delve a bit deeper – epoch Jan 04 '22 at 14:46
  • The problem (i think) is, that there is only one pom for all the "classifiers" in that (& "normal") artifact, which pulls you all (unwanted) transitive dependencies. – xerx593 Jan 04 '22 at 14:48
0

But

Your assumptions about maven were also wrong:

Maven Pulls All Dependencies Screenshot

Maven pulls all! (In module-test-parents no dependencies defined.)

To achieve the same (and even more) in maven we'd also have to:

  <dependencies>
    <dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka_2.13</artifactId>
      <version>3.0.0</version>
      <classifier>test</classifier>
      <exclusions>
        <exclusion> <!--sledge hammer -->
          <groupId>*</groupId>
          <artifactId>*</artifactId>
        </exclusion>
        <!-- or selectively ... -->
      </exclusions>
    </dependency>
  </dependencies>

In gradle the according would be (tested):

implementation ('org.apache.kafka:kafka_2.13:3.0.0:test'){
  exclude group: '*'
}
xerx593
  • 12,237
  • 5
  • 33
  • 64