Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude a dependency from a POM built by the Gradle Maven Publishing plugin?

I have the following dependencies in my 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'
}

When I use the Maven Publishing plugin to publish my library, it includes both the ANTLR runtime and compile time JARs as dependencies in the 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>

I only want the runtime library to be included in this POM.

The culprit is the antlr dependency: If I remove this line, the generated POM does not have the compile-time dependency. However, then the build fails.

like image 300
Dmitry Minkovsky Avatar asked Dec 11 '22 14:12

Dmitry Minkovsky


1 Answers

From @RaGe suggestion to use pom.withXml I was able to use this hackery to remove that extra dependency.

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

Before:

<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>

After:

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

Some more links to explain the issue:

  • https://discuss.gradle.org/t/antlr-plugin-adds-compile-dependency-on-the-whole-antlr/10768

  • https://issues.gradle.org/browse/GRADLE-3325

like image 145
iamcornholio Avatar answered Dec 14 '22 22:12

iamcornholio