# Running Jetty App and simple Java app at same time

**URL:** <https://discuss.gradle.org/t/running-jetty-app-and-simple-java-app-at-same-time/11357>\
**Category:** Help/Discuss\
**Created:** [August 26, 2015, 7:40pm UTC](https://discuss.gradle.org/t/running-jetty-app-and-simple-java-app-at-same-time/11357 "2015-08-26T19:40:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![curoli](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@curoli](https://discuss.gradle.org/u/curoli)\
**Post date:** [August 26, 2015, 7:40pm UTC](https://discuss.gradle.org/t/running-jetty-app-and-simple-java-app-at-same-time/11357/1 "2015-08-26T19:40:54Z")

</div>

```
 Hello,

```

I have a web app based on Jetty plugin and I want to write a profiler app that is a simple Java app that sends request to this web app. This means web app and profiler need to run at the same time. I’d like everything to be part of the same project and both launched by Gradle. But I don’t see how I can make Gradle run two tasks: if I start the web app (jettyRun), I don’t get a prompt back until it stops. If I start another instance of Gradle, it complains the build file is locked.

Is there a way? Thanks!

```
 Best, Oliver
```

---

<div class="post-metadata">

**Author:** ![Shinya\_Mochida](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/shinya_mochida/32/2393_2.png) [@Shinya\_Mochida](https://discuss.gradle.org/u/Shinya_Mochida)\
**Post date:** [August 26, 2015, 8:52pm UTC](https://discuss.gradle.org/t/running-jetty-app-and-simple-java-app-at-same-time/11357/2 "2015-08-26T20:52:38Z")

</div>

Add the following contents into your `build.gradle`(following uses `integTest` as task name).

```groovy
task integTest(type: Test) {
    // start jetty server
    doFirst {
        jettyRun.daemon = true
        jettyRun.start()
    }
    // stop jetty server
    doLast {
        jettyStop.stop()
    }
}

```

---

<div class="post-metadata">

**Author:** ![sterling](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sterling/32/5395_2.png) [@sterling](https://discuss.gradle.org/u/sterling)\
**Post date:** [August 27, 2015, 3:43am UTC](https://discuss.gradle.org/t/running-jetty-app-and-simple-java-app-at-same-time/11357/3 "2015-08-27T03:43:45Z")

</div>

Hey @curoli

What you want to use is a task dependency on the task that starts the Jetty application and a ‘finalizer’ task that stops the Jetty application.

So if `jettyStart` starts the Jetty application, `runProfiler` runs the profile application and `jettyStop` stops the Jetty application, you would do something like:

```gradle
runProfiler.dependsOn 'jettyStart'
runProfiler.finalizedBy 'jettyStop'

```

This will ensure that Jetty is running before the profiler starts and Jetty stops when the build finishes. For the built-in Jetty plug-in, you can turn on ‘daemon’ mode, which will make the Jetty task non-blocking. [https://docs.gradle.org/current/dsl/org.gradle.api.plugins.jetty.JettyRun.html#org.gradle.api.plugins.jetty.JettyRun:daemon](https://docs.gradle.org/current/dsl/org.gradle.api.plugins.jetty.JettyRun.html#org.gradle.api.plugins.jetty.JettyRun:daemon)

We generally encourage people to look at the Gretty plugin instead of using the built-in Jetty plugin because it’s much better and supports newer versions of Jetty. You can accomplish the same thing with it. Here’s some documentation for it: [http://akhikhl.github.io/gretty-doc/Integration-tests-support.html](http://akhikhl.github.io/gretty-doc/Integration-tests-support.html)

---

<div class="post-metadata">

**Author:** ![sterling](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sterling/32/5395_2.png) [@sterling](https://discuss.gradle.org/u/sterling)\
**Post date:** [August 27, 2015, 3:48am UTC](https://discuss.gradle.org/t/running-jetty-app-and-simple-java-app-at-same-time/11357/4 "2015-08-27T03:48:56Z")

</div>

Hey @Shinya_Mochida

Thanks for your answer. I wanted to point out a couple of best practices. See my answer above for a more idiomatic way of doing the same thing.

1. You shouldn’t call a task’s main action method directly. Gradle is responsible for making sure the task executes when it’s necessary. Calling the method directly might cause problems.
2. If you have an action that should always be performed after another action (even when that action may fail), you should use a [finalizer task](https://docs.gradle.org/current/userguide/more_about_tasks.html#N10F3E).

I’m glad you love Gradle 😄

---

<div class="post-metadata">

**Author:** ![curoli](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@curoli](https://discuss.gradle.org/u/curoli)\
**Post date:** [August 27, 2015, 12:02pm UTC](https://discuss.gradle.org/t/running-jetty-app-and-simple-java-app-at-same-time/11357/5 "2015-08-27T12:02:36Z")

</div>

Awesome, thanks for the advice, Shinya and Sterling!
