Gradle: artifactoryPublish execute from within build.gradle does not publish the artifacts to the repository

I am executing the artifactoryPublish task from within build.gradle for a certain set of sub projects (in order to avoid explicit call from the command line)

task publishAllApis() {

    doLast {
        project.subprojects.each { subproject ->
            if (subproject.name.endsWith("api")) {
                println "executing publish" + subproject.name
               
                subproject.getTasks().getByName("artifactoryPublish").execute()
            }
        }
    }
}


        task all () {
           dependsOn buildAllApis
           dependsOn installAllApis
           dependsOn publishAllApis
       }

On running ./gradlew clean all , it calls publishAllApis and prints the below message

 Putting task artifact state for task ':dataservice-plaid-api:artifactoryPublish' into context took 0.0 secs.
Executing task ':dataservice-plaid-api:artifactoryPublish' (up-to-date check took 0.0 secs) due to:  Task has not declared any outputs.

but does not deploy to the artifactory. All the individual sub projects have their publishing blocks and if I execute ./gradlew clean :dataservice-api:artifactoryPublish, it publishes it but not via the custom task

execute() is not part of Gradle’s Task API and should never be called. You want to use task dependencies instead.

task publishAllApis {
    dependsOn( subprojects.findAll {
        it.name.endsWith("api")
    }.collect {
        it.tasks.named("artifactoryPublish")
    } )
}

Calling tasks.named may fail since the task must exist, meaning the subprojects must be configured first. To avoid this issue, you can add a call to evaluationDependsOnChildren() to the buildscript containing the publishAllApis task.

Thank you Chris for your response. I was able to get it to run last week. I did remove the execute and did something similar to your suggestion (which looks more professional)

task publishAllApis () {
subprojects.each { subproject ->
if(subprojectTypeApi(subproject.name)){
dependsOn “$subproject.name:artifactoryPublish”
}
}
}
where subprojectTypeApi is checking for the name ending with api.

Good idea using a string for dependsOn, avoids the need for evaluationDependsOnChildren.

Consider using subproject.path instead of subproject.name. Doing so will support the case where you have nested subprojects.