# Executing tasks by name on multi-project

**URL:** <https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046>\
**Category:** Help/Discuss\
**Created:** [July 14, 2023, 3:24pm UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046 "2023-07-14T15:24:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobma](https://avatars.discourse-cdn.com/v4/letter/b/a9a28c/32.png) [@bobma](https://discuss.gradle.org/u/bobma)\
**Post date:** [July 14, 2023, 3:24pm UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046/1 "2023-07-14T15:24:16Z")

</div>

As per the documentation we can run tasks on all subprojects from the root [Multi-Project Builds](https://docs.gradle.org/current/userguide/intro_multi_project_builds.html#sec:executing_tasks_by_name) :

> The command `gradle test` will execute the `test` task in any subprojects, relative to the current working directory, that have that task. If you run the command from the root project directory, you’ll run `test` in _api_ , _shared_ , _services:shared_ and _services:webservice_ . If you run the command from the _services_ project directory, you’ll only execute the task in _services:shared_ and _services:webservice_ .

Could you please provide an example of the implementation, I assume we won’t use the dependsOn and getTaskByName to achieve this?  
thanks

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [July 15, 2023, 6:52pm UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046/2 "2023-07-15T18:52:03Z")

</div>

That is about executing manually, not programmatically.

---

<div class="post-metadata">

**Author:** ![bobma](https://avatars.discourse-cdn.com/v4/letter/b/a9a28c/32.png) [@bobma](https://discuss.gradle.org/u/bobma)\
**Post date:** [July 16, 2023, 5:45pm UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046/3 "2023-07-16T17:45:32Z")

</div>

I am wondering mainly if there is a solution out of the box that allows to run the task from root on all subprojects or it needs to be configured with dependency.  
For now I define the tasks as follow :

```gradle
        tasks.register("testing") {
        description = "Runs the testing task on all subprojects"
        logger.lifecycle(" it's the root... ")
        dependsOn(subprojects.map { it.tasks.getByName("testing") })
    }

```

and on the subproject

```gradle
tasks.register("testing") {        
        description = "Runs the testing task on subproject"
         logger.lifecycle(" it's the from subproject... ")
    }

```

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [July 16, 2023, 11:15pm UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046/4 "2023-07-16T23:15:53Z")

</div>

Just don’t do it at all.  
As the documentation you quoted yourself states.  
If you run `./gradlew testing`, it anyway runs all `testing` tasks in all sub projects where such a task exists.

---

<div class="post-metadata">

**Author:** ![bobma](https://avatars.discourse-cdn.com/v4/letter/b/a9a28c/32.png) [@bobma](https://discuss.gradle.org/u/bobma)\
**Post date:** [July 17, 2023, 8:34am UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046/5 "2023-07-17T08:34:00Z")

</div>

Yes I tried this from the root and it worked. What if we would like to execute task on all sub-modules:

```plaintext
Root project 'multiproject'
+--- Project ':api'
+--- Project ':services'
| +--- Project ':services:shared'
| \--- Project ':services:webservice'
\--- Project ':shared'

```

How to enable `./gradlew :services:testing` to run task on the services:shared and services:webservice ?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [July 21, 2023, 11:36am UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046/6 "2023-07-21T11:36:50Z")

</div>

```bash
cd services
../gradlew testing

```

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [July 21, 2023, 11:38am UTC](https://discuss.gradle.org/t/executing-tasks-by-name-on-multi-project/46046/7 "2023-07-21T11:38:56Z")

</div>

Of course you could also add task dependencies if you really need.  
You could also do it like `dependsOn(":services:shared:testing", ":services:webservice:testing")`, the task paths are lazily resolved, so there will be no order-problem at configuration time.
