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

Dmi*_*sky 4 antlr gradle antlr4 build.gradle

我有以下依赖项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'
}
Run Code Online (Sandbox Code Playgroud)

当我使用Maven Publishing插件发布我的库时,它包括ANTLR运行时和编译时JAR作为生成的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>
Run Code Online (Sandbox Code Playgroud)

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

罪魁祸首是antlr依赖:如果删除此行,生成的POM不具有编译时依赖性.但是,构建失败.

小智 9

从@RaGe建议到使用pom.withXml我能够使用这个hackery来删除那个额外的依赖.

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

之前:

<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>
Run Code Online (Sandbox Code Playgroud)

后:

<dependencies>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-runtime</artifactId>
      <version>4.5.1</version>
      <scope>runtime</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

更多链接解释了这个问题: