# For the 'application' plugin how to add a second run task which targets another mainClass

**URL:** https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540
**Category:** Help/Discuss
**Created:** [September 22, 2023, 2:19am UTC](https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540 "2023-09-22T02:19:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://discuss.gradle.org/u/halloleo)
#### Post date: [September 22, 2023, 2:19am UTC](https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540/1 "2023-09-22T02:19:28Z")

</div>

For a Java project I have set up Gradle 8.3 using the `'application'` plugin.

In the build file I set

```gradle
application {
    mainClass = 'com.mycompany.myproject.App1'
}

```

so that `gradle run` runs the `main` function in class `App1`.

In the same project I have a class

```
 com.mycompany.myproject.App2 

```

which I would like to run with another task, say `runApp2`. How do I “clone” the standard `run` task and then change for the cloned task the `mainClass` property, so that `gradle runApp2` runs the `main` function in class `App2`?

Thanks heaps for any pointers!

**What have I tried so far?**

- Inspired by [“Run main method using gradle ‘run’ task”](https://stackoverflow.com/a/28610276/65889) I have added

* * *

_PS:_ For more details on build file and directory structrture see [“How to define multiple run commands for different mainClasses in Gradle’s build file?”](https://stackoverflow.com/questions/77148603/how-to-define-multiple-run-commands-for-different-mainclasses-in-gradles-build).

---

<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: [September 22, 2023, 8:40am UTC](https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540/2 "2023-09-22T08:40:42Z")

</div>

There is no way to “copy” or “clone” a task, other than copying or wiring its properties one-by-one.  
If they are lazy properties, this might work, if they are not yet lazy properties, not so good.  
But usually you just configure a new task as you need it.  
So in your case something like:

```groovy
tasks.register("runApp2", JavaExec) {
    group = ApplicationPlugin.APPLICATION_GROUP
    classpath = sourceSets.main.runtimeClasspath
    mainClass = 'com.mycompany.myproject.App2'
}

```

---

<div class="post-metadata">

### Author: ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://discuss.gradle.org/u/halloleo)
#### Post date: [September 23, 2023, 7:41am UTC](https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540/3 "2023-09-23T07:41:41Z")

</div>

Cool. Thanks. Will try it out next week when I’m back at my Java machine!

---

<div class="post-metadata">

### Author: ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://discuss.gradle.org/u/halloleo)
#### Post date: [September 25, 2023, 2:01am UTC](https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540/4 "2023-09-25T02:01:59Z")

</div>

Thanks again, @Vampire. It works indeed. 🙂

However I don’t understand how the task knows it is a _`run`_ task which compiles the code before running it. – When I [read up on JavaExec](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html), I saw only that tasks of this type _execute_ Java classes via the JVM, but not that they _compile_ the source code as well.

---

<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: [September 25, 2023, 10:01am UTC](https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540/5 "2023-09-25T10:01:46Z")

</div>

That’s right, `JavaExec` runs Java classes, but does not compile them.  
But you configured `classpath = sourceSets.main.runtimeClasspath`.  
The `classpath` property of `JavaExec` is marked with `@Classpath` which effectively is a specialization of `@InputFiles`.  
`sourceSets.main.runtimeClasspath` is a `FileCollection`. If that file collection is properly filled with properties that carry task-dependency information and not just with plain tasks, it knows which tasks need to run if its value is needed.

In Gradle you should almost never configure paths, but always wire task outputs to task inputs. By doing so, you get the necessary implicit task dependencies automatically. Like you have seen in this case where you wired a file collection that has task dependency information to an input property of a task and thus got automatically the necessary task dependency.

---

<div class="post-metadata">

### Author: ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://discuss.gradle.org/u/halloleo)
#### Post date: [September 27, 2023, 9:18am UTC](https://discuss.gradle.org/t/for-the-application-plugin-how-to-add-a-second-run-task-which-targets-another-mainclass/46540/6 "2023-09-27T09:18:15Z")

</div>

Makes total sense, @Vampire! Thanks for you detailed explanation!

I get the impression, Gradle is pretty cool in this regards. – Hard to understand for a newbie though. Will need to work through more of the doco…
