npeder
npeder

Reputation: 858

Gradle task dependencies on dynamically created tasks

I have a multiproject build where I need to publish some dependencies to a custom local Maven repository on disk and then add this folder to a distribution zip.

I define publishing tasks for each subproject using the maven-publish plugin.

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'
            }
        }
    }
}

Using the distribution plugin I create a zip file

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

How can I make sure that all the dynamically created publish tasks are run before creating the zip file?

I tried making a new task for all subprojects that depends on the dynamically created tasks, but it seems they're not created yet at that time.

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

Upvotes: 1

Views: 3508

Answers (1)

npeder
npeder

Reputation: 858

I found a solution to this problem. By collecting the names of the artifacts and generating the tasknames I know will be created later and adding them as dependencies.

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'
    }
}

Upvotes: 1

Related Questions