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

时间:2016-10-26 16:53:30

标签: 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作为{{3}}中的依赖项:

<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就没有编译时依赖性。但是,构建失败了。

2 个答案:

答案 0 :(得分:8)

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

更多解释此问题的链接:

答案 1 :(得分:1)

给予gradle-fury一击。它肯定处理排除,我很确定生成的poms中只包含已知的配置。它还有一些代码,以确保没有重复的条目与范围冲突(这是一个很难找到解决方案)

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

免责声明,我正在努力