如何在Gradle中使用Pom类型依赖 [英] How to use pom type dependency in Gradle

查看:654
本文介绍了如何在Gradle中使用Pom类型依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的pom类型的Java库中产生传递依赖.这是一个有关如何执行此操作的示例:

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"])
  }
}

此配置的问题在于,在我的库的已发布的 pom.xml 中,依赖项的类型为 jar (默认情况下),并且声明如下:

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>

因此,当我尝试使用另一个项目中的已发布库时,它会失败,因为没有诸如 apache-sshd 之类的工件,因为它的类型应为 pom .那么如何使用Gradle正确发布所需的依赖关系呢?

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?

在带有Kotlin DSL的Gradle 5.3.1上运行.

Running on Gradle 5.3.1 with Kotlin DSL.

推荐答案

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

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
}

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

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.

这篇关于如何在Gradle中使用Pom类型依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆