How to use pom type dependency in Gradle

时间:2019-04-08 13:52:33

标签: gradle gradle-kotlin-dsl

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.

2 个答案:

答案 0 :(得分:1)

尝试使用以下构造在Gradle中声明依赖项

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

默认情况下,像Gradle一样使用所有依赖项作为jar类型。 Maven插件使用此提取的类型在pom文件中生成依赖项部分。对于pom依赖性,必须将正确的值放入生成的文件的type字段中。但是,如果您为依赖项添加pom扩展名,则Gradle将无法解析此工件中声明的可传递依赖项。设置传递标记的值可以解决此问题。

答案 1 :(得分:0)

我以以下方式使用它:

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

,如果要排除传递依赖项 添加transitive标志并将其设置为false

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