【问题标题】:How can I exclude a dependency from a POM built by the Gradle Maven Publishing plugin?如何从 Gradle Maven Publishing 插件构建的 POM 中排除依赖项?
【发布时间】:2018-10-25 02:57:01
【问题描述】:

我的build.gradle 中有以下依赖项:

dependencies {
    compile 'org.antlr:antlr4-runtime:4.5.1'
    compile 'org.slf4j:slf4j-api:1.7.12'
    antlr "org.antlr:antlr4:4.5.1"
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'org.codehaus.groovy:groovy-all:2.4.4'
    testCompile 'cglib:cglib-nodep:3.1'
    testCompile 'org.objenesis:objenesis:2.1'
}

当我使用 Maven Publishing 插件发布我的库时,它包括 ANTLR 运行时和编译时 JAR 作为 generated POM 中的依赖项:

<dependencies>
  <dependency>                    <!-- runtime artifact -->
    <groupId>org.antlr</groupId>
    <artifactId>antlr4-runtime</artifactId>
    <version>4.5.1</version>
    <scope>runtime</scope>
  </dependency>
  <dependency>                    <!-- compile time artifact, should not be included -->
    <groupId>org.antlr</groupId>
    <artifactId>antlr4</artifactId>
    <version>4.5.1</version>
    <scope>runtime</scope>
  </dependency>
</dependencies>

我只希望运行时库包含在这个 POM 中。

罪魁祸首是antlr 依赖:如果我删除这一行,生成的POM 没有编译时依赖。但是,构建失败。

【问题讨论】:

  • 很明显,您正在将 antlr 配置中的依赖项添加到您的 build.gradle 中其他位置的 compile 配置中。需要查看更多的 build.gradle。还有为什么你有一个antlr 配置?
  • 当然,这是 build.grade:github.com/graphql-java/graphql-java/blob/v2.1.0/build.gradle。我有一个antlr 配置,因为我使用的是Gradle ANTLR plugin
  • @RaGe: ./gradlew generatePomFileForGraphqlJavaPublicationbuild/publications/graphqlJava/pom-default.xml 中生成pom
  • antlr 插件是doing internally,我有点指责你的所作所为。肯定会起作用的方法是编写一个任务以在生成的 xml 中啜饮并删除有问题的部分。甚至可以在发布部分使用pom.withxml
  • antlr 插件应该将antlr 配置添加到compileOnly 而不是compile,这将解决所有这些问题。 copileOnly 依赖项不会进入 pom。另外:想知道您是否可以从构建脚本中覆盖插件正在执行的操作。

标签: gradle antlr build.gradle antlr4


【解决方案1】:

根据@RaGe 建议使用pom.withXml,我能够使用这个hackery 来消除额外的依赖。

pom.withXml {
  Node pomNode = asNode()
  pomNode.dependencies.'*'.findAll() {
    it.artifactId.text() == 'antlr4'
  }.each() {
    it.parent().remove(it)
  }
}

之前:

<dependencies>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-runtime</artifactId>
      <version>4.5.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4</artifactId>
      <version>4.5.1</version>
      <scope>runtime</scope>
    </dependency>
</dependencies>

之后:

<dependencies>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-runtime</artifactId>
      <version>4.5.1</version>
      <scope>runtime</scope>
    </dependency>
</dependencies>

更多解释问题的链接:

【讨论】:

    【解决方案2】:

    试试 gradle-fury。它肯定会处理排除项,我很确定生成的 pom 中只包含已知配置。它还有一些代码来确保没有具有冲突范围的重复条目(找出解决方案是一件非常痛苦的事情)

    https://github.com/gradle-fury/gradle-fury

    免责声明,我正在努力

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2015-10-26
      • 2020-02-15
      • 2018-04-12
      • 1970-01-01
      • 2019-03-31
      相关资源
      最近更新 更多