gradlegradle-kotlin-dsl

How to use pom type dependency in Gradle


I need to produce transitive dependency from my Java library which is of type pom. Here is an example on how I'm doing it:

plugins {
  `java-library`
  `maven-publish`
}
repositories {
  // some maven repo
}
dependencies {
  // This is POM type dependency:
  api("org.apache.sshd:apache-sshd:1.6.0") {
    exclude(group = "org.slf4j")
  }
}
publications {
  create<MavenPublication>("maven") {
    from(components["java"])
  }
}

The problem with this configuration is that in the published pom.xml of my library the dependency is of type jar (by default) and declared like that:

<dependency>
  <groupId>org.apache.sshd</groupId>
  <artifactId>apache-sshd</artifactId>
  <version>1.6.0</version>
  <!-- Should declare pom type -->
  <scope>compile</scope>
  <exclusions>
    <exclusion>
      <artifactId>*</artifactId>
      <groupId>org.slf4j<groupId>
    </exclusion>
  </exclusions>
</dependency>

So when I try to use my published library from another project it fails as there is no such artifact as apache-sshd because it's type should be pom. So how to correctly publish desired dependency using Gradle?

Running on Gradle 5.3.1 with Kotlin DSL.


Solution

  • Try to use following construction for declaring dependency in Gradle

    api("org.apache.sshd:apache-sshd:1.6.0@pom") {
       exclude(group = "org.slf4j")
       transitive = true  // edited
    }
    

    Looks like Gradle consumes all dependencies as jar type by default. And Maven plugin generates dependency section in pom file by using this extracted type. For pom dependency it is necessary to put correct value into type field of generated file. But if you put pom extension for your dependency, Gradle won't resolve transitive dependencies that are declared in this artifact. Set the value of transitive flag resolves this issue.