我的 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 作为 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 没有编译时依赖。但是,然后构建失败。
最佳答案
根据@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>
一些更多的链接来解释这个问题:
关于gradle - 如何从 Gradle Maven Publishing 插件构建的 POM 中排除依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40268027/