2016-10-26 88 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

謝謝!將檢查出來。 –

相關問題