gradle - 如何从 Gradle Maven Publishing 插件构建的 POM 中排除依赖项?

标签 gradle antlr build.gradle antlr4

我的 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 没有编译时依赖。但是,然后构建失败。

最佳答案

根据@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>

一些更多的链接来解释这个问题:
  • https://discuss.gradle.org/t/antlr-plugin-adds-compile-dependency-on-the-whole-antlr/10768
  • https://issues.gradle.org/browse/GRADLE-3325
  • 关于gradle - 如何从 Gradle Maven Publishing 插件构建的 POM 中排除依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40268027/

    相关文章:

    java - 如何通过 Groovy 获取文件 build.gradle 的完整路径?

    python - ANTLR 语法后缀

    Android Studio 更新 0.5.3 - 找不到平台 'android-19'

    java - 为什么在 eclipse kepler 中导入 gradle 后缺少依赖项

    android - 如何打造 flavor ?

    android - Gradle + Annotations + Flavors = 不会运行注释处理器

    java - Antlr AST 树构建

    android - 由于它是在另一台PC上构建的,因此我无法同步我的android项目。怎么解决呢?

    android - 保持 Android Studio 的特定依赖项顺序

    antlr - Antlr4 中语义谓词的语法