How to make ivy-publish generated publish task depend on another task?

TL;DR:
What I have: ./gradlew packageFoo && ./gradlew publishFoo
What I want: ./gradlew publishFoo

I’m applying and configuring ivy-publish in a project.afterEvaluate block, to publish android apks to a local ivy repo for sharing with other tools How do I make the publications’ publish tasks depend on the tasks that produce the artifact they are publishing? Right now, I have to run the packageFoo task before I run the publishFooPublicationToLocalRepository.

project.afterEvaluate {
    apply plugin: 'ivy-publish'
    project.publishing { projPub ->
        publications { pubCfg ->

        // create a publication for each flavor and type
        project.android.productFlavors.all { flavor ->
            ['debug', 'release'].each { type ->
                def buildName = "${flavor.name.capitalize()}${type.capitalize()}"
                def pkgTaskName = "package${buildName}"
                def pkgTask = project.tasks.getByName(pkgTaskName)
                def apkFile = pkgTask.outputs.files.find { f -> f.name.endsWith('.apk') }

                "${buildName}"(IvyPublication) {
                    artifact apkFile
                    organisation "org.me"
                    module "myname-${flavor.name}-${type}"
                    revision project.version
                }   
            }   
        }   
    }   
    repositories { ... set up "local" repo }
}   

I tried having more code after the project.publishing block which then explicitly wires up the dependencides, but those don’t seem to run in order so it doesn’t work.

Configure the artifact of the publication to be built by the task:

artifact( apkFile ) {
    builtBy pkgTask
}
1 Like

Thanks a lot, that works great, not sure I how I missed the documentation for it.