2016-10-26 90 views
2

我有以下依赖我build.gradle如何从Gradle Maven Publishing插件构建的POM中排除依赖项?

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发布插件发布我的图书馆,它包括了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不具有编译时间依赖性。但是,构建失败。

+0

清楚你从'antlr'配置将依赖于你的'compile'配置别的地方在你的build.gradle。需要看到更多的build.gradle。另外为什么你有一个'antlr'配置? – RaGe

+0

当然,这里是build.grade:https://github.com/graphql-java/graphql-java/blob/v2.1.0/build.gradle。我有一个'antlr'配置,因为我使用了[Gradle ANTLR插件](https://docs.gradle.org/current/userguide/antlr_plugin.html) –

+0

@RaGe:'./gradlew generatePomFileForGraphqlJavaPublication'生成了pom 'build/publications/graphqlJava/pom-default.xml' –

回答

4

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

给予gradle-fury一枪。它绝对处理排除,我很确定只有已知配置包含在生成的poms中。它也有一些代码,以确保有没有重复的条目与冲突的范围(这是一个皇家疼痛找出解决方案)

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

声明,我就可以

+0

谢谢!将检查出来。 –

相关问题