How to use pom type dependency in Gradle

倖福魔咒の 提交于 2020-12-11 04:49:36

问题


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.


回答1:


Try to use following construction for declaring dependency in Gradle

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

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.




回答2:


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
}



来源:https://stackoverflow.com/questions/55575065/how-to-use-pom-type-dependency-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!