【问题标题】:Gradle remove specific dependency configuration from generated pomGradle 从生成的 pom 中删除特定的依赖配置
【发布时间】:2017-06-01 19:41:17
【问题描述】:

在使用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
            }
        }
    }
}

【问题讨论】:

    标签: gradle build.gradle maven-publish


    【解决方案1】:

    您可以创建自己的发布 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 依赖项,用于与主项目分离的集成测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 2021-12-25
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      相关资源
      最近更新 更多