Izbassar Tolegen
Izbassar Tolegen

Reputation: 2152

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.

Upvotes: 6

Views: 15220

Answers (2)

Eugene Svalukhin
Eugene Svalukhin

Reputation: 126

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.

Upvotes: 8

Lavish
Lavish

Reputation: 720

I have used it in the following way:

compile(group: "dependency_group", name: "dependency_name", version: "dependency_name", extension: "pom")

and if you want to exclude the transitive dependencies add transitive flag and set it to false

compile(group: "dependency_group", name: "dependency_name", version: "dependency_name", extension: "pom"){
     transitive = false
}

Upvotes: 1

Related Questions