Gradle 从生成的 pom 中删除特定的依赖配置

Gradle remove specific dependency configuration from generated pom

在使用gradles插件maven-publish生成pom文件时,我需要忽略配置"configurations.nonDistributable"中定义的一些依赖项,我还没有找到一个可靠的方法来做这个,除了用于手动解析 XML 以删除它们。我是否遗漏了什么,gradle 是否允许更简单的方法?

build.gradle 示例:

configurations{
    nonDistributable
}

dependencies {
    nonDistributable ('org.seleniumhq.selenium:selenium-java:2.52.0'){
        exclude group:'com.google.guava' // included in elasticsearch
    }

    nonDistributable ('com.assertthat:selenium-shutterbug:0.3') {
        transitive = false
    }

    nonDistributable 'mysql:mysql-connector-java:5.1.40'
    nonDistributable fileTree(dir: 'non-distributable-libs', include: '*.jar')
}

// generate subprojects pom
subprojects {
    apply plugin: 'maven-publish'
    model {
        tasks.generatePomFileForMavenJavaPublication {
            destination = file("$buildDir/../../$distDir/build/pom/$project.name-pom.xml")
        }
    }
    afterEvaluate { project ->
        publishing {
            publications {
                mavenJava(MavenPublication) {
                    from components.java
                }
            }
        }
    }
}

// generate root project pom
model {
    tasks.generatePomFileForMavenJavaPublication {
        destination = file("$buildDir/../$distDir/build/pom/$project.name-pom.xml")
    }
}
afterEvaluate { project ->
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
            }
        }
    }
}

您可以创建自己的发布 pom。它看起来像这样:

customMaven(MavenPublication) {
    artifactId 'myArtifactId'
    pom.withXml {
        def dependencies = asNode().appendNode('dependencies')
        configurations.specialConfiguration.getResolvedConfiguration().getFirstLevelModuleDependencies().each {
            def dependency = dependencies.appendNode('dependency')
            dependency.appendNode('groupId', it.moduleGroup)
            dependency.appendNode('artifactId', it.moduleName)
            dependency.appendNode('version', it.moduleVersion)
        }
    }
}

然后您可以创建一个特殊配置,仅从您想要包含的那些配置中扩展。

我正在使用它来创建一个特殊的 pom,其中包含用于与主项目分离的集成测试的所有 testRuntime 依赖项。