Search code examples
gradlebuild-processbuild.gradle

Add depencency to group of gradle tasks


I have a project with a bunch of publishing tasks:

Publishing tasks
----------------
generatePomFileForMavenJavaPublication - Generates the Maven POM file for publication 'mavenJava'.
generatePomFileForMavenPublication - Generates the Maven POM file for publication 'maven'.
publish - Publishes all publications produced by this project.
publishMavenJavaPublicationToMavenLocal - Publishes Maven publication 'mavenJava' to the local Maven repository.
publishMavenJavaPublicationToMavenRepository - Publishes Maven publication 'mavenJava' to Maven repository 'maven'.
publishMavenPublicationToMavenLocal - Publishes Maven publication 'maven' to the local Maven repository.
publishMavenPublicationToMavenRepository - Publishes Maven publication 'maven' to Maven repository 'maven'.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.

Now I want all of these tasks to be dependent on build, to ensure tests and checks are run before anything is published (by default publishing tasks are dependent on assemble).

Of course the following works:

publish.dependsOn(build)
publishToMavenLocal.dependsOn(build)

But it is cumbersome to maintain and it does not work for any generated task like publishMavenPublicationToMavenRepository. Is there a way to add a dependency to all publishing tasks? Of course I tried publishing.dependsOn(build) but then it just says:

Could not find method dependsOn() for arguments [task ':build'] on org.gradle.api.publish.internal.DefaultPublishingExtension_Decorated@5b5f8cd6.

Solution

  • There's no possibility to define a group dependency, however you can try:

    tasks.findAll { it.name.startsWith('publish') }*.dependsOn(DA_TASK)