Gradle任务依赖于动态创建的任务

时间:2015-11-30 11:23:11

标签: groovy gradle build.gradle gradle-plugin

我有一个多项目构建,我需要将一些依赖项发布到磁盘上的自定义本地Maven存储库,然后将此文件夹添加到分发zip。

我使用maven-publish插件定义每个子项目的发布任务。

subprojects {
    apply plugin: 'maven-publish'

    configurations {
        offlineDependencies
    }

    publishing {
        publications {
            configurations.each { config ->
                if (config.name == "offlineDependencies") {
                    def files = config.files
                    config.dependencies.each { dep ->
                        files.each { file ->
                            if (file.name == "${dep.name}-${dep.version}.jar") {
                                "${dep.name}"(MavenPublication) {
                                    artifact (file) {
                                        groupId = "${dep.group}"
                                        artifactId = "${dep.name}"
                                        version = "${dep.version}"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        repositories {
            maven {
                name 'dependencies'
                url '../build/repository'
            }
        }
    }
}

使用分发插件我创建一个zip文件

distributions {
    release {
        baseName 'release'
        contents {
            from('src/main/resources/')
            into("repository"){
                from("$buildDir/repository")
            }
        }
    }
}

如何在创建zip文件之前确保运行所有动态创建的发布任务?

我尝试为依赖于动态创建的任务的所有子项目创建一个新任务,但是当时它们似乎尚未创建。

subprojects {
    task offlineDep << {
        println 'Creating offline dependencies'
    }
    offlineDep.dependsOn {
        tasks.findAll { task -> task.name.endsWith('PublicationToDependenciesRepository') }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。通过收集工件的名称并生成我知道的任务名,我将在稍后创建它们并将它们作为依赖项添加。

subprojects {
    apply plugin: 'maven-publish'
    def offlineDependencyNames = []

    publishing {
        publications {
            configurations.each { config ->
                if (config.name == "offlineDependencies") {
                    def files = config.files
                    config.dependencies.each { dep ->
                        files.each { file ->
                            if (file.name == "${dep.name}-${dep.version}.jar") {
                                offlineDependencyNames << dep.name
                                "${dep.name}"(MavenPublication) {
                                    artifact (file) {
                                        groupId = "${dep.group}"
                                        artifactId = "${dep.name}"
                                        version = "${dep.version}"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        repositories {
            maven {
                name 'dependencies'
                url "${rootProject.buildDir}/repository"
            }
        }
    }

    task publishOfflineDependencies
    publishOfflineDependencies.dependsOn {
        offlineDependencyNames.collect { name ->
"publish${name[0].toUpperCase()}${name.substring(1)}PublicationToDependenciesRepository"
        }
    }
}


releaseDistZip.dependsOn {
    subprojects.collect {
        p -> p.path + ':publishOfflineDependencies'
    }
}