Run all publish tasks after first executing all test tasks in multiproject build

I’m using the ivy-publish plugin to publish artifacts to our Artifactory server. We have a multi-project build with ~35 projects, and I’d like to ensure that the test task for all projects are run before any of the publish tasks run, because otherwise it is possible to get partial publication of some of the projects if a UT failure occurs in a later project. I thought the following would work (in the master subprojects closure), but it doesn’t - there is no difference in the task ordering:

tasks.withType( PublishToIvyRepository ) { publishTask ->
            tasks.withType(Test) { testTask ->
                publishTask.mustRunAfter testTask
            }
       }

What am I missing?

Thanks in advance,

Tom

I think the problem here is that the ‘withType()’ method takes a configuration closure, whereas you are expecting a closure that takes a ‘Task’ as an argument. This should work.

tasks.withType(PublishToIvyRepository) {

dependsOn rootProject.subprojects.collect {

it.tasks.withType(Test)

}

}

Also, if you want to be sure that publication is only after tests are done you should use a ‘dependsOn’ relationship. Using ‘mustRunAfter’ will not force test to run.

This question has been asked before as well. http://forums.gradle.org/gradle/topics/how-do-i-run-tests-in-all-subprojects-before-uploading-any

Updated my comment above. You have to ask for the tasks of each subproject to get the behavior you want. In this context (since you are in the ‘subprojects’ closure) ‘tasks’ refers only to that subproject’s tasks so you would simply upload your archive after that subproject’s tests ran.

Thank you very much for your reply and explanation - it works perfectly.